发错了,呵呵,应该有区别吧,new关键字给对象分配了内存

解决方案 »

  1.   


    String s;

    String s = null;
    呢?
      

  2.   

    String s="a";
    String t="a";
    // s==t // true
    s = new String("a");
    t = new String("b");
    // s==t // false
    // s.equals(t) // true
      

  3.   

    为什么s.equals(t)是true 呢?
      

  4.   

    s==t比较的是两个对象是否相等
    s.equals(t)比较的是两个对象的内容是否相等
      

  5.   

    public class Test{
      public static void main(String[] args){
       String s = "a";
       String t = "a";
       String u = new String("a");
       String v = new String("a");
       if(s == t)System.out.println("s == t");
       if(u == v)System.out.println("u == v");
       if(u.equals(v))System.out.println("u.equals(v)");
       if(s == u)System.out.println("s == u");
      }
    }result:
    s == t
    u.equals(v)I think s and t are primitive type so they can ==:)
    but u and v are Object! so they couldn't == only equales()...
      

  6.   

    String s;
    这个s不也是个对象吗?是不是一定是通过new建立的才是对象呢?
      

  7.   

    something wrong:) String is just like primitive typein "core java2 volume I" P43:
    注意不要用==运算符来测试两个字串的相等性!它只能判断出两个字串是否保存在相同的内存位置。
    ....
    如果编译器总会安排相等的字串共享,就可用==测试其相等性,但字串的具体存储方式却要依情况而定。标准的实现方式只对字串常数进行共享...
    ...
    s == "a"
    t == "a"
    之所以相等是因为他们都指向了"a"的内存位置//当常数了
    而u 和 v则各自生成了一个对象,所以用==比较时他们的内存位置是不同的,故不能比较总之用equales总不会错:)
      

  8.   

    楼上的说的很清晰,不过能不能告诉我由
    String s;产生的s是不是一个对象呢?如何给他分配存储空间呢?
      

  9.   

    A:String s;
    B:String s = null;
    C:String s = new String();
    当然有区别,
    A,起了名字没有内容,就想你还没生儿子,但确已经起好了名字,呵呵:)
    B,起了名字,给了个空内容,你的儿子是null:P:P:P呵呵
    C,起了名字,也有了儿子,只是你的儿子“看不见”而已,呵呵,其内容是s == "",呵呵,再打个比喻把,你叫A呢,没人答应,你叫B呢,他不答应,你叫C呢,他答应了你可能听不到,呵呵:)
      

  10.   

    我的理解是String s;只是建立一个对象,但不分配空间,String s=new String();是创建对象的空间,l_walker(苦行僧) 的比喻很形象,但是有点并不准确,如果你创建了对象,怎么能说看不见呢,毕竟还是存在的,儿子没有生也可以有摇篮啊,难道等生了再去买,或者自己上山砍竹子?嘿嘿!
      

  11.   

    when initialize a Strint, 
    use String s = ""; 
    don't use String s = new String();
    because the constructor of String was called twice in the second case.
      

  12.   

    String S; 是String变量 
    String S = new String();是String对象
      

  13.   

    不好意思,笔误.
    String s = new String("a");
    String t = new String("a");
    s == t // false
    s.equals(t) // true
      

  14.   

    String s;只是声明了一个String型变量,没有指向任何对象,如果它是一个类的成员变量,它默认为null;
    String s=new String("a");将s指向了对象,该对象有内存空间,可以用s来使用String的各函数,如s.length();
      

  15.   

    String s;
    String s=null;
    这2者是一样的,因为java对于没有初始化的对象都是自动赋null值
      

  16.   

    一个是定义了string类型的变量,一个是定义了string类的实例。
      

  17.   

    一个是定义了string类型的变量,一个是定义了string类的实例。
    同意String S;
    S并没有实例化,刚学JAVA,如果有错,请误解。
    JAVA所有的实例都在堆上生成,这点完全不同于C或C++,因此你必须NEW一下才能有实例。
      

  18.   

    String s;//执行这条语句时,JVM分配了一个存放指向String的引用的空间,
             //并用s表示,而且系统默认值为null。s=new String();//这时,JVM在动态存储内存区分配一个空String对象的空间
                   //并取这个String对象的引用(当然也就是内存地址指针),
                   //给s赋值。(本质上s只是String类型的引用而已)以下是关于s==t的解答:public class Test{
      public static void main(String[] args){
       String s = "a";
       String t = "a";
       String u = new String("a");
       String v = new String("a");
       if(s == t)System.out.println("s == t");
       if(u == v)System.out.println("u == v");
       if(s.equals(t))System.out.println("s.equals(t)");
       if(u.equals(v))System.out.println("u.equals(v)");
       if(s == u)System.out.println("s == u");
      }
    }result:
    s == t     //因为"a"是一个常量,JVM分配存储空间是在静态数据区,
               //而且相同的常量只分配一次空间。因此s和t用来初始化的引用 
               //值是相同的。
    s.equals(t)//这句可以说明s和t也是String对象类型的引用。
    u.equals(v)//u!=v是因为用new创建时JVM分别为"a"分配了两个不同的动态
               //变量存储空间,因而用来初始化String引用u和v的内存地址
               //不同,因而u!=v。而它们指向的内存中存放的东西是一样的(a)总结:
    应该说JAVA是存在一种隐含基本数据类型的-----引用型。它可以用=(!=)来比较,当然在这种比较中,实质进行比较的不是引用指向的对象,而是引用本身而当调用引用指向对象的方法进行比较时,才真正地在比较对象是否相等。
    所以出现上面的结果是正常的。
      

  19.   

    关于这个问题,今天我在JAVA的官方网站的FORUM中看到下面的文章,这应该是非常准确的解释吧。
    9. When testing for equality between strings, what is the difference between using '==' operator and java.lang.String.equals(String)? The '==' operator compares two string references to see if they point to the same string object. The java.lang.String.equals() method checks if the objects' values are equal.That is, if the referent string objects have the same character sequence. In the case of string literals (a sequence of characters enclosed in double quotes) applying '==' operator and the equals method will yield the same results. All string literals reference an instance of class string. String literals are "interned" to allow the sharing of String instances. A pool of strings are maintained. When a new string literal is created, the pool is checked for a string instance with the same character sequence. If such an instance exists then a reference to that object is returned. Otherwise the string object is added to the pool, and a reference to that object is returned. 
    For example: String s1 = "hello"; String s2 = "hell"+"o"; 
      
      System.out.println("Using equals op"+ (s1==s2)); //True System.out.println("Using equals method" + (s1.equals(s2))); //True 
      
      This is not the case with strings that are created with the keyword, "new". 
      
      String s3 = new String("hello"); String s4 = new String("hello"); 
    System.out.println("Using equals op" + (s3==s4)); //False System.out.println("Using equals method" + (s3.equals(s4))); //True Using the new operator you will create two distinct objects and thus, two different references - although the underlying string literal is the same. With reference to the above code, the '==' operator returns false because the two references are different, while equals returns true, since both object values represent the same sequence of characters.