如果不行的话,如何解释:
class test 
{
public static void main(String[] args) 
{
String a = "abc";
String b = "def";
System.out.println(a==b); String c = "abc";
String d = "abc";
System.out.println(c==d);
}
}
结果是
true
false

解决方案 »

  1.   

    结果反了吧
    应该是:
    false
    true
    在java中没有像c++中的字符重载
    在java中"=="符号如果用于比较引用,比较的是两个引用的指向的地址是否一致而不是内容是否一致
    ,equals()函数比较内容是否一致
      

  2.   

    String a = "abc";
    String b = "def";
    String c = "abc";
    String d = "abc";这四句创建了2个字符串常量,"abc"和"def",变量a、c、d分别引用"abc",变量b引用"def"。
    故"a==c"、"a==d"、"c==d"都是true
      

  3.   

    to: zxbjlu1983(晚风长街) The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
      

  4.   

    string a = "A";
    string b = "B";
    a+b;// "+"
    int A = 0;
    int B = 1;
    A+B;// "+"加号不是重载吗?
      

  5.   

    字符串的“+”运算仅仅是为了方便写代码,编译器去完成转换而已String a,b;a = a + b; 实际上会被编译器变成 
    StringBuffer tmp = new StringBuffer();
    tmp.append(a);
    tmp.append(b);
    a = tmp.toString();
      

  6.   

    帮上面补充一下多个String相加,如果每一个部分在编译期就已经可以确定其值的话,那么在编译的时候只会生成一个String而没有中间的那些Buffer过程(从反编译回来的代码中可以得到验证),所以在有些情况中,+反而比StringBuffer效率高
      

  7.   

    但是加号在连接字符串时会每连一次新增一个对象,也就是要开辟一个新的内存地址,而StringBuffer是不会的,你怎么能说加号比StringBuffer效率高呢?
      

  8.   

    To SaKura2003(小飞仔),请注意我说的“如果每一个部分在编译期就已经可以确定其值的话,那么在编译的时候只会生成一个String”, 这有点类似:void f() {
      if (true) return;
      doSomething(); //编译错误,因为这段代码在“编译期”就已经能被确定永远不会被执行
    }只要有一个String不能在编译期确定其值的话(比如是一个参数,或者其他方法的返回值,即使这个参数的值或者函数返回值永远固定),那么还是如上所讲,使用StringBufferpackage test;public class Test {
      private static final String a = "a" + "a" + "a" + "aa";
      private static final  String b = "b" ;
      private static final String c = "c";
      private static final   String d = a + b + c;  public static void main(String[] args) { }}
    反编译结果
    package test;public class Test {  // Fields
      private static final String _$258 = "aaaaa";
      private static final String _$259 = "b";
      private static final String _$260 = "c";
      private static final String _$261 = "aaaaabc";  // Constructors
      public Test() { }  // Methods
      public static void main(String[] args) { }
    }
      

  9.   

    那么是不是程序中所有的"abc"全部都指向同一个String对象呢?只要不new?为什么new一下就会false呢?还有那个String对象是几个?一个还是多个呢?如果是一个那么应该怎么比都相同呢.还有用==比字符串是否相同是不是很不保险?而应该用equals()?
      

  10.   

    因为你用了new只后会产生一个新的String对象,它与原来的对象的内存地址是不同的,所以==的比较结果就是false了。这时候就应该用equals()方法,而不应该用==进行判断。
      

  11.   

    字符比较不要使用"==",这是肯定的.
    因为String是使用一种特殊对象处理的.String c = "abc";
    String d = "abc";
    是相同的字符,c、d是指向同一个内存地址。所以"=="返回是true;
    当执行c = c+ "a";的时候,c又被重新分配了地址,再执行c==d则返回false;
      

  12.   

    class test 
    {
    public static void main(String[] args) 
    {
    String a = "abc";
    String b = "abc";
    System.out.println(a+" "+b+" "+(a==b)); b = b + "d" ;
    System.out.println(a+" "+b+" "+(a==b)); b = b.substring(0,3);
    System.out.println(a+" "+b+" "+(a==b));
    }
    }
    结果是:
    abc abc true
    abc abcd false
    abc abc false
    证明了楼上的观点。会不会是编译器作的事情呢?在编译的时候将所有相同的字符串都用一个对象来存储,
    而在运行时就不会再检查相同的字符串是否使用同一个对象了,对吧?
      

  13.   

    不对,他们之所以==,是因为我上面反复强调的“编译期”就已经确定了的,在“运行期”得到的,两者并不==同样都是abc,一个是String a = "abc";另外一个是从数据库或者文件或者参数中读出来,两者equals却!=