Math
1.1 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(88));
System.out.println(Math.abs(-88));
//public static double ceil(double a)返回大于或者等于参数的最小double值,等于一个整数
System.out.println(Math.ceil(12.56));
System.out.println(Math.ceil(11.56));
//public static double floor(double a )返回大于或者等于参数的最大double值,等于一个整数
System.out.println(Math.floor(12.56));
//public static int round(float a )按照四舍五入返回最接近参数的int
System.out.println(Math.round(12.34F));
System.out.println(Math.round(12.56F));
//public static int max(int a,int b)返回int中两个较大的值
System.out.println(Math.max(12,16));
//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)
System.out.println((int)(Math.random()*100));
}
}
System 类
System 包含几个有用的类字段和方法,他不能被实例化
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前运行的虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
public class SystemDemo {
public static void main(String[] args) {
System.out.println("Begin");
// public static void exit(int status)终止当前运行的虚拟机,非零表示异常终止
//System.exit(0);
System.out.println("End");
//public static long currentTimeMillis()返回当前时间(以毫秒为单位)
System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365+"年");
long start=System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
long end=System.currentTimeMillis();
System.out.println("共耗时:"+(end-start)+"毫秒");
}
}
Object类
Object类的概述
Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接的继承自该类
构造方法:public Object()
回想到面向对象中,为什么说子类的构造方法默认访问的是父类的无参构造方法?
因为他们的顶级父类只有无参构造方法
方法名 | 说明 |
---|---|
public String toString() | 返回对象的字符串形式,建议所有子类重写该方法,自动生成 |
public boolean equals(Object o) | 比较对象是否相等。默认比较地址,重写可以比较内容,自动生成 |
public class ObjectDemo {
public static void main(String[] args) {
Student student = new Student();
student.setAge(18);
student.setName("Lucas");
System.out.println(student);
System.out.println(student.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());
}
*/
}
}
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
//判断两个对象是否来自于一个类
if (o == null || getClass() != o.getClass()) return false;
//向下转型java
Student student = (Student) o;//student=s2
//比较姓名年龄是否相同
return age == student.age && Objects.equals(name, student.name);
}
System.out.println(student.equals(s2));
Arrays 类
最常用的两种方法
方法名 | 说明 |
---|---|
public static String toString(int[]a) | 返回指定数组的内容的字符串表现形式 |
public static void sort(int[]a) | 按照数字顺序排列指定的数组 |
基本类型包装类
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer
方法名 | 说明 |
---|---|
public static Integer valueOf(int i) | 返回指定的int值的Integer实例 |
public staitc Integer valueOf(String s) | 返回一个保存指定值的Integer对象String |
public class IntegerDemo {
public static void main(String[] args) {
//int---->String
int number=100;
//方式1
String s1=""+number;
System.out.println(s1);
//方法二
//public static String valueOf(int i) 该方法是String 类中的方法
String s2=String.valueOf(number);
System.out.println(s2);
System.out.println("===========================");
//String----->int
String s="100";
//方式1
//String--->Integer---->int
Integer i=Integer.valueOf(s);
//public int intValue()
int x=i.intValue();
System.out.println(x);
//方式二
//public static int parseInt(String s) 该方法是Integer类中的方法
int y=Integer.parseInt(s);
System.out.println(y);
}
}