equal vs ==


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: equal vs ==

  1. #1
    Join Date
    Oct 2002
    Posts
    6

    Unhappy equal vs ==

    What's the difference between 'equal' and '==' in JAVA?

  2. #2
    Join Date
    Nov 2002
    Posts
    138
    i think it's like this:

    String x = new String( "hello world" );
    String y = new String( "hello world" );
    String z = y;

    if( x == y ){ .... }
    if( y == z ){ .... }
    if( x.equals( y ) ){ .... }

    the answer would be:

    y == z, x.equals( y ) would return true, but x == y would be false.

    strings x and y are not pointing to the same address. but when you use the equals() method, it would check the contents of the string and not the address.

  3. #3
    Join Date
    Oct 2002
    Posts
    6
    Originally posted by xylex_blaiste
    i think it's like this:

    String x = new String( "hello world" );
    String y = new String( "hello world" );
    String z = y;

    if( x == y ){ .... }
    if( y == z ){ .... }
    if( x.equals( y ) ){ .... }

    the answer would be:

    y == z, x.equals( y ) would return true, but x == y would be false.

    strings x and y are not pointing to the same address. but when you use the equals() method, it would check the contents of the string and not the address.
    The below helloworld.java, it doesn't use the 'new String'. I wonder why the output is 'Success', so s1 and s2 are pointing to the same address?

    -------helloworld.java----------
    public class helloworld
    {

    public static void main(String args[])
    {
    String s1="hello world";
    String s2="hello world";
    if (s1==s2){
    System.out.println("Success");
    }
    else {
    System.out.println("Failure");
    }
    }
    }
    -------helloworld.java---------

  4. #4
    Join Date
    Nov 2002
    Posts
    138
    obviously! you are using a string literal in your example. when you do it the other way ( new String( "hello world"), there are 2 string objects created. the new string"hello world" and the literal "hello world".

    when you use a string literal in the same class with the same content, they represent the same reference. but there are instances when you don't get the same( true) results. if they were created during runtime they'll have different references.

  5. #5
    Join Date
    Nov 2002
    Posts
    138
    you read more about strings. it's interesting what you'll learn on this particular class. if you have a good book go ahead. but there's always the net....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center