1 2 Previous Next 21 Replies Latest reply: Aug 1, 2007 3:31 PM by 807600 RSS

    strings

    807600
      String str = "rome";
      str = "florence";
      String str2 = "rome";
      str at first was pointing to the object "rome".
      str2 is now pointing to an object "rome"
      is str2 pointing to the exact same object that str was aimed at before, or is there a separate object with the same value? my guess is the objects are different but i'm not sure.
        • 1. Re: strings
          807600
          str != str2, but str.equals(str2)
          • 2. Re: strings
            807600
            what? they don't equal each other if you do
            System.out.print(str.equals(str2));
            you get false
            • 3. Re: strings
              3004
              In the case of string literals--stuff in "double quotes" in your code--it will be the same object.

              "rome" and "florence" go into the constant pool when your class is loaded, and anywhere "rome" or "florence" appears in your code it will refer to that object in the pool.

              For strings that are not in the constant pool--those that come from, say, user input, or reading a file, or whatever--you can force them into the pool with the intern() method.
              • 4. Re: strings
                800282
                You could test if your guess is correct:
                String str = "rome";
                String str1 = str;
                str = "florence";
                String str2 = "rome";
                System.out.println(str1 == str2);
                • 5. Re: strings
                  3004
                  str != str2
                  Wrong.

                  Or rather, the orginal str == str2. After str = "florence", both == and equals are false.
                  • 6. Re: strings
                    807600
                    str != str2
                    Wrong.

                    Or rather, the orginal str == str2. After str =
                    "florence", both == and equals are false.
                    Ya ok thats what i thought
                    • 7. Re: strings
                      807600
                      You could test if your guess is correct:
                      String str = "rome";
                      String str1 = str;
                      str = "florence";
                      String str2 = "rome";
                      System.out.println(str1 == str2);
                      i ran that code and it came up true, so i was wrong!
                      so the same object first had the reference str and now it has str2, right?
                      • 8. Re: strings
                        807600
                        str != str2
                        Wrong.

                        Or rather, the orginal str == str2. After str =
                        "florence", both == and equals are false.
                        Blast! he reassigned str. I thought that was like str3 or something.
                        • 9. Re: strings
                          807600
                          str1 is assigned to the reference of "rome" and then str is changed but str1 reference is still the same(this is main characteristic of non primitive types)
                          so the result is true
                          • 10. Re: strings
                            800282
                            i ran that code and it came up true, so i was wrong!
                            so the same object first had the reference str and
                            now it has str2, right?
                            I made a drawing of it:
                            String str = "rome";
                                    ┌-----------------┐
                                    |   String-pool   |
                                    ├-----------------┤
                                    |                 |
                            str --------> "rome"      |
                                    |                 |
                                    └-----------------┘
                            
                            
                            String str1 = str;
                                    ┌-----------------┐
                                    |   String-pool   |
                                    ├-----------------┤
                                    |                 |
                            str ------┬-> "rome"      |
                                    | |               |
                            str1 -----┘               |
                                    |                 |
                                    └-----------------┘
                            
                            
                            str = "florence";
                                    ┌-----------------┐
                                    |   String-pool   |
                                    ├-----------------┤
                                    |                 |
                            str --------> "florence"  |
                                    |                 |
                            str1 -------> "rome"      |
                                    |                 |
                                    └-----------------┘
                                    
                            
                            String str2 = "rome";  // "rome" was already present, so it gets 'recycled'.
                                    ┌-----------------┐
                                    |   String-pool   |
                                    ├-----------------┤
                                    |                 |
                            str --------> "florence"  |
                                    |                 |
                            str1 -----┬-> "rome"      |
                                    | |               |
                            str2 -----┘               |
                                    |                 |
                                    └-----------------┘
                                    
                            String str3 = new String("rome"); // forces a new "rome" to be created
                                    ┌-----------------┐
                                    |   String-pool   |
                                    ├-----------------┤
                                    |                 |
                            str --------> "florence"  |
                                    |                 |
                            str1 -----┬-> "rome"      |
                                    | |               |
                            str2 -----┘               |
                                    |                 |
                            str3 -------> "rome"      |
                                    |                 |
                                    └-----------------┘
                            
                            
                            At the end:
                            str1 == str2        (true)
                            str1 == str3        (false)
                            str1.equals(str3)   (true)
                            • 11. Re: strings
                              807600
                              nicely done!
                              makes things clearer,
                              ty
                              • 12. Re: strings
                                807600
                                str1 is assigned to the reference of "rome" and then
                                str is changed but str1 reference is still the
                                same(this is main characteristic of non primitive
                                types)
                                so the result is true
                                        String s1 = "A";
                                        s1 = "B";
                                        String s2 = "A";
                                        System.out.println(s1 == s2);
                                will definitely print "false" ...
                                • 13. Re: strings
                                  800282
                                  ...
                                  String s1 = "A";
                                  s1 = "B";
                                  String s2 = "A";
                                  System.out.println(s1 == s2);

                                  will definitely print "false" ...
                                  Yes, that prints false, but that was (probably) not what s/he meant.
                                  • 14. Re: strings
                                    800282
                                    nicely done!
                                    makes things clearer,
                                    ty
                                    You're welcome Mark.
                                    I see you've moved away from the math problems lately.
                                    ; )
                                    1 2 Previous Next