×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / Help: the following doesn't compile, why?
    new String("Hello")==new String("Hello");
    • == means shallow comparison
      In Java, one should exactly distingish Objects and their references. Take the following as an example:

      Animal a = new Bird();

      This means 3 steps: 1) Declare a variable with a type of Animal; 2) Create a Bird (Animal) according to the shape of a bird, using key word "new"; 3) Assign this Bird(Animal) to variable a. "a" is a kind of reference.

      In Java, to judge where two Object references point to the same Objects or not,
      we use the operator ==. This is called "shallow comparison".

      Now on your issue. You created 2 Strings. However, your assignment is wrong. You did not follow the following rule: Assign an Object to an Object reference.


      You created two Strings. They are not assigned to Object references. They are called "orphan Objects" because nobody can access them.
      • No democracy among Java objects?
        Hi, jabber, thanks a lot for your answer.
        I totally agree with you, but look at the following cases:

        1) String s1=new String("abc");
        String s2=new String(edf");
        s1==s2;
        This is a standard Java statement.
        Of course, compile and run well.

        2) "abc"=="edf";
        Complie and run well.
        A little bit strange since we compare two objects(not references), nevertheless it is understandable if noticing special properties of anonymous strings.

        3) new String("abc")==new String("edf");
        doesn't compile.
        Excluded Java standards. However,

        4) For all wrapper classes like
        new Boolean("TRUE")==new Boolean("hi");
        compile and run well!

        So democracy doesn't exist, there are some special objects having extraordinary rights.
        Do you agree?
        • You hit some dark corners...
          本文发表在 rolia.net 枫下论坛Java is a very "simple" language.

          In Java, if you create an Object and don't assign it to some reference, it will be an "orphan" Object. It means it is abandoned and it is subject to garbage collection. Of course, JVM can generate some internal references for an Object, but this is transparent to us, application programmers.

          You have listed 4 cases. 2), 3) and 4) are meaningless, no matter what results you get.
          Operator == is used to check if two references point to the same Object in the same JVM. In your case 2-4, you played the game agaist this rule. In some case, you pass because of the internal implementaion of JVM. In practice, no Java programmers write code of type 2), 3) and 4).

          Now about your case 1). You created two different Objects (Strings) in JVM. They have two different references "s1" and "s2". You compared s1 and s2. This should return false. However, you don't pick up this return value, so your comparison is much ado about nothing. In other word, your comparison has no effects.

          Reference is widely used in the conditionals such as

          if(s==null) {
          //do sth
          }


          if(s1==s2) {
          //do sth
          }

          I advise you stop thinking about case 2, 3, and 4). You cannot find similar code in practice. Thinking about them does not help you understand Java.

          In Passing, I mention the folllowing is OK though we don't give a reference to the created Object. See:

          new Timer().start();

          System.out.println(new String("abc").toUpperCase());更多精彩文章及讨论,请光临枫下论坛 rolia.net