This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query with string intern method

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This displays NotEqual . why so .

Can anybody please tell me according to the docs , the intern will save memory.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49755
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down the execution of each statement, how many String objects are involved, and which String object they point to. That will sort out your confusion.
You need to record how many different copies of "Ravi" you are creating, how many simply vanish, and which of them you are referring to.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Campbell , what do you mean by this?
 
Henry Wong
author
Marshal
Pie
Posts: 21386
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell , what do you mean by this?


He means use a sheet of paper, and pencil, and pretend you are the JVM -- create the heap, create the reference to the heap, etc. and follow along the program. If you do it correctly, you should understand what is going on.

This is a talent that you need to develop as a programmer, so it's a good time to learn, if you have never done it before.

Henry
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the support . But when i checked these objects values through

they are pointing to the same reference inside heap .
 
Henry Wong
author
Marshal
Pie
Posts: 21386
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Pavan wrote:
But when i checked these objects values through

they are pointing to the same reference inside heap .


Really? How does printing out the values of two strings (reference by) tell you that they are the same string object, and not two different string objects with the same string value?

Henry
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Really? How does printing out the values of two strings (reference by) tell you that they are the same string object, and not two different string objects with the same string value?


Do i need to use javap ??
 
Henry Wong
author
Marshal
Pie
Posts: 21386
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Pavan wrote:
Do i need to use javap ??


It would be easier with a pencil and a sheet of paper. But sure... learning javap (and byte codes) is something that is good to learn too.

Henry
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"It would be easier with a pencil and a sheet of paper. "

I have got pencil and paper , but i don't know how to proceed , just give me a start .
 
Campbell Ritchie
Sheriff
Pie
Posts: 49755
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Declare an int variable and call it campbell
  • Put the value of 3 -----> here and then campbell points to that 3 ----->
  • Declare an int variable and call it ravi
  • Take the value you previously called campbell and . . .
  • Now the result of that arithmetic is what ravi ----> points at
  • Print the value of whatever ravi --------> points at and we can see the result which is 6


  • So you should have something like this
    campbell -----> 3
    ravi ------> 6
    Only I might have added it wrongly and it might not be 6.

    Now do the same for the Strings, so draw a square to represent the heap and write the value of all the Strings inside the heap, and the names you gave the outside the square, and join the names to the values with arrows --------->. As you go through the execution you may have to erase some arrows and draw new arrows.
     
    Ravi Kiran Va
    Ranch Hand
    Posts: 2234
    Eclipse IDE Firefox Browser Redhat
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you campbell . Always you point out to a real world .
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And I presume you have worked it out by now. Please tell us what you found.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you got it worked out yet?
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20606
    60
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Have you got it worked out yet?

    http://faq.javaranch.com/java/PatienceIsAVirtue

    Sorry, couldn't help it
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Prime wrote:http://faq.javaranch.com/java/PatienceIsAVirtue


    At least Rob has read my posts!
     
    Ravi Kiran Va
    Ranch Hand
    Posts: 2234
    Eclipse IDE Firefox Browser Redhat
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:And I presume you have worked it out by now. Please tell us what you found.


    sorry for the late reply as i thought you already know the answer .

    To save memory Java supports “interning” of Strings. When the intern() method is invoked on a String. If a String object with the same content is already in the table, a reference to the String in the table is returned. Otherwise, the String is added to the table and a reference to it is returned.

    To be frank , Every site only tells "The == operator will only be true if two String references point to the same underlying String object." which everybody knows .

    But when i checked internally ,
    this is not creating two Objects then why == is always false .


    And why Memory locations are not getting printed (;. I use Java 5.0
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know why you are getting false. I didn't know what you found about it.

    Java doesn't give access to memory locations directly. That is contrary to the spirit of using a high-level language; leave the JVM to remember what the memory locations are. In fact the memory locations can be different for different JVMs, and change whenever the garbage collector runs.
    I think you have missed something about String constructors. You are (strictly speaking) correct that the code you quoted doesn't involve two objects.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20606
    60
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ravi Pavan wrote: this is not creating two Objects then why == is always false .

    This is definitely creating two objects. Every time you use "new", you create a new object, even with String. str1 and str2 will share data, true, but they are still different objects. That's why str1 == str2 will be false. str1.intern() == str2.intern() will be true though.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49755
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I said it didn't involve two objects; apart from the two new String()s there is "Ravi" (twice) as well = 3 objects.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic