深入探讨Java.lang包

发布时间:2023-05-23

一、Object类

Object类是Java.lang包中的一个重要类,是所有类的父类,它提供了若干方法,可供所有子类继承和使用。 其中,Object类中最常用的方法是equals()方法和toString()方法。equals()方法用于判断对象是否相等,默认情况下比较的是对象的引用值,可以通过重写该方法实现细节的比较;而toString()方法则返回对象的字符串表示形式,方便输出和打印。

public class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

二、String类

String是Java中常用的一个类,用于表示字符串,具有不可变性和线程安全性。这是因为一旦字符串对象被创建,它的值就不能被改变,对其修改会产生新的对象。 String类中常用的方法有: 1)equals()方法和==运算符,用于比较字符串是否相等,它们的区别在于equals()比较的是字符串的内容,而==比较的是对象的引用。 2)charAt()方法和substring()方法,用于获取字符串中的某个字符和某个子串。 3)length()方法和indexOf()方法,分别用于获取字符串的长度和查找某个字符或子串第一次出现的位置。 4)concat()方法、replace()方法和trim()方法,用于拼接字符串、替换字符串和去除字符串两边的空格。

public class StringExample {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "world";
        String str3 = str1 + str2; //使用+号进行拼接
        String str4 = str1.concat(str2); //使用concat()方法进行拼接
        System.out.println(str3); //输出"helloworld"
        System.out.println(str4); //输出"helloworld"
        String str5 = " ab cd ";
        String str6 = str5.trim(); //使用trim()方法去除两边空格
        System.out.println(str5); //输出" ab cd "
        System.out.println(str6); //输出"ab cd"
    }
}

三、Math类

Math类是Java.lang包中的一个用于数学操作的类,提供了若干数学函数,包括常用的数学运算和三角函数等。 Math类中常用的方法有: 1)abs()方法和max()/min()方法,分别用于获取数值的绝对值和最大/小值。 2)random()方法,用于生成一个伪随机数,返回值为一个0到1之间的随机数。 3)sqrt()方法、pow()方法和exp()方法,用于计算平方根、次方和指数。

public class MathExample {
    public static void main(String[] args) {
        int a = -10;
        int b = 20;
        double c = -15.6;
        System.out.println(Math.abs(a)); //输出10
        System.out.println(Math.max(a, b)); //输出20
        System.out.println(Math.min(a, b)); //输出-10
        System.out.println(Math.random()); //输出一个0到1之间的随机数
        System.out.println(Math.sqrt(Math.abs(c))); //输出3.9475734493587844
    }
}

四、System类

System类是Java.lang包中的一个用于访问系统资源的类,提供了若干与系统操作相关的方法。 System类中常用的方法有: 1)currentTimeMillis()方法,用于获取当前时间的毫秒数。 2)arraycopy()方法,用于将一个数组的某个范围内的元素复制到另一个数组中。 3)gc()方法,用于请求JVM进行垃圾回收。

public class SystemExample {
    public static void main(String[] args) {
        long start = System.currentTimeMillis(); //获取当前时间的毫秒数
        int[] a = {1,2,3,4,5};
        int[] b = new int[5];
        System.arraycopy(a, 0, b, 0, 5); //将a数组复制到b数组
        long end = System.currentTimeMillis();
        System.out.println("复制数组所花时间: " + (end - start) + " 毫秒");
        System.gc(); //请求JVM进行垃圾回收
    }
}

五、Throwable类

Throwable类是Java.lang包中用于表示异常的基类,所有的异常类都是从它派生而来的。 Throwable类中常用的方法有: 1)getMessage()方法和printStackTrace()方法,分别用于获取异常信息和打印异常堆栈信息。 2)fillInStackTrace()方法,用于重置异常信息和堆栈信息。

public class ThrowableExample {
    public static void main(String[] args) {
        try {
            int a = 1 / 0;
        } catch (Throwable throwable) {
            System.out.println(throwable.getMessage()); //输出" / by zero"
            throwable.printStackTrace(); //打印异常堆栈信息
        }
    }
}

六、小结

以上介绍了Java.lang包中几个重要的类,包括Object类、String类、Math类、System类和Throwable类。这些类在Java的开发中具有广泛的应用。我们可以通过熟悉和掌握这些类的特点和方法,提高我们的Java开发能力。