Results 1 to 2 of 2
  1. #1
    venat123 is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Same old Question please reply

    Hi All,

    Please don't get weird with the same old question. I am getting confusion in the string comparision.
    I am trying to execute the following programme where i am getting doubt.
    String s = new String("abcus");
    String s1 = new String("abcus");
    String s2 = new String("abcus");
    String s3 = new String("abcus");

    int sh = s.hashCode();
    int s1h = s1.hashCode();

    int s2h = s2.hashCode();
    int s3h = s3.hashCode();

    System.out.println(sh);
    System.out.println(s1h);
    System.out.println(s2h);
    System.out.println(s3h);
    if(sh == s1h)
    {
    System.out.println("equal");
    }
    else{
    System.out.println("not equal");
    }

    }


    When I compiled the above code


    92599936
    92599936
    92599936
    92599936

    if i try to compile with checking the hash code using "==" then it is giving every time
    as EQUAL.
    if i try to compile with checking the reference variable using "==" then it is giving as NOT EQUAL.

    By the above output i understand that while checking with "==" it not only checks the HashCode it also compares other thing.

    I tried to get the actual implementation of Equals method and "==" but not able to find :(

    Could you please let me know the reason behind the above checking and it would be great if I get the url for actual implementation of the above code.


    Thanks in advance to all.
    Regards,

    Venkat

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:51 AM
  3. Need help on this question
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-27-2008, 04:58 PM
  4. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 11:18 AM
  5. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 10:17 PM

Posting Permissions

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