Math
Math类概述
Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数
Math类的常用方法
方法名 | 说明 |
---|---|
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 返回大于或等于参数值的最小double值,等于一个整数 |
public static double floor(double a) | 返回小于或等于参数值的最大double值,等于一个整数 |
public static int round(float a) | 按照四舍五入返回最近参数的int |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow(double a,double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,取值范围[0.0 , 1.0) |
public class MathDemo {
public static void main(String[] args) {
//public static int abs(int a)
System.out.println(Math.abs(123-13));
System.out.println(Math.abs(13-123));
System.out.println("--------------");
//public static double cell(double a)
System.out.println(Math.ceil(3.1415926));
System.out.println(Math.ceil(-3.1415926));
System.out.println("--------------");
//public static double floor(double a)
System.out.println(Math.floor(3.1415926));
System.out.println(Math.floor(-3.1415926));
System.out.println("--------------");
//public static int round(float a)
System.out.println(Math.round(12.34F));
System.out.println(Math.round(-56.78F));
System.out.println("--------------");
//public static int max(int a,int b)
System.out.println(Math.max(66,99));
System.out.println(Math.max(-66,-99));
System.out.println("--------------");
//public static int min(int a,int b)
System.out.println(Math.min(66,99));
System.out.println(Math.min(-66,-99));
System.out.println("--------------");
//public static double pow(double a,double b)
System.out.println(Math.pow(2.0,9.0));
System.out.println("--------------");
//public static double random()
System.out.println(Math.random());
System.out.println("--------------");
//0-100的随机数,取不到100
System.out.println((int)((Math.random()*100)));
//1-100的随机数,能取到100
System.out.println((int)((Math.random()*100))+1);
}
}
输出:
110 110 -------------- 4.0 -3.0 -------------- 3.0 -4.0 -------------- 12 -57 -------------- 99 -66 -------------- 66 -99 -------------- 512.0 -------------- 0.3796388531826984 -------------- 14 16
System
System类概述
System类包含一些有用的类字段和方法,它不能被实例化
System类的常用方法
方法名 | 说明 |
---|---|
static void exit(int status) | 终止当前运行的Java虚拟机,非零表示异常终止 |
static long currentTimeMillis() | 返回当前时间(以毫秒为单位) <tab>返回: <tab><tab>当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。 |
public class SystemDemo {
public static void main(String[] args) {
/*System.out.println("开始");
//static void exit(int status)
System.exit(1);
System.out.println("结束");*/
//static long currentTimeMillis(); 用法
// System.out.println(System.currentTimeMillis());
System.out.println(System.currentTimeMillis() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
long start=System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
long end=System.currentTimeMillis();
System.out.println("共耗时"+(end-start)+"毫秒");
}
}
Object
Object概述
Object是类层次结构的根,每个类都可以将Object作为超类,所有的类都直接或者间接的继承自该类
Object类的toString()方法
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s=new Student();
s.setName("阿朱");
s.setAge(21);
System.out.println(s);//com.CommonAPI.Object.Student@7ba4f24f
System.out.println(s.toString());
/*public void println(Object x) {
String s = String.valueOf(x);
if (getClass() == PrintStream.class) {
writeln(String.valueOf(s));
} else {
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());
}
*/
}
}
Object类的equals()方法
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
/*
this --- s1
o --- s2
*/
//比较地址是否相同,如果相同,直接返回true
if (this == o) return true;
//判断参数是否为null
//判断两个对象是否来自于同一个类
if (o == null || getClass() != o.getClass()) return false;
//向下转型
Student student = (Student) o; //student = s2;
//比较姓名内容是否相同
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("阿朱");
s1.setAge(21);
Student s2 = new Student();
s2.setName("阿朱");
s2.setAge(21);
//比较两个内容是否相同
// System.out.println(s1 == s2);
System.out.println(s1.equals(s2));//false
/*
public boolean equals(Object obj) {
//this --- s1
//obj --- s2
return (this == obj);
}
*/
}
}
小结
方法名 | 说明 |
---|---|
public String toString() | 返回对象的字符串表示形式,建议所有子类重写该方法,自动生成 |
public boolean equals(Object ob) | 比较对象是否相等,默认比较地址,重写可以比较内容,自动生成 |