常用API

本文详细介绍了Java中的Math类,包括其常用的数学运算方法如绝对值、四舍五入、最大值和最小值等,并通过示例展示了它们的使用。接着讨论了System类,特别是exit()方法和currentTimeMillis()方法的应用。最后探讨了Object类的toString()和equals()方法,强调了重写这两个方法的重要性。文章还提供了实际的代码示例以加深理解。
摘要由CSDN通过智能技术生成

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)比较对象是否相等,默认比较地址,重写可以比较内容,自动生成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值