1.Object
1.1Object类概述
- Object是类层次结构的根类
- 每个类都使用Object作为超类
- 所有对象包括数组都实现这个类的方法
- 只有一个空参构造器
- 默认每一个方法中都有一个super();用来接受这个构造方法
1.2API文档
Object常用方法可以通过API中去查询,具体位置是lang包下的object类
URL:JAVA API1.8
1.3Equals()
重写前比较地址值的(开发基本不用)
重写后比较实体内容(属性)(开发要这么用)
开发不需要手写equals但是要会写
可以使用IDEA的快捷键直接生成equals重写
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public boolean equals(Object obj){ if (this == obj){ return true; }
if (obj instanceof StudentList) { StudentList slt = (StudentList) obj; return this.age == age && this.name.equals(slt.name); } return false; }
|
测试类比较
1 2 3 4 5 6 7 8 9 10 11 12 13
|
boolean isEquals = studentList1.equals(studentList2); System.out.println("studentlist类型比较equals:"+isEquals);
System.out.println("studentlist类型比较==:"+(studentList1 == studentList2));
int a = 10; int b = 20; System.out.println("int类型比较:"+(a == b));
|
1.4toString()
重写前,调用返回的是地址值
重写后,更直观的显示实体内容属性
开发不需要手写toString但是要会写
可以使用IDEA的快捷键直接生成toString重写
重写方法
1 2 3 4 5 6
| @Override public String toString() { return "StudentList{" + "name='" + name + '\'' + ", age=" + age + '}'; }
|
测试类比较
1 2 3 4 5
| StudentLists=newStudentList("小白",3);
System.out.println(s); System.out.println(s.toString());
|
1.5getClass()
具体体现在反射
返回此Object的运行类
可以通过Class类中的一个方法,获取对象真实类的全名称
Code
1 2 3 4 5
| StudentList studentList =new StudentList("xiaohuia",13);
System.out.println(studentList.getClass());
System.out.println(studentList.getClass().getSuperclass());
|
1.6hashCode()
具体使用在反射部分讲解
Code
1 2 3 4
| Object ob=new Object(); int hashCode=ob.hashCode(); System.out.println(hashCode); 返回该对象的哈希码值 类型为int类型
|
1.7==和Equals()的区别
经典面试题
共同点
都可以做比较
返回值类型都是boolean
不同点
==是运算符,基本数据类型/比较引用数据类型都可以比较
基本数据类型比较的是数据值(类型可以不相同/除boolean)
引用数据类型是地址值,即两个对象是否指向同一个对象实体
equals是方法,只能比较引用数据类型(底层依赖是==)
重写前,比较的是地址值<—>重写后,比较对象中的实体内容(属性)
(后面的常用类基本上都重写了equlas方法,例如String、Date等)
注:比较地址值是没有意义的
2.包装类
2.1包装类概述
- 针对基本数据类型定义的相对应引用数据类型
- int写为Integer char写为Character 其他六种首字母大写(父类为Number)
- 有了类的特点,就可以调用类的方法,才是真正意义的面向对象
- 核心点就是基本数据类型、包装类、String三者的转换
2.2类型转换
2.2.1基本数据类型—>包装类
- 调用包装类构造器传参即可
- 如果传的是String就要保持是这个类型的格式
- Boolean对象的形参只要不是true,返回的就是false
- 包装类在不赋值默认输出为null(因为它是引用数据类型)
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Integer i1 = new Integer(300); System.out.println(i1.toString());
Integer i2 = new Integer("300"); System.out.println(i2.toString());
Boolean b1 = new Boolean("trdue"); Boolean b2 = new Boolean("false"); System.out.println(b1); System.out.println(b2);
oreder or = new oreder();
System.out.println(or.isFlag);
System.out.println(or.isFLAG);
|
2.2.2包装类—>基本数据类型
1 2 3 4 5 6 7 8 9
| @Test public void revereTest(){
Float ft = new Float(13.2);
float ft2 = ft.floatValue();
System.out.println(ft2 + 26.9); }
|
2.3自动装箱 自动拆箱
- JDK的新特性,1.5版本以后,取代了上面的两种方法
自动装箱
- 不需要去new一个对象
- 直接 包装类型 类型 = 基本数据类型名称 就可以了
- 相当于之前的转换方法被替代掉了
自动拆箱
- 把包装类直接赋值给基本数据类型即可(除String)
Code
1 2 3 4 5 6 7 8 9 10 11 12
| int num1 = 1000; Interger it0 = num1;
Integer it1 = new Integer(300);
System.out.println(it1.toString());
int num2 = it1; System.out.println(num2 + 500);
|
2.4与String的转换
基本数据类型/包装类—–>String
直接通过String的valueOf(xxx)重载方法
String—–>基本数据类型/包装类
直接通过包装类的parseXXX()方法
Code
1 2 3 4 5 6 7 8 9 10 11
| @Test public void testReverseString(){ String s1 = "123.456232"; float f1 = Float.parseFloat(s1); System.out.println(f1);
String s2 = "TRUE"; boolean b1 = Boolean.parseBoolean(s2); System.out.println(b1); }
|
2.5面试题
Code
1 2 3 4 5 6 7 8 9 10 11
| @Test public void testReverseString(){ String s1 = "123.456232"; float f1 = Float.parseFloat(s1); System.out.println(f1);
String s2 = "TRUE"; boolean b1 = Boolean.parseBoolean(s2); System.out.println(b1); }
|
解释
- 返回为false具体看Integer源码的IntegerCache结构的的Integer[]
- Integer[]保存了-128~127的整数
- 在此范围内,直接使用此数字元素,无需再new,提高开发效率
判断输出
1 2 3 4 5 6 7 8 9 10 11
| @Test public void interviewTest(){ Integer i1 = new Integer(1); Integer i2 = new Integer(1); System.out.println(i1 == i2);
Integer a = 2; Integer b = 2; System.out.println(a == b);
|