/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}
main方法执行结果:
true
false
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}
main方法执行结果:
false
false
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
}
}
main方法执行结果:
true
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "a";
String s2 = "a";
System.out.println(s1.equals(s2)); //true
System.out.println(s1 == s2); //true
System.out.println(s1.hashCode() == s2.hashCode()); //true
String s3 = new String("a");
String s4 = new String("a");
System.out.println(s3.equals(s4)); //true
System.out.println(s3 == s4); //false
System.out.println(s3.hashCode() == s4.hashCode()); //true
String s5 = "a";
String s6 = new String("a");
System.out.println(s5.equals(s6)); //true
System.out.println(s5 == s6); //false
System.out.println(s5.hashCode() == s6.hashCode()); //true
String s7 = "a" + "b";
String s8 = "a" + "b";
System.out.println(s7.equals(s8)); //true
System.out.println(s7 == s8); //true
System.out.println(s7.hashCode() == s8.hashCode()); //true
String s9 = "ab";
String s10 = "a";
String s11 = s10 + "b";
System.out.println(s9.equals(s11)); //true
System.out.println(s9 == s11); //false
System.out.println(s9.hashCode() == s11.hashCode()); //true
System.out.println(s7.equals(s9)); //true
System.out.println(s7 == s9); //true
System.out.println(s7.hashCode() == s9.hashCode()); //true
System.out.println(s7.equals(s11)); //true
System.out.println(s7 == s11); //false
System.out.println(s7.hashCode() == s11.hashCode()); //true
String s12 = "abc";
char[] array = {'a', 'b', 'c'};
String s13 = new String(array);
s13 = s13.intern();
System.out.println(s12.equals(s13)); //true
System.out.println(s12 == s13); //true
System.out.println(s12.hashCode() == s13.hashCode()); //true
}
}
java中基本类型的包装类的大部分都实现了常量池技术,这些类是Byte,Short,Integer,Long,Character,Boolean,另外两种浮点数类型的包装类则没有实现。另外Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象。