Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. == String comparison [modified]

== String comparison [modified]

Scheduled Pinned Locked Moved Java
javaasp-netquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnnnS
    wrote on last edited by
    #1

    Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:

    modified on Thursday, February 11, 2010 8:38 AM

    L N 2 Replies Last reply
    0
    • A AnnnS

      Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:

      modified on Thursday, February 11, 2010 8:38 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      As you guess the objects are the same, as they both point to the exact same constant; this is the Java compiler optimising your code. Then when you add another character to a it becomes a new object and is no longer the same as b, even though, in human terms it has not changed.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      A 1 Reply Last reply
      0
      • L Lost User

        As you guess the objects are the same, as they both point to the exact same constant; this is the Java compiler optimising your code. Then when you add another character to a it becomes a new object and is no longer the same as b, even though, in human terms it has not changed.

        txtspeak is the realm of 9 year old children, not developers. Christian Graus

        A Offline
        A Offline
        AnnnS
        wrote on last edited by
        #3

        Thanks a lot !!

        1 Reply Last reply
        0
        • A AnnnS

          Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:

          modified on Thursday, February 11, 2010 8:38 AM

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          To add to Richards earlier answer, == checks that the objects are the same and equals(Object) should be overriden for ALL classes and checks that the contents are the same [this is the case with String]. Try this to see:

          public class TestEquals
          {
          public static void main(String args[])
          {
          // Make sure we have two DIFFERENT objects:
          String a = new String("Hai");
          String b = new String("Hai");

          if(a == b)
          {
            System.out.println("a == b true"); 
          } else {
            System.out.println("a == b false");
          }
          
          if(a.equals(b))
          {
            System.out.println("a.equals(b) true"); 
          } else {
            System.out.println("a.equals(b) false");
          }
          

          }
          }


          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

          A 1 Reply Last reply
          0
          • N Nagy Vilmos

            To add to Richards earlier answer, == checks that the objects are the same and equals(Object) should be overriden for ALL classes and checks that the contents are the same [this is the case with String]. Try this to see:

            public class TestEquals
            {
            public static void main(String args[])
            {
            // Make sure we have two DIFFERENT objects:
            String a = new String("Hai");
            String b = new String("Hai");

            if(a == b)
            {
              System.out.println("a == b true"); 
            } else {
              System.out.println("a == b false");
            }
            
            if(a.equals(b))
            {
              System.out.println("a.equals(b) true"); 
            } else {
              System.out.println("a.equals(b) false");
            }
            

            }
            }


            Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

            A Offline
            A Offline
            AmbiguousName
            wrote on last edited by
            #5

            Nagy Vilmos wrote:

            // Make sure we have two DIFFERENT objects: String a = new String("Hai"); String b = new String("Hai");

            Thanx...You are right. We should make sure that we make two different objects. But I have a doubt in my mind....both a and b should refer to same objects if we write b = a...why they are refering to same object if we say a="Hai" and b="Hai"??

            N 1 Reply Last reply
            0
            • A AmbiguousName

              Nagy Vilmos wrote:

              // Make sure we have two DIFFERENT objects: String a = new String("Hai"); String b = new String("Hai");

              Thanx...You are right. We should make sure that we make two different objects. But I have a doubt in my mind....both a and b should refer to same objects if we write b = a...why they are refering to same object if we say a="Hai" and b="Hai"??

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #6

              What I think you are asking is why, when we write...

              String a="Hai";
              String b="Hai";

              ...are a and b the same object and not just the same value? When you use a literal string, there will only be one created unless you explicitly create a new instance. m Think of the literal as a constant referencing an object.


              Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups