java.lang.Object类
1.Object类是所有Java类的根父类
2.如果在类的申明中未应用extends关键字指明其父类,则默认父类为java.lang.Object类
3.Object类中的性能(属性、办法)就具备通用性。
属性:无
办法:equals() / toString()
getClass()
hashCode()
clone() / finalize()
wait() 、 notify()、notifyAll()
4.Object类只申明了一个空参的结构器
Order order = new Order();
System.out.println(order.getClass().getSuperclass());
面试题:final、finally、finalize的区别?
面试题:
== 和 equals() 区别
\== :运算符(能够应用在根本数据类型变量和援用数据类型变量中)
如果比拟的是根本数据类型变量:比拟两个变量保留的数据是否相等。(不肯定类型要雷同)(主动类型晋升)
如果比拟的是援用数据类型变量:比拟两个对象的地址值是否雷同.即两个援用是否指向同一个对象实体
补充: == 符号应用时,必须保障符号左右两边的变量类型统一。
咱们对以上内容理论测试如下:
援用类型:
Customer cust1 = new Customer("Tom",21);
Customer cust2 = new Customer("Tom",21);
System.out.println(cust1 == cust2);//false
String str1 = new String("atguigu");
String str2 = new String("atguigu");
System.out.println(str1 == str2);//false
equals()办法的应用
1. 是一个办法,而非运算符,只能实用于援用数据类型。
2. Object类中equals()的定义:
public boolean equals(Object obj) {
return (this == obj);
}
阐明:Object类中定义的equals()和==的作用是雷同的:比拟两个对象的地址值是否雷同.即两个援用是否指向同一个对象实体
3.像String、Date、File、包装类等都重写了Object类中的equals()办法。
重写当前,比拟的不是两个援用的地址是否雷同,而是比拟两个对象的"实体内容"是否雷同。
4.通常状况下,咱们自定义的类如果应用equals()的话,也通常是比拟两个对象的"实体内容"是否雷同。那么,咱们就须要对Object类中
的equals()进行重写.
重写的准则:比拟两个对象的实体内容是否雷同.
\================================================================
equals()办法:
System.out.println(cust1.equals(cust2));//false--->true 多态无处不在(形参object,这里是其子类)
System.out.println(str1.equals(str2));//true 重写了Object类中的equals()办法
1、主动生成的equals() 目前曾经把握了------>生成构造方法、生成get()和set()办法、生成equals()和hash()
2、手动实现equals()的重写
@Override //主动生成的equals()
public boolean equals(Object obj) { //Object obj 多态无处不在
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
//正式比拟
Customer other = (Customer) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
重写的准则:比拟两个对象的实体内容(即:name和age)是否雷同
手动实现equals()的重写
@Override
public boolean equals(Object obj) {
// System.out.println("Customer equals()....重写的办法执行了");
if (this == obj) {
return true;
}
if(obj instanceof Customer){
Customer cust = (Customer)obj;
//比拟两个对象的每个属性是否都雷同
// if(this.age == cust.age && this.name.equals(cust.name)){
// return true;
// }else{
// return false;
// }
//或
return this.age == cust.age && this.name.equals(cust.name);
//根本数据类型age只能用== 援用数据类型name只能用equals()办法
}else{
return false;
}
}
Object类中toString()的应用
1、当咱们输入一个对象的援用时,实际上就是调用以后对象的toString()
Customer cust1 = new Customer("Tom",21); //com.atguigu.java1.Customer@15db9742
System.out.println(cust1.toString()); //com.atguigu.java1.Customer@15db9742
System.out.println(cust1);
2、Object类中toString()的定义:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
3、像String、Date、File、包装类等
都重写了Object类中的toString()办法。
使得在调用对象的toString()时,返回”实体内容”信息
4、自定义类也能够重写toString()办法,当调用此办法时,返回对象的”实体内容”
手动实现
@Override
public String toString() {
return "Customer[name = " + name + ",age = " + age + "]";
}
主动实现
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + "]";
}
Java中的JUnit单元测试
hamcrest-core-1.3.jar
下载43KB的jar包就能够
此单元测试办法上须要申明注解:@Test,并在单元测试类中导入:import org.junit.Test;
申明好单元测试办法当前,就能够在办法体内测试相干的代码。
1.如果执行后果没有任何异样:绿条
2.如果执行后果出现异常:红条
包装类(Wrapper)的应用
1、java提供了8种根本数据类型对应的包装类,使得根本数据类型的变量具备类的特色
总结:根本类型、包装类与String类间的转换
2、根本数据类型、包装类、String三者之间的互相转换
总结:(只看这个就能够了)
1、JDK 5.0 新个性:主动装箱 与主动拆箱
int num2 = 10;
Integer in1 = num2;//主动装箱
int num3 = in1;//主动拆箱
2、根本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);//"12.3"
3、String类型 --->根本数据类型、包装类:调用包装类的parseXxx(String s)
String str1 = "123";
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1); //124
======================================================================================================
根本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
@Test
public void test4(){
int num1 = 10;
//形式1:连贯运算
String str1 = num1 + "";
//形式2:调用String的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);//"12.3"
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2);
System.out.println(str3);//"12.4"
}
String类型 --->根本数据类型、包装类:调用包装类的parseXxx(String s)
@Test
public void test5(){
String str1 = "123";
//谬误的状况:
// int num1 = (int)str1;
// Integer in1 = (Integer)str1;
//可能会报NumberFormatException
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
JDK 5.0 新个性:主动装箱 与主动拆箱
@Test
public void test3(){
// int num1 = 10;
// //根本数据类型-->包装类的对象
// method(num1);
//主动装箱:根本数据类型 --->包装类
int num2 = 10;
Integer in1 = num2;//主动装箱
boolean b1 = true;
Boolean b2 = b1;//主动装箱
//主动拆箱:包装类--->根本数据类型
System.out.println(in1.toString());
int num3 = in1;//主动拆箱
}
public void method(Object obj){
System.out.println(obj);
}
根本数据类型 --->包装类:调用包装类的结构器
int num1 = 10;
// System.out.println(num1.toString()); error
Integer in1 = new Integer(num1);
System.out.println(in1.toString()); //10
Integer in2 = new Integer("123");
System.out.println(in2.toString()); //123
//报异样 NumberFormatException: For input string: "123abc"
// Integer in3 = new Integer("123abc");
// System.out.println(in3.toString());
//=======================================================
Float f1 = new Float(12.3f);
Float f2 = new Float("12.3");
System.out.println(f1); //12.3
System.out.println(f2); //12.3
//=======================================================
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("TrUe");
System.out.println(b2); //true
Boolean b3 = new Boolean("true123");
System.out.println(b3);//false 布尔类型特地一点:优化过
包装类--->根本数据类型:调用包装类Xxx的xxxValue()
@Test
public void test2(){
Integer in1 = new Integer(12);
int i1 = in1.intValue();
System.out.println(i1 + 1);
Float f1 = new Float(12.3);
float f2 = f1.floatValue();
System.out.println(f2 + 1);
}
面试题
@Test
public void test1() {
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);// 1.0
}
@Test //为什么??
public void test2() {
Object o2;
if (true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2);// 1
}
@Test
public void test3() {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false
Integer外部定义了IntegerCache构造(外部类),IntegerCache中定义了Integer[],
保留了从-128~127范畴的整数。如果咱们应用主动装箱的形式,给Integer赋值的范畴在-128~127范畴内时,
能够间接应用数组中的元素,不必再去new了。
目标:提高效率
@Test
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true
Integer x = 128;//相当于new了一个Integer对象
Integer y = 128;//相当于new了一个Integer对象
System.out.println(x == y);//false
}
发表回复