wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes query with string intern method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "query with string intern method" Watch "query with string intern method" New topic
Author

query with string intern method

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



This displays NotEqual . why so .

Can anybody please tell me according to the docs , the intern will save memory.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
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

Joined: Apr 18, 2009
Posts: 2234



Campbell , what do you mean by this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


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

they are pointing to the same reference inside heap .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

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

Joined: Apr 18, 2009
Posts: 2234


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
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

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

Joined: Apr 18, 2009
Posts: 2234

"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

Joined: Oct 13, 2005
Posts: 37958
    
  22
  • 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

    Joined: Apr 18, 2009
    Posts: 2234

    Thank you campbell . Always you point out to a real world .
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    You're welcome
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    And I presume you have worked it out by now. Please tell us what you found.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    Have you got it worked out yet?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    Campbell Ritchie wrote:Have you got it worked out yet?

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

    Sorry, couldn't help it


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    Rob Prime wrote:http://faq.javaranch.com/java/PatienceIsAVirtue


    At least Rob has read my posts!
    Ravi Kiran Va
    Ranch Hand

    Joined: Apr 18, 2009
    Posts: 2234

    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

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    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

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    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

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    I said it didn't involve two objects; apart from the two new String()s there is "Ravi" (twice) as well = 3 objects.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: query with string intern method
     
    Similar Threads
    String Problem
    intern method
    intern() method
    Why would this print true?
    String class