/***********************************************************/
public class StringVSChar {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
char[] ch = { 'a', 'b', 'c' };
String s3 = new String(ch);
String s4=new String("abc");
char[] c = s1.toCharArray();
if (s1.equals(s2)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (c.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(s3)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(s4)) {
System.out.println(true);
}else {
System.out.println(false);
}
if (s1==s4) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1==s3) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
/***********************************************************/
结果:
true
false
false
true
true
false
false
/***********************************************************/
从结果可以看出要弄清几点:
1.String类型可以和char[]进行相互转换,但两种是不同的类型;
2.弄清楚创建的对象在内存中是处于栈还是堆;
3.String中重写了equal(),比较的是两个对象的内容是否一样,如果不重写,和==一样都是比较的对象的引用,new出来的对象都是放在堆中的。
/***********************************************************/
public class IntVSInteger {
public static void main(String[] args) {
Integer i1 = -18;
Integer i2 = -18;
Integer i3=new Integer(-18);
Integer i4=new Integer(-18);
if (i1 == i2) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i1.equals(i2)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i3==i4) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i3.equals(i4)) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
/***********************************************************/
结果:
true
true
false
true
/***********************************************************/
经调试,在Java中,integer的-128~127是系统预留在栈中的共享数据,这个范围之外的赋值,用==比较就是false.
public class StringVSChar {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
char[] ch = { 'a', 'b', 'c' };
String s3 = new String(ch);
String s4=new String("abc");
char[] c = s1.toCharArray();
if (s1.equals(s2)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (c.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(s3)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(s4)) {
System.out.println(true);
}else {
System.out.println(false);
}
if (s1==s4) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1==s3) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
/***********************************************************/
结果:
true
false
false
true
true
false
false
/***********************************************************/
从结果可以看出要弄清几点:
1.String类型可以和char[]进行相互转换,但两种是不同的类型;
2.弄清楚创建的对象在内存中是处于栈还是堆;
3.String中重写了equal(),比较的是两个对象的内容是否一样,如果不重写,和==一样都是比较的对象的引用,new出来的对象都是放在堆中的。
/***********************************************************/
public class IntVSInteger {
public static void main(String[] args) {
Integer i1 = -18;
Integer i2 = -18;
Integer i3=new Integer(-18);
Integer i4=new Integer(-18);
if (i1 == i2) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i1.equals(i2)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i3==i4) {
System.out.println(true);
} else {
System.out.println(false);
}
if (i3.equals(i4)) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
/***********************************************************/
结果:
true
true
false
true
/***********************************************************/
经调试,在Java中,integer的-128~127是系统预留在栈中的共享数据,这个范围之外的赋值,用==比较就是false.