Math
常量有两个
E=2.~
PI=3.1415926
没有构造方法,都是静态方法,常量 。可以直接通过类名调用
随机数可以控制在一定范围之间的随机数,[x,y)
x+(int)(Math.random()*(y-x))
System类
都是静态的
System.out.println("开始");
System.exit(0);
System.out.println("结束");//虚拟机已经退出,不会输出结果
//计时用
long time = System.currentTimeMillis();
for (int i = 0; i <10000 ; i++) {
System.out.println(i);
}
long end =System.currentTimeMillis();
System.out.println(end-time);
Object类
只有一个无参构造方法,Object()
常用方法
toString
equals
Student s = new Student();
s.setAge(12);
s.setName("xiaoli");
//对象打印直接是类名@地址值,建议重写toString方法
System.out.println(s);
System.out.println(s.toString());
equals方法的重写
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
equals
有两个相似的就是== 与equals
==直接比较的是地址
equals对于字符串比较的是内容
对象类引用变量比较的是地址
因此需要重写equals方法,使其比较内容
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}