Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literal chapter 3 review question (Java OCA 8 Programmer I Study Guide, Sybex)

 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the study guide on page 153 there's a review question as on the picture.
Correct answers: A,C,D.

I've read this great article about String literal pool, as well as several similar threads on coderanch about it.

However, lines 8 and 9 still remained unclear to me.

Why is it possible to write "Hello" == s ? It confuses me that "Hello" is an object, but here references are being compared.
"Hello".equals(s) in contrast is perfectly clear to me.

According to the article I mentioned above, "Hello" is referenced from a String literal pool. How is this reference stored (in a variable...) ?

Thank you very much in advance,

Aleksandra

review_chapter3_Q2.JPG
[Thumbnail for review_chapter3_Q2.JPG]
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Pestova wrote:Why is it possible to write "Hello" == s ? It confuses me that "Hello" is an object, but here references are being compared.
"Hello".equals(s) in contrast is perfectly clear to me. According to the article I mentioned above, "Hello" is referenced from a String literal pool. How is this reference stored (in a variable...) ? 


Strings, Literally by Corey McGlone wrote:When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool

  • I think you might have read above quoted part from that article.
  • Please refer diagram and all those numbers beneath all objects are just assumed location Or you can say reference of objects in memory to understand which objects we are talking about.
  • So when class is loaded then String objects of "Hello", "one", "two", "three", "four" and "five" String literals are created on heap and their references are kept in String literal pool.
  • Only String created by below code is created at runtime having value "Hello". The reference held by t will not be replaced with reference of String literal having same value "Hello" present in String literal pool until It is interned.

  • When you use == It compares references but not It's content. "Hello" == s   means It checks reference of "Hello" present in pool Or not then reference of "Hello" is already in pool so it returns that reference here and checks 1000 == 1750 where 1750 is assumed reference of object referred by t, which returns false but when "Hello".equals(s) then it compares actual value referred by these references i.e. "Hello".equals("Hello") which returns true.


  • Answer:
  • "Hello".equals(s) means "Hello".equals("Hello") returns true.
  • t == s means 1750 == 1000 returns false.
  • t.equals(s) means "Hello".equals("Hello") returns true.
  • "Hello" == s means  1000 == 1000 returns true.
  • "Hello" == t means  1000 == 1750 returns false.

  • Correct answer is A C and D.


    StringLiteralPool.png
    [Thumbnail for StringLiteralPool.png]
    String literal pool
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aleksandra Pestova wrote:In the study guide on page 153 there's a review question as on the picture.

    Why do you post code snippets as screenshots and not the plain code snippets (and you should of course UseCodeTags )? If I want to compile (and execute) your code snippets, I know have to type them myself. So I'll be losing precious time which I could otherwise be spending on typing a nice reply... And another reason: attached images are not searchable using the search engine, plain text or a code snippet definitely is.

    Aleksandra Pestova wrote:Why is it possible to write "Hello" == s ? It confuses me that "Hello" is an object, but here references are being compared.
    "Hello".equals(s) in contrast is perfectly clear to me.

    It is a bit strange that "Hello".equals(s) is perfectly clear to you, but "Hello" == s confuses you a bit Because if you think a little bit about the first statement: methods are only invoked on reference variables, never on objects. So "Hello" is a reference variable of type String (referring to a String object with value "Hello"). So that's why in the second statement reference variables are being compared (the String literal "Hello" is interpreted as a String reference variable referring to a String object with value "Hello"). So you could rewrite the original code snippet and use a temporary String reference variableAnd you can perform similar code constructs with other objects as well

    Aleksandra Pestova wrote:According to the article I mentioned above, "Hello" is referenced from a String literal pool. How is this reference stored (in a variable...) ?

    This post tries to explain what might be happening behind the scenes when the String Literal Pool comes into play. So read it carefully and if you still have doubts, let us know.

    In this forum you'll find plenty of topics about strings and the String Literal Pool. If you want to read a bit more about this hard to understand topic, the following topics are definitely worth reading as they offer great explanations with informative code snippets:
  • new String("abc") - one object or two?
  • strings and the string pool
  • What is now right about the String?


  • I think "String (and the number of objects created/eligible for GC)" is without any doubt the most popular topic in this forum. So using the search function you'll find plenty of topics about "String (and the number of objects created/eligible for GC)". These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about String (and the number of objects created/eligible for GC):
  • Question about strings
  • Garbage Collection Question
  • Confusion with String object (String Pool & Garbage Collection)
  • Confusion with String...
  • new String("abc") - one object or two?
  • strings and the string pool
  • OCA Java SE 8 Programmer - Question on String immutability
  • String method chaining and garbage collector
  • Understanding Equality, on page 118 (Java OCA 8 Programmer I Study Guide)
  • Doubts about String Concatenation Operator & Garbage Collection (K&B7 CH5 Q9)


  • Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:The reference held by t will not be replaced with reference of String literal having same value "Hello" present in String literal pool until It is interned.

    Please note that the intern() method of the String class is not on the OCAJP certification exam.

    Ganesh Patekar wrote:When you use == It compares references but not It's content. "Hello" == s   means It checks reference of "Hello" present in pool Or not then reference of "Hello" is already in pool so it returns that reference here and checks 1000 == 1750 where 1750 is assumed reference of object referred by t, which returns false but when "Hello".equals(s) then it compares actual value referred by these references i.e. "Hello".equals("Hello") which returns true.

    Just one remark on a cow-worthy post. The String Literal magic is performed by the JVM when the class is loaded. So when evaluating the expression "Hello" == s there will be no check to see if the String object "Hello" is present in the String Literal pool. This check has already been executed when the class is loaded.
    I think you made a (minor) typo: "Hello" == s should be "Hello" == t. Otherwise this part of the sentence is incorrect: "and checks 1000 == 1750 where 1750 is assumed reference of object referred by t, which returns false". If it really is reference variable s, then 1000 == 1000 would be evaulated and true is (obviously) printed.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:Please note that the intern() method of the String class is not on the OCAJP certification exam.
    Just one remark on a cow-worthy post. The String Literal magic is performed by the JVM when the class is loaded. So when evaluating the expression "Hello" == s there will be no check to see if the String object "Hello" is present in the String Literal pool. This check has already been executed when the class is loaded.
    Ohh I see ...
    Roel De Nijs wrote:I think you made a (minor) typo: "Hello" == s should be "Hello" == t.
    Ehh yes I did, mistakenly wrote s rather than t. Thank you Roel for correctig me
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for a great explanation, Roel and Ganesh! 

    the String literal "Hello" is interpreted as a String reference variable referring to a String object with value "Hello"


    "Hello" == s   means It checks reference of "Hello" present in pool

    That's what I didn't know. I understood that in a String literal pool references to the literals are stored and then got totally confused HOW Java was going to find out that I meant a literal and not an object.
    I know very little about what's going on internally, so it makes harder to understand things very often.

    With a temporary String reference variable it's much easier.

    Thanks a lot for so many links! I'll definitely go through them.

    Why do you post code snippets as screenshots and not the plain code snippets (and you should of course UseCodeTags )? If I want to compile (and execute) your code snippets, I know have to type them myself. So I'll be losing precious time which I could otherwise be spending on typing a nice reply... And another reason: attached images are not searchable using the search engine, plain text or a code snippet definitely is. 


    Sorry I haven't thought about that.    It was faster to make a screenshot, next time - nice code snippet using code tags. 
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aleksandra Pestova wrote:"Hello" == s   means It checks reference of "Hello" present in pool
    I hope you read further discussion, It is "Hello" == t, I mistakenly wrote s rather than t
    Aleksandra Pestova wrote:I'll definitely go through them.
    Me too   and you're welcome
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah, I read further as well. No problem.

    Thanks!
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aleksandra Pestova wrote:I understood that in a String literal pool references to the literals are stored and then got totally confused HOW Java was going to find out that I meant a literal and not an object.
    I know very little about what's going on internally, so it makes harder to understand things very often.

    With a temporary String reference variable it's much easier.

    Thinking about it as a temporary String reference variable is the way to go! That's a simplified version of all the String Literal magic happening behind the scenes. A tad more complex example of this magic introduces an array to store all String literalsAnd then the String literal gets added somehow to this array at an unused (free) indexAnd then all occurences of this String literal are replaced with the appropriate reference from the String Literal Pool

    Hope it helps!
    Kind regards,
    Roel
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow that's an awesome analogy! Thanks a lot!  
    Now it's perfectly clear.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic