Java基础-Object类&包装类

常用类-Object类

  • 凡是 java.lang 这个包下面的类都不需要导入
  • Object类是所有类的根基类,在java.lang下面

1.Object常用方法

1.1equals方法

比较 是否为同一个对象也就是同一个引用

  • 自反性 x.equasl(x)=true
  • 对称性 x.equals(y) 和y.equals(x)返回结果一致
  • 传递性 x.equals(y)为true y.equals(z)为true,那么x.equals(z)也为true
  • 一致性 ,每次调用结果应该要一致
  • x.equals(null)} should return {@code false}. 和null比较结果为false
 public static void main(String[] args) {
        Student stu1 = new Student();
        Student stu2 = new Student();
        //Object 的equals方法比较是否是同一个对象 和==结果是一致的;
        System.out.println(stu1.equals(stu2));
        System.out.println(stu1 == stu2);

        // String 类重写了equals方法,比较的是字符串内容是否相等
        String str1 = "abc";
        String str2 = new String("abc");
        System.out.println(str1.equals(str2));
        System.out.println(str1 == str2);
    }

1.2.hashCode方法

hash值可以唯一 的代表一个对象,不同的对象hash值应该是不一样的;

  • 一致性,多次调用返回相同的结果
  • 如果两个对象equals,那么hash值要相同
  • 如果两个对象不equals,应该要返回不同的hash值(有特殊情况在,就是会返回相同的hash值)
  • 重写equals方法的时候一定要重写hashCode方法
//本地方法,代码实现代码不是Java语言,是由C语言实现的
public native int hashCode();
public static void main(String[] args) {
        Student stu1 = new Student();
        Student stu2 = stu1;
        String str1 = "Aa";
        String str2 = "BB";
        System.out.println(str1 == str2);
    //特殊情况,两个对象不相等但是hash值相等
        System.out.println(str1.hashCode());
        System.out.println(str2.hashCode());
    }

Student类

public class Student {
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        //判断 obj是否是Student类型的对象(可以是Student的子类型),如果是返回结果为true
        if (obj instanceof Student) {
            return name.equals(((Student) obj).getName());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.3.toString()方法

返回的是代表这个对象的字符串,String类内部也已经进行了重写

 //等同的
        System.out.println(javaStudent1);
        System.out.println(javaStudent1.toString());
        String str = "hello";
        System.out.println(str.toString());

1.4.clone方法

克隆,表示生成一个新的对象

public class Test implements Cloneable {
    //当成员变量是基本数据类型,改动源对象的成员变量值不会影响克隆对象的成员变量值
    private int a;

    public static void main(String[] args) {
        Test test = new Test();
        test.a = 10;
        try {
            Object clone = test.clone();
            System.out.println("是否是同一个对象:" + (clone == test));
            System.out.println("是否是Test类型的:" + (clone instanceof Test));
            test.a = 20;
            if (clone instanceof Test) {
                //打印出的值还是10
                System.out.println(((Test) clone).a);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
public class Test implements Cloneable {
    private Student student;
	//当成员变量是引用类型,clone方法其实是浅克隆
	//如果有引用类型的成员变量,源对象和克隆对象是同一个
    public static void main(String[] args) {
        Test test = new Test();
        test.student = new Student("tom");
        try {
            Object clone = test.clone();
            System.out.println("是否是同一个对象:" + (clone == test));
            System.out.println("是否是Test类型的:" + (clone instanceof Test));
            test.student.setName("jerry");
            if (clone instanceof Test) {
                System.out.println(((Test) clone).student.getName());
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

1.5.finalize()方法

这是GC会调用的方法,当对象要被回收的时候会主动调用

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("student对象要被回收了");
    }

包装类

java种万事万物皆对象,提供的包装类就是对基本数据类型的封装

包装类丰富了对基本数据类型的操作,因为提供了较多的方法和成员变量

int的包装类是Integer ,char的包装类是Character,其余都是首字母大写

包装类型转换为对应的基本数据类型 拆箱

基本数据类型自动转换为包装类型 装箱

1.1.Integer类常用方法

public static void main(String[] args) {
        Integer integer = 10;
        //int的最大值和最小值
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);
        //把数字的字符串转为对应的int 值
        int parse = Integer.parseInt("123");
        System.out.println(parse);
        // //把数字的字符串转为对应的integer值
        Integer integer1 = Integer.valueOf("123");
        Integer integer2 = new Integer(123);
        double d1 = integer1.doubleValue();
        System.out.println("对应的double值是:" + d1);
        System.out.println("toString方法:" + integer1.toString());
        System.out.println("是否相等:" + integer1.equals(integer2));
        // -1 0 1
        Integer.compare(10, 20);
        Integer.max(10, 20);
    }

1.2.缓存池中的内容

  • byte -128-127
  • short -128-127
  • int -128-127
  • 浮点型的值没有在内
  • boolean 值都在内 true ,false
  • char 所有的字符都在内
 public static void main(String[] args) {
        Integer in1 = 128;
        Integer in2 = 128;
        Integer in3 = new Integer(12);
        Integer in4 = new Integer(12);
        Float f1 = 12.8f;
        Float f2 = 12.8f;
        Character c1 = 'a';
        Character c2 = 'a';
        System.out.println(in1 == in2);
        System.out.println(in4 == in3);
        System.out.println(f1 == f2);
        System.out.println(c1==c2);
    }

1.3.Character包装类

 public static void main(String[] args) {
        boolean a = Character.isDigit('1');
        System.out.println(Character.isLetter('a'));//判断字母
        System.out.println(Character.isUpperCase('A'));//判断大写
        System.out.println(Character.isLowerCase('a'));//判断小写
//        System.out.println(a);
    }

Math类

  • 全都都是和数学和几何运算相关的方法,全都是静态方法
public static void main(String[] args) {
        System.out.println("最大值:" + Math.max(10, 20));
        System.out.println("最大值:" + Math.min(10, 20));
        System.out.println("次方值:" + Math.pow(2, 4));
        System.out.println("求根:开平发" + Math.sqrt(16));
        System.out.println("四舍五入值:" + Math.round(4.2));
        System.out.println("绝对值:" + Math.abs(-23));
        System.out.println("向上取最小整数" + Math.ceil(3.4));
        System.out.println("向下取最大整数" + Math.floor(3.4));
        System.out.println("得到[0-1)直接的值," + Math.random());
    }

Objects类

  • 该类优雅的处理了当调用Object的方法出现空指针的情况
  public static void main(String[] args) {
        System.out.println("两个对象是否相等:" + Objects.equals(null, "java"));
        System.out.println("判断对象是否为null:" + Objects.isNull("null"));
        // requireNonNull如果参数为null,直接抛异常出来
//        Objects.requireNonNull(null,"参数不能为null");
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {1, 2, 3};
        System.out.println(Objects.equals(arr1, arr2));
        // deepEquals比较的是数组元素是否一一相等
        System.out.println(Objects.deepEquals(arr1, arr2));
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值