Math类概述
Math包含执行基本数字运算的方法,类的成员都是静态的,通过类名就可以直接调用
常用方法
public class MathDemo {
/**
*测试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//返回参数绝对值
System.out.println(Math.abs(-1));
System.out.println(Math.abs(1));
System.out.println(Math.abs(-1.23));
System.out.println("----------------");
//返回大于等于参数的最小整数值(double)
System.out.println(Math.ceil(2.1));
System.out.println("----------------");
//返回小于等于参数的最大整数值(double)
System.out.println(Math.floor(2.1));
System.out.println("----------------");
//返回按四舍五入,最接近参数的整数
System.out.println(Math.round(2.4));
System.out.println(Math.round(2.5));
System.out.println("----------------");
//返回两个数中最大值、最小值
System.out.println(Math.max(5, 6));
System.out.println(Math.min(5, 6));
System.out.println("----------------");
//返回a的b次幂
System.out.println(Math.pow(2, 3));
System.out.println("----------------");
//返回0-1之间随机数
System.out.println(Math.random());
}
}
System类
常用方法
public class MathDemo {
/**
*测试
*/
public static void main(String[] args) {
//终止当前运行的JAVA虚拟机,0表异常
System.out.println("开始");
System.exit(0);
System.out.println("结束");
}
}
会输出什么呢?????
public class MathDemo {
/**
*测试
*/
public static void main(String[] args) {
//返回当前时间-距离1970-1-1的时间(以毫秒为单位)
System.out.println(System.currentTimeMillis());
System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365+"年");
}
}
还可以计算一段代码运行时间
long last=System.currentTimeMillis();
for(int i=0;i<10000;i++){
System.out.println(i);
}
long end=System.currentTimeMillis();
System.out.println("共运行了"+(end-last)+"毫秒");
Object类
Object类是类结构的根,每一个类都有Object类作为超类
问:为什么子类的构造方法都默认访问父类的无参构造方法
因为它们的顶级父类(Object)只有无参构造方法
常用方法
toString();
//返回对象的字符串表示形式,建议重写此方法
public String toString();
我们建立一个学生类,对其进行测试
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
直接进行代码测试,会发现
public class ObjectDemo {
/**
* 测试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.setName("菜鸡");
s.setAge(88);
System.out.println(s);
}
}
通过底层代码发现其调用了object的toString方法
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
底层代码可选中方法,右键选择declaration查看(myeclipse)
System.out.println(s);
System.out.println(s.toString());
上面两者效果等同
当我们重写toString 方法
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+","+"age="+age;
}
equals();
public class ObjectDemo {
/**
* 测试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=new Student();
Student s2=new Student();
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
会输出什么呢?????
答:全为false
默认状态下,比较两者的地址
可以进行方法重写(自动生成)
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
就会输出true了