[size=medium][/size][size=large][/size]1.== 用来比较两个变代表的地址是否相同;(地址)
基本类型和类类型都能使用==比较,,,比较地址
2.equals Object类中定义的方法,判断两个对象是不是“相等”;
在Integer中,类型相同,值相同,就相等;
//自动拆箱(包)(只有8个基本类型才会拆箱):当基本类型和类类型进行计算时,将封装的基本类型数据取出(int和 Integer)
//两个都是类类型,不拆箱比较
equals默认比较的是地址,各个类可以重写equals方法,自行定义方法;(重写,参数不能改;)——
3.equalsIgnoreCase
是string类中定义的方法,用来比较两个字符串中对应的字符是否相等,忽略大小写(验证码,搜索时,有的不区分大小写)
1.2.3都是用来比较两个值是否相等[color=green][/color]
public class D {
public static void main(String[] args) {
//基本类型都对应一个类类型
//类类型里面封装了一个基本类型的
// class Integer{
// int a;}
int a1 = 10;
int a2 = 10;
Integer a3 = 10;
Integer a4 = 10;
Integer a5 = new Integer(11);
Integer a6 = new Integer(10);
/*1. == 用来比较两个变代表的地址是否相同;(地址)
基本类型和类类型都能使用==比较,,,比较地址
* */
System.out.println(a1 == a2);//true
System.out.println(a1 == a3);//true
System.out.println(a3 == a4);//true
//自动拆箱(包):当基本类型和类类型进行计算时,
将封装的基本类型数 据 取 出(int和 Integer)
//自动拆箱[拆包]:当基本类型和类类型进行计算的时候,
//会自动将封装的基本类型数据取出进行计算
System.out.println(a1==a5);//true
//如果两个都是类类型,不会拆箱
System.out.println(a5==a6);//false
//如果两个都是类类型,不会拆箱
System.out.println(a3==a5);//false
//在Integer中,类型相同,并且值相同,就相等
System.out.println(a5.equals(a6));//true
System.out.println(a3.equals(a5));//true
//自定义的类,重写equals方法,想怎么比较就怎么写
//这里定义必须为MyInteger类型相同,并且值相同
MyInteger a7=new MyInteger(10);
MyInteger a8=new MyInteger(10);
System.out.println(a5.equals(a7));//false
System.out.println(a7.equals(a8));//true
//
String s1="abc";
String s2="ABC";
String s3="abc";
System.out.println(s1.equals(s3));//true String里面的equals方法不区分大小写
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
/*equalsIgnoreCase
是string类中定义的方法,用来比较两个字符串中对应的字符是否相等,忽略大小写(验证码,搜索时,有的不区分大小写)
*/
}
}
基本类型和类类型都能使用==比较,,,比较地址
2.equals Object类中定义的方法,判断两个对象是不是“相等”;
在Integer中,类型相同,值相同,就相等;
//自动拆箱(包)(只有8个基本类型才会拆箱):当基本类型和类类型进行计算时,将封装的基本类型数据取出(int和 Integer)
//两个都是类类型,不拆箱比较
equals默认比较的是地址,各个类可以重写equals方法,自行定义方法;(重写,参数不能改;)——
3.equalsIgnoreCase
是string类中定义的方法,用来比较两个字符串中对应的字符是否相等,忽略大小写(验证码,搜索时,有的不区分大小写)
1.2.3都是用来比较两个值是否相等[color=green][/color]
public class D {
public static void main(String[] args) {
//基本类型都对应一个类类型
//类类型里面封装了一个基本类型的
// class Integer{
// int a;}
int a1 = 10;
int a2 = 10;
Integer a3 = 10;
Integer a4 = 10;
Integer a5 = new Integer(11);
Integer a6 = new Integer(10);
/*1. == 用来比较两个变代表的地址是否相同;(地址)
基本类型和类类型都能使用==比较,,,比较地址
* */
System.out.println(a1 == a2);//true
System.out.println(a1 == a3);//true
System.out.println(a3 == a4);//true
//自动拆箱(包):当基本类型和类类型进行计算时,
将封装的基本类型数 据 取 出(int和 Integer)
//自动拆箱[拆包]:当基本类型和类类型进行计算的时候,
//会自动将封装的基本类型数据取出进行计算
System.out.println(a1==a5);//true
//如果两个都是类类型,不会拆箱
System.out.println(a5==a6);//false
//如果两个都是类类型,不会拆箱
System.out.println(a3==a5);//false
//在Integer中,类型相同,并且值相同,就相等
System.out.println(a5.equals(a6));//true
System.out.println(a3.equals(a5));//true
//自定义的类,重写equals方法,想怎么比较就怎么写
//这里定义必须为MyInteger类型相同,并且值相同
MyInteger a7=new MyInteger(10);
MyInteger a8=new MyInteger(10);
System.out.println(a5.equals(a7));//false
System.out.println(a7.equals(a8));//true
//
String s1="abc";
String s2="ABC";
String s3="abc";
System.out.println(s1.equals(s3));//true String里面的equals方法不区分大小写
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
/*equalsIgnoreCase
是string类中定义的方法,用来比较两个字符串中对应的字符是否相等,忽略大小写(验证码,搜索时,有的不区分大小写)
*/
}
}