1、 int 和Integer的区别
a、int是java的基本数据类型;Integer是int提供的封装类;
b、int默认值是0;Integer默认是NULL;
c、int直接储存数据;Integer是对象,需要用一个引用指向对象;
d、声明int的变量不需要实例化,而声明Integer的变量需要实例化。
2、相等运算符与引用类型的变量区别
例1: public static void main (string [] args) {
int i=38;
int j=66;
System.out.print("i=j"); //false
System.out.print("i!=j"); //true
} //判断基本类型i和j的取值是否相等;
例2:public static void main (string [] args) {
string s1="abcd";
string s2="abcd";
string s3=new string("abcd");
string s4=new string("abcd");
System.out.printIn("s1=s2"); //true 同一字符串
System.out.printIn("s1=s3"); //false 可能是相同的字符串,但不是相同的对象
System.out.printIn("s3=s4"); //false 可能是相同的字符串,但不是相同的对象
System.out.printIn("s1.equals(s3)"); //true 不同的对象,相同的内容
System.out.printIn("s4.equals(s3)"); //true 不同的对象,相同的内容
}
//判断对象以及内容是否相等;
例3:int i=8;
int j=8;
Integer s3=new Integer("8");
Integer s4=new Integer("8");
System.out.printIn("i==j"); //true 同一数字
System.out.printIn("s3.equals(i)"); //true 整数对象的值与整数相同
System.out.printIn("s3==s4"); //false 可能是相同的整数值,但不是相同的对象
System.out.printIn("s4.equals(s3)"); //true 不同的整数对象,相同的内容
总结:
1、基本类型的变量名是变量本身,只要俩个变量的值相同使用运算符==的取值就为真;
2、当变量与对象的值使用运算符==时,取值为假;
3、对象与对象的值使用运算符==时,取值为假;
4、对象是否包含变量使用s3.equals(i)时,取值为真;
5、对象是否包含对象使用s3.equals(i)时,取值为假。