Results 1 to 15 of 15
  1. #1
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Exclamation what the difference?!!

    Hi,
    Whats the difference between
    "==",
    "=",
    "equal"???
    thank you....:)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Engineer View Post
    Hi,
    Whats the difference between
    "==",
    "=",
    "equal"???
    thank you....:)
    They're two different operators; == compares two values for equality (of reference if the operands are object references) and = is the assignment operator. equals() is just a method that normally checks whether or not the content (state) of two objects are equal.

    kind regards,

    Jos

  3. #3
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    They're two different operators; == compares two values for equality (of reference if the operands are object references) and = is the assignment operator. equals() is just a method that normally checks whether or not the content (state) of two objects are equal.
    aha,ok but in this code
    PHP Code:
    String a  = new String("Java String1"); 
    String b = new String("Java String1"); 
             
    if(a == b) 
            System.out.println("Both Java Strings are equal."); 
    else 
        System.out.println("Java Strings are not equal."); 
             
    if(a.equals(b)) 
        System.out.println("Both Java Strings are equal."); 
    else 
        System.out.println("Java Strings are not equal.");
    i dont understaJava its output:confused:
    PHP Code:
    Java Strings are not equal. 
    Both Java Strings are equal

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Engineer View Post
    aha,ok but in this code
    PHP Code:
    String a  = new String("Java String1"); 
    String b = new String("Java String1"); 
             
    if(a == b) 
            System.out.println("Both Java Strings are equal."); 
    else 
        System.out.println("Java Strings are not equal."); 
             
    if(a.equals(b)) 
        System.out.println("Both Java Strings are equal."); 
    else 
        System.out.println("Java Strings are not equal.");
    i dont understaJava its output:confused:
    PHP Code:
    Java Strings are not equal. 
    Both Java Strings are equal
    As I wrote in my previous reply: the == operator checks whether or not two references are equal; a and b are definitely pointing to different Strings (that both have the same content) so a == b is false here. The equals( ... ) method checks the contents of the objects so a.equals(b) == true.

    kind regards,

    Jos

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    As I wrote in my previous reply: the == operator checks whether or not two references are equal; a and b are definitely pointing to different Strings (that both have the same content) so a == b is false here. The equals( ... ) method checks the contents of the objects so a.equals(b) == true.

    Jos

    but if you write

    Java Code:
    		String a  = "Java String1"; 
    		String b  = "Java String1";

    instead, then also == will be true, since the jvm will use the string pool, because the strings are equal and you will have the same reference for both strings a and b. but, if you use new explicitly, then the jvm will not use the string pool and will create two different objects, so the references will not be the same and so also == will return false.

  6. #6
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    :(
    Quote Originally Posted by j2me64 View Post
    but if you write

    Java Code:
    		String a  = "Java String1"; 
    		String b  = "Java String1";

    instead, then also == will be true, since the jvm will use the string pool, because the strings are equal and you will have the same reference for both strings a and b. but, if you use new explicitly, then the jvm will not use the string pool and will create two different objects, so the references will not be the same and so also == will return false.
    sorry , i cant understand:(

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Ok,

    == - compares two things
    = - assings a variable
    equals() - compares something.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Engineer View Post
    :(

    sorry , i cant understand:(
    Imagine you and me, both writing "this is a String" on a piece of paper and we both point our finger to our piece of paper. Do we point to the same piece of paper? No, so comparing our fingers with the == operator is false. Are the Strings on both pieces of paper the same? (ignoring handwriting style etc.) Yes, so yourFinger.equals(myFinger) is true. Forget about the previous example about String pools for now. First try to understand the difference of references (our fingers) and the difference of contents (the String on a piece of paper).

    kind regards,

    Jos
    Last edited by JosAH; 08-29-2010 at 04:29 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    Ok,

    == - compares two things
    = - assings a variable
    equals() - compares something.
    Great, that's exactly what the OP doesn't understand; 'compares something' and 'compares two things' isn't exactly clarifying stuff for sombody who doesn't understand references and content.

    kind regards,

    Jos

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    == is almost the same as equals()

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    == is almost the same as equals()
    Define 'almost'. 3 == 3.00000000001?

    kind regards,

    Jos

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Define 'almost'. 3 == 3.00000000001?

    kind regards,

    Jos
    They are the same !

    Because if you do

    Java Code:
    int a = 1;
    int b = 1;
    
    if(a == b){
    System.out.println("hello");
    }
    if(a.equals(b)){
    System.out.println("bye");
    }
    They are both the same?
    Last edited by PhQ; 08-29-2010 at 04:23 PM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    So it is the same?
    I was asking you.

    Quote Originally Posted by PhQ View Post
    Because if you do

    Java Code:
    int a = 1;
    int b = 1;
    
    if(a == b){
    System.out.println("hello");
    }
    if(a.equals(b)){
    System.out.println("bye");
    }
    They are both the same?
    Nope, it won't compile because primitives don't have methods.

    kind regards,

    Jos

  14. #14
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Quote Originally Posted by PhQ View Post
    Ok,

    == - compares two things
    = - assings a variable
    equals() - compares something.
    Not quite right.
    == compares the actual value stored in a variable. This could be a primitive (a char or an int) or could be the reference to an object.

    .equals() compares whats stored *inside* an object - what the reference points to.

  15. #15
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Imagine you and me, both writing "this is a String" on a piece of paper and we both point our finger to our piece of paper. Do we point to the same piece of paper? No, so comparing our fingers with the == operator is false. Are the Strings on both pieces of paper the same? (ignoring handwriting style etc.) Yes, so yourFinger.equals(myFinger) is true. Forget about the previous example about String pools for now. First try to understand the difference of references (our fingers) and the difference of contents (the String on a piece of paper).

    kind regards,

    Jos
    By this example i can understand......
    u are an exelent teacher:)
    from your explaining
    1-==
    to know if two references are to the same object
    and strings
    2-equal
    check the content of objects

Similar Threads

  1. What is the difference between
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 01-19-2009, 04:49 AM
  2. what is the difference between jsp and jsf?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-31-2008, 05:55 PM
  3. difference
    By nishant in forum New To Java
    Replies: 2
    Last Post: 09-17-2008, 06:04 PM
  4. Difference between ASP and JSP
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •