equals()
== 和 equals()区别
一、回顾 == 的使用:
== :运算符
-
可以使用在基本数据类型变量和引用数据类型变量中
-
如果比较的是基本数据类型变量,比较两个变量保存的数据是否相等(不一定类型相等)
如果比较的是引用数据类型,比较两个对象的地址是否相等,即两个引用是否指向同一个对象实体
补充:== 符号使用时,必须保证符号左右两边变量类型一致
二、equals()方法的使用:
-
是一个方法,而非运算符
-
只能适用于引用数据类型
-
Object类中equals()的定义:
public boolean equals(Object obj){ return (this == obj); }说明:Object类中定义的equals()和==的作用相同,比较两个对象的地址值是否相同,即两个引用是否指向同一个对象实体
-
像String、Date、FIle、包装类等都重写了Object类中的equals()方法,重写以后,比较的不是两个引用的地址是否相同,而是比较两个对象“实体内容”是否相同
-
通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的“实体内容”是否相等,那么,我们就需要对Object类中的equals()进行重写。
toString()
Object类中toString()的使用
-
当我们输出一个对象的引用时,实际上就是调用当前对象的toString
-
object类中toString()的定义:
public String toString(){
return getClass( ).getName( ) +“@”+ Integer.toHexString(hashCode());
}
- 像String、 Date、File、包装类等都重写了0bject类中的toString()方法。使得在调用对象的toString()时,返回"实体内容"信息。
单元测试方法
创建Java类,进行单元测试:
-
此时的Java类要求:① 此类是public的 ② 此类提供公共的无参的构造器
-
此类中声明单元测试方法。
此时的单元测试方法:方法权限是public,没有返回值(或者为void),没有形参
-
此单元测试方法上需要声明注释:@Test,并在单元测试类中导入:import org.junit.Test;
-
声明好单元测试方法以后,就可以在方法体内测试相关代码。
说明:
- 如果执行结果没有任何异常:绿条
- 如果执行结果出现异常:红条
包装类:
import org.testng.annotations.Test;
public class Test009 {
/**基本数据类型--->包装类:调用包装类的构造器*/
@Test
public void test1(){
int num1 = 10;
Integer int1 = new Integer(num1);
System.out.println(int1.toString());
System.out.println("*****************************");
Integer int2 = new Integer("1234");
System.out.println(int2);
}
@Test
public void test2(){
Float f1 = new Float(12.3f);
Float f2 = new Float("12.34");
System.out.println(f1);
System.out.println(f2);
}
@Test
public void test3(){
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("true132");//false
Order order =new Order();
System.out.println(order.isMale);//false 基本数据类型
System.out.println(order.isFemale);//null 包装类
}
//包装类--->基本数据类型:调用包装类Xxx的xxxValue()
@Test
public void test4(){
Integer int1 = new Integer("1234");
int i1 = int1.intValue();
System.out.println(++i1);
System.out.println("*****************");
int1 = new Integer(i1);
System.out.println(int1);
}
//JDK 5.0新特性:自动装箱与自动拆箱
@Test
public void test5(){
//自动装箱:基本数据类型--->包装类
int num = 10;
Integer int1 = num;
// 自动拆箱:包装类--->基本数据类型
int num2 = 20;
num2= int1;
}
//基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
@Test
public void test6(){
int num1 = 10;
//方式一、连接运算
String str1 = num1 + "";
//方式二、调用String重载的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);
Double double1 = new Double(13.14);
String str3 = String.valueOf(double1);
System.out.println(str2);
System.out.println(str3);
}
//String类型--->基本数据类型、包装类:调用包装类的parseXxx()
@Test
public void test7(){
String str1 = "123";
int num = Integer.parseInt(str1);
System.out.println(num);
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
}
class Order{
boolean isMale;
Boolean isFemale;
}
注:Integer内部定义了IntegerCache结构,IntegerCache中定义了Inter[ ],保存了从-128~127范围内的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128到127范围内时,可以直接使用数组中的元素,不用new了,目的是为了提高效率
public class Main{
@Test
public void test(){
Integer i = new Integer(1);
Integer j = new Integer(j);
System.out.println(i == j);//false
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true
Integer x = 128; //相当于new了一个Integer对象
Integer y = 128;
System.out.println(x == y);//false
}
}