java的一些常用API(上)

之前的String,StringBuilder,Scanner,Random都是API

一,Math

Math(无需导包)包含执行基本数字运算的方法
通过类名直接使用方法

下面是一些常用方法

public class MathTest {
    public static void main(String[] args) {
//        绝对值
        System.out.println(Math.abs(-12));
//        向上取整(只是去掉小数点后面的非0,结果不是int型)
        System.out.println(Math.ceil(1.11));
//        向下取整(只是去掉小数点后面的非0,结果不是int型)
        System.out.println(Math.floor(1.11));
//        四舍五入为整数(int),貌似只看十分位
        System.out.println(Math.round(1.44));
        System.out.println(Math.round(1.45));
//        返回两个数的最大值,同理min返回最小值
        System.out.println(Math.max(1.44,2.22));
//        返回element1的element2次幂
        System.out.println(Math.pow(2,5));
//        随机小数(0<=x<1)
        System.out.println(Math.random());
    }
}

结果

二,System

System(无需导包)
通过类名直接访问

下面是一些常用方法

public class SystemTest {
    public static void main(String[] args) {
//        currentTimeMillis()方法返回1970年1月1日但现在经过的时间,一毫秒为单位,可以用来计时,如下
        long starttime=System.currentTimeMillis();
        for(int i=0;i<=10000;i++){
            System.out.println("用时"+i+"毫秒");
        }
        long endtime=System.currentTimeMillis();
        System.out.println("用时"+(endtime-starttime)+"毫秒");
//        exit()方法用来结束java虚拟机,终止程序,0是异常终止
        System.out.println("开始");
        System.exit(0);
        System.out.println("结束");
    }
}

结果:

三,Object

每个类都有它作为超类,所有的类直接或间接的继承它

toString方法

//Student类
public class Student{
    public String name;
    public int age;
//    toString方法继承字Object类,原本会返回地址,不好观察,重写它容易观察
    @Override
    public String toString(){
        return ("名字:"+name+"\t"+"年龄:"+age);
    }
}



//test类
public class test {
    public static void main(String[] args) {
        Student s=new Student();
        s.name="小吕";
        s.age=21;
        System.out.println(s);
    }
}

equals方法

//Animal类
public class Animal {
    public String name;
    public int age;
//    @Override
//    public String toString(){
//        return ("名字:"+name+"\t"+"年龄:"+age);
//    }

    @Override
    public boolean equals(Object o) {
//        判断a与c地址是否相同,注意等号直接连接对象是比较地址值
        if (this == o) return true;
//        判断c是否为空以及他们是不是来自同一个类
        if (o == null || getClass() != o.getClass()) return false;
//        向下转型,不过这里测试的都是Animal类
        Animal animal = (Animal) o;
//        age不相同直接false
        if (age != animal.age) return false;
//        判断name是否一样,这里的equals是String类的方法,与重写的这个不同
        return name != null ? name.equals(animal.name) : animal.name == null;
    }

}


//test类
public class Test2 {
    public static void main(String[] args) {
        Animal a=new Animal();
        a.name="小花";
        a.age=8;
        Animal c=new Animal();
        c.name="小花";
        c.age=8;
//        重写toString方法,然后用下面的方法就可以变成String类型的比较,就不需要重写equals方法了
//        System.out.println(a.toString().equals(c.toString()));
//        不重写equals方法比较的是地址
        System.out.println(a.equals(c));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值