class X
{
public String strX="hello";
}
class Y
{
public String strY= new String("hello");
}
class Z
{
public String strZ="hell"+"o";
}
public class TestString
{
public static void main(String[] args)
{
X x=new X();
Y y=new Y();
Z z=new Z();
System.out.println( x.strX==y.strY);
System.out.println( x.strX==z.strZ);
String s1="hel";
String s2="lo";
System.out.println( x.strX==(s1+s2));
System.out.println( x.strX==(s1+s2).intern());
}
}
输出:
false
true
false
true
{
public String strX="hello";
}
class Y
{
public String strY= new String("hello");
}
class Z
{
public String strZ="hell"+"o";
}
public class TestString
{
public static void main(String[] args)
{
X x=new X();
Y y=new Y();
Z z=new Z();
System.out.println( x.strX==y.strY);
System.out.println( x.strX==z.strZ);
String s1="hel";
String s2="lo";
System.out.println( x.strX==(s1+s2));
System.out.println( x.strX==(s1+s2).intern());
}
}
输出:
false
true
false
true
本文深入探讨了Java中不同字符串初始化方式对内存的影响,并通过实例展示了字符串比较的特性,包括直接引用比较和字符串拼接比较的区别。

被折叠的 条评论
为什么被折叠?



