Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

possible error in exercises on 1z0-808 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, in chapter III question 10. What is the result of the following code?


A)COmpile error on line 14
b)Compile error on line 15.
c)Compile error on line 16.
d)Compile error on another line.
e)==
f)equals
g)an exception is thrown

The solutions say F, but should it instead be E and F ?
thanks
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:The solutions say F, but should it instead be E and F ?


No, the study guide is spot-on! The correct answer for this question is F. Have a look at this topic where you'll find a great detailed explanation about the exact same question. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, so I've had a read and in the case of 'a', the value is created at runtime. Just so I get it absolutely clear: if I have something like this:

then the value of a is a literal and goes in the string pool and the value of a isn't because we create it after, well, at runtime that is, since it starts with an empty string?
thanks
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:then the value of a is a literal and goes in the string pool and the value of a isn't because we create it after, well, at runtime that is, since it starts with an empty string?


No, that's incorrect! In your code snippet you have 3 String literals which will reside all in the String Literal Pool: "this is a new string", "", and "another string". These strings are already known at compile-time, the compiler doesn't need to execute any code to know the actual value of this String value.
So in your code snippet reference variable a1 first refers to the String literal "" (line2) and on line3 the reference of the String literal "another string" is assigned to reference variable a1.

All the above can be easily illustrated with a code snippet

Hope it helps!
Kind regards,
Roel
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, no in that case I don't think I've understood anything at all. Let me try again. And with your code this time.
In your code you said:

I would have said that the second statement would have been true as well because I would have thought that both a1 and a2 are now pointing to the same object because, from what I've read, when 2 variables of type string contain the same literal (and in your case they both contain "another string" then the value) then both variable are assigned a reference to that same object. Therefore a1 must be == to "another string" and so a2. But of course it's not, and I think I got lost...
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:from what I've read, when 2 variables of type string contain the same literal (and in your case they both contain "another string" then the value) then both variable are assigned a reference to that same object.


That's incorrect! The String Literal Pool only applies to String literals (and that's why it is called String Literal Pool of course ). If a String object is created at runtime (using e.g. the String concatenation operator as with reference variable a2 or using a StringBuilder), then this String object will never be in the String Literal Pool for the very simple reason: it is not a String literal, it's a String object created at runtime.

Let's have another look at the code snippet I providedIn this code snippet you have the following String literals: "this is a new string", "", "another string", "another ", and "string". The String Literal Pool will contain all these String literals. But although the String created on line3 has the same value as String literal "another string", it will be a different object. So that means in memory you'll have 2 String objects with the same value: a String literal "another string" (referred by reference variable a1, on line4, and on line5) and a String object "another string" (only referred by reference variable a2).
You should definitely read this post. It contains a very simple explanation (with code snippet) about how String literals might be processed. And I am pretty sure it might help you to understand this concept correctly.

Hope it helps!
Kind regards,
Roel
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic