Math类
Math类是用于数学计算的类,不需要创建对象就可以调用类中的方法,如求绝对值、四舍五入、求某个数的n次幂等,以下为一些常见的Math方法示例:
1 2 3 4 5 6 7 8 9 10 11
| public class test1 { public static void main(String[] args) { System.out.println(Math.abs(-1)); System.out.println(Math.ceil(3.21)); System.out.println(Math.floor(4.56)); System.out.println(Math.round(5.51)); System.out.println(Math.max(3,5.4) + Math.min(3,5.4)); System.out.println(Math.pow(3.5,3)); System.out.println(Math.sqrt(4) + Math.cbrt(8)); } }
|
Math类也定义了一些较高精度的数学常数,如圆周率pi和自然常数e等
1 2 3 4 5 6
| public static final double E = 2.718281828459045; public static final double PI = 3.141592653589793; public static final double TAU = 2.0 * PI; private static final double DEGREES_TO_RADIANS = 0.017453292519943295; private static final double RADIANS_TO_DEGREES = 57.29577951308232;
|
System类
System类是与系统相关的类,提供了操作系统相关的方法,不需要创建对象就可以调用类中的方法,以下是示例方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Arrays;
public class test1 { public static void main(String[] args) { System.out.println(System.currentTimeMillis());
int[] arr = {1,2,3,4,5,6}; int[] arr1 = new int[3]; System.arraycopy(arr, 3, arr1, 0, 3); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr1));
System.exit(0); } }
|
其中时间原点为1970年1月1日0时0分0秒(UTC+0)
Runtime类
Runtime类与程序运行的环境有关,也不需要创建对象就可以调用类中的方法,以下为一些常见的方法示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.io.IOException;
public class test1 { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime(); System.out.println(rt.availableProcessors()); System.out.println(rt.maxMemory()); System.out.println(rt.totalMemory()); System.out.println(rt.freeMemory()); rt.exec(new String[]{"notepad"}); rt.exit(0); } }
|
Object类
Object类是Java中的最高父类,所有的类都直接或间接继承于Object类
Object类没有带参数的构造方法,只有空参构造方法,但是可以使用其子类的构造方法
1 2 3 4
| public static void main(String[] args) { Object obj = new Object(); Object obj1 = new int[4]; }
|
以下为Object类中的常见成员方法,这些方法都可以被重写:
1 2 3 4 5 6
| public class test { public static void main(String[] args) { System.out.println(obj1.toString()); System.out.println(obj1.equals(obj2)); } }
|
此外,Object类中还有一个 clone() 方法,用于克隆对象的属性值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class test1 implements Cloneable { int anInt; double aDouble; String aString;
@Override protected Object clone() throw CloneNotSupportedException { return super.clone(); } }
public class Main { public static void main(String[] args) throws CloneNotSupportedException { test1 t1 = new test1(1, 2.0, "3"); test1 t2 = (test1) t1.clone(); System.out.println(t1); System.out.println(t2); } }
|
这里需要介绍一下深克隆与浅克隆的区别,它们主要体现在对引用数据类型成员变量的复制(除了字符串类型):
- 浅克隆:直接复制成员的地址。这表明浅克隆出来的对象B,与原对象A共用相同的引用数据类型成员变量
- 深克隆:先创建与成员变量X相同类型的新变量Y,并复制成员变量X的内容,然后再复制新变量Y的地址。这表明深克隆出来的对象B,与原对象A不共用引用数据类型的成员变量
clone() 方法默认为浅克隆,当然我们也可以通过重写方法来实现深克隆
Objects类
Objects是一个工具类,提供了一些工具方法,不需要创建对象
Objects有三个常用的成员方法:
1 2 3 4 5 6 7 8 9 10 11 12
| import java.util.Objects;
public class Main { public static void main(String[] args) throws CloneNotSupportedException { test1 t1 = new test1(1, 2.0, "3"); test1 t2 = (test1) t1.clone();
System.out.println(Objects.equals(t1, t2)); System.out.println(Objects.isNull(t1)); System.out.println(Objects.nonNull(t2)); } }
|