Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: regarding String

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    india
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default regarding String

    1)whether object reference and hashcode are equal or not?


    2)if it is equl


    String s1=new String("ravikumar");
    String s2=new String("ravikumar");
    if(s1==s2)//here i am getting false y


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    584
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: regarding String

    s1 and s2 are different instances of String objects, so s1==s2 must be false

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    india
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: regarding String

    but i am getting the same hashcode for both the objects....

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: regarding String

    So!

    My parents both have the same address. It doesn't mean they are the same person!
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    india
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: regarding String

    k...thanku but

    i need one confirmation whether object reference and hashcode are same or not

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: regarding String

    Of course they are not the same. A reference is how a variable can access an object stored in memory. A hashcode is some arbitrary value that the hashcode method returns for a particular object. The following code is a valid but not very useful hashcode method.
    public int hashcode() {
        return 0;
    }
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    india
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: regarding String

    thanku........

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM