代码
package test;
public class XIandeng {
public static void main3(String[] args) {
int c = 2;
System.out.println(c);//2
System.out.println(++c);//3
System.out.println(c++);//3
System.out.println(c);//4
}
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = new Integer(20);
Integer i3 = 20;
Integer i4 = 128;
Integer i5 = new Integer(128);
Integer i6 = 128;
System.out.println(i1==i2);//false
System.out.println(i1==i3);//ture
System.out.println(i2==i3);//false
System.out.println(i4==i5);//false
System.out.println(i4==i6);//false
System.out.println(i5==i6);//false
}
public static void main1(String[] args) {
String s1 = "helloworld";
String s2 = new String("helloworld");
String s3 = "helloworld";
String s4 = "hello"+"world";
System.out.println(s1==s2);//false;
System.out.println(s1==s3);//true;
System.out.println(s1==s4);//true;
System.out.println(s2==s3);//false;
System.out.println(s2==s4);//false;
System.out.println(s3==s4);//true;
System.out.println(s1==s2.intern());//true;
}
}
个人理解:
全部都是只有new对象,==,判断时一定不相等
1.Integer类型是int类型的包装类,当值在 -128 ------- 127 时不会new Integer对象 超出这一个范围每一次都会new新的对象所以不会相等
2.String类型:每个string类型的值都会保存到常量池中,所以只要后面的String类型不是通过new产生对象的都会去常量池中找有没有该字符,如果有直接引用该字符串在堆中的地址值
3.++在前------>表示先加后用.
++在后--------->表示先用后加.