1、Math类概述
2、Math中方法的调用方式
3、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) |
System类的常用方法
| 方法名 | 说明 |
|---|---|
| public static void exit(int status) | 终止当前运行的 Java 虚拟机,非零表示异常终止 |
| public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
示例代码
xxxxxxxxxxpublic class SystemDemo { public static void main(String[] args) { // 获取开始的时间节点 long start = System.currentTimeMillis(); for (int i = 1; i <= 10000; i++) { System.out.println(i); } // 获取代码运行结束后的时间节点 long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); }}Object类概述
查看方法源码的方式
重写toString方法的方式
toString方法的作用:
示例代码:
xxxxxxxxxxclass Student extends Object { 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; } 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(30); System.out.println(s); System.out.println(s.toString()); }}运行结果:
xxxxxxxxxxStudent{name='林青霞', age=30}Student{name='林青霞', age=30}equals方法的作用
重写equals方法的场景
重写equals方法的方式
示例代码:
xxxxxxxxxxclass 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; } public boolean equals(Object o) { //this -- s1 //o -- s2 if (this == o) return true; 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(30); Student s2 = new Student(); s2.setName("林青霞"); s2.setAge(30); //需求:比较两个对象的内容是否相同 System.out.println(s1.equals(s2)); }}面试题
xxxxxxxxxx// 看程序,分析结果String s = “abc”;StringBuilder sb = new StringBuilder(“abc”);s.equals(sb); sb.equals(s); public class InterviewTest { public static void main(String[] args) { String s1 = "abc"; StringBuilder sb = new StringBuilder("abc"); //1.此时调用的是String类中的equals方法. //保证参数也是字符串,否则不会比较属性值而直接返回false //System.out.println(s1.equals(sb)); // false //StringBuilder类中是没有重写equals方法,用的就是Object类中的. System.out.println(sb.equals(s1)); // false }}常用方法
| 方法名 | 说明 |
|---|---|
| public static String toString(对象) | 返回参数中对象的字符串表示形式。 |
| public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
| public static Boolean isNull(对象) | 判断对象是否为空 |
| public static Boolean nonNull(对象) | 判断对象是否不为空 |
示例代码
学生类
xxxxxxxxxxclass 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; } public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }测试类
xxxxxxxxxxpublic class MyObjectsDemo { public static void main(String[] args) { // public static String toString(对象): 返回参数中对象的字符串表示形式。 // Student s = new Student("小罗同学",50); // String result = Objects.toString(s); // System.out.println(result); // System.out.println(s); // public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数. //Student s = new Student("小花同学",23); // Student s = null; // String result = Objects.toString(s, "随便写一个"); // System.out.println(result); // public static Boolean isNull(对象): 判断对象是否为空 //Student s = null; // Student s = new Student(); // boolean result = Objects.isNull(s); // System.out.println(result); // public static Boolean nonNull(对象): 判断对象是否不为空 //Student s = new Student(); Student s = null; boolean result = Objects.nonNull(s); System.out.println(result); } }作用
可以用来进行精确计算
构造方法
| 方法名 | 说明 |
|---|---|
| BigDecimal(double val) | 参数为double |
| BigDecimal(String val) | 参数为String |
常用方法
| 方法名 | 说明 |
|---|---|
| public BigDecimal add(另一个BigDecimal对象) | 加法 |
| public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
| public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
| public BigDecimal divide (另一个BigDecimal对象) | 除法 |
| public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
总结
代码示例:
xxxxxxxxxxBigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);参数1 ,表示参与运算的BigDecimal 对象。参数2 ,表示小数点后面精确到多少位参数3 ,舍入模式 BigDecimal.ROUND_UP 进一法 BigDecimal.ROUND_FLOOR 去尾法 BigDecimal.ROUND_HALF_UP 四舍五入