8 Replies Latest reply: Jan 5, 2007 7:12 AM by 807599 RSS

    string comparsion......


      The following code will print : fasle
      if( new Boolean("true") == new Boolean("true"))
      can you please give me a case when if i compare strings using "==" the answer will evaluate to true. When i ever I compare strings that way i always get "false". I was just wondering how i might get a true answer instead false.


        • 1. Re: string comparsion......
          You are not comparing strings, you are comparing Boolean objects.

          Use .equals for comparing objects.
          • 2. Re: string comparsion......
            Compare Objects for equality by using their equals(Object) method.
            • 3. Re: string comparsion......
              String a = new String("abc");
              String b = new String("abc");
              String c = b;
              System.out.println("a == b: "+(a==b));//false
              System.out.println("a.equals(b): "+(a.equals(b)));//true
              System.out.println("b == c: "+(b==c));//true
              System.out.println("b.equals(c): "+(b.equals(c)));//true
              • 4. Re: string comparsion......
                String test = "This is a String";
                String test2 = test;
                System.out.println(test == test2);
                Will print true

                This works because both string variables are pointing at the same String Object. If you do this instead
                          String test = "This is a String";
                          String test2 = new String(test);
                          System.out.println(test == test2);
                It prints false, because the new String() creates a new String Object and sets its value to the value of test.

                • 5. Re: string comparsion......
                  Compare Objects for equality by using their
                  equals(Object) method.
                  I know i should use:
                  s1.equals(s2) -
                  to compare the character contents of the two strings.


                  s1= = s2 to compare two object references to check if they refer to the same instance.

                  BUT what im trying to say is when will two objects ever refer to the same instance???? I mean why was the answer to te question that i posted false and not true?

                  Im new to java and these concepts are new to me.

                  Thanks for bearing with me
                  • 6. Re: string comparsion......
                    In your example you are creating 2 different objects, so they are not equal when using == to compare (they are references to 2 different objects)

                    if you do this
                    System.out.println("test" == "test");
                    it prints true, because java will create a String object with the value "test" and intern it, so when you compare "test" to "test" both strings are pointing at the same Object, thus it prints true.

                    Hope that helps to clarify it for you.

                    • 7. Re: string comparsion......
                      Thank you soooooooooooo much everyone.......I really understand it now :D

                      • 8. Re: string comparsion......
                          System.out.println(Boolean.TRUE == Boolean.TRUE);