试了一下,那样不行,应该这样才行
                  String strMsg2=new String("test1").intern();
                  String strMsg1="test1";

if(strMsg1==strMsg2)
{
System.out.println("==");
}
else
{
System.out.println("!=");
}
intern()方法大概就是你说的这个意思

解决方案 »

  1.   

    还是不行呀。还是运行错误
    Exception in thread "main" java.lang.NoSuchMethodError: main
    :)一运运intern方法就这样55这是怎么回事呀。
      

  2.   

    public class test { public test() {
    }
    public static void main(String[] args) {
    String a1 = new String("aaaa").intern();
    String a2 = new String("aaaa");
    if(a1 == a2)
    {
    System.out.println("==");
    }else if(a1.equals(a2))
    {
    System.out.println("equal");
    }
    else{
    System.out.println("!=");
    }
    }
    }
    输出equal
      

  3.   

    new String("test1")和"test1"可不一样
      

  4.   

    我知道不一样,但是intern的意思不就是在地址池中保留字符对象引用吗??所以我认为第二次new的时候这两个引用应该相等呀??我是想测试这个结论。这是没成功,是不是我对inten方法的理解不对呀??
      

  5.   

    main前面的限定符是:public static void 
      

  6.   

    这个问题已经看到了。但是用intern方法后为什么还是不能直接比较引用呢??