JAVA 常用API Math System Object equals ==

Math

常量有两个 
	E=2.~
	PI=3.1415926
没有构造方法,都是静态方法,常量	。可以直接通过类名调用

在这里插入图片描述
随机数可以控制在一定范围之间的随机数,[x,y)

x+(int)(Math.random()*(y-x))

System类

都是静态的

在这里插入图片描述

        System.out.println("开始");
        System.exit(0);
        System.out.println("结束");//虚拟机已经退出,不会输出结果
//计时用
        long time = System.currentTimeMillis();
        for (int i = 0; i <10000 ; i++) {
            System.out.println(i);
        }
        long end =System.currentTimeMillis();
        System.out.println(end-time);

Object类

只有一个无参构造方法,Object()
常用方法
	toString
	equals

在这里插入图片描述

	Student s = new Student();
	s.setAge(12);
	s.setName("xiaoli");
	//对象打印直接是类名@地址值,建议重写toString方法
	System.out.println(s);
	System.out.println(s.toString());

equals方法的重写

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

equals
有两个相似的就是== 与equals

==直接比较的是地址
equals对于字符串比较的是内容
对象类引用变量比较的是地址
因此需要重写equals方法,使其比较内容

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值