Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How many String objects created?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, ranchers:
String A=new String("WHY");
How many? one or two? And reasons.
Thanks
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String A = new String("WHY");
only creates one String object. Why two? You mean the literal inside the paranthesis?


Je penses que je suis --Renee Descartes


In fact, it's "Je pense, donc je suis"
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply, Valentin:
So, in this case only one String object created? How about that String literal? Last time people
argued that there would be 2 String objects created: one by string literal, one by string constructor. I need a final say about this.

BTW, "je pense que je suis" should be crrect, que means donc in this case.
victor

Originally posted by Valentin Crettaz:
String A = new String("WHY");
only creates one String object. Why two? You mean the literal inside the paranthesis?

In fact, it's "Je pense, donc je suis"

 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you say this has been argued many times, so you are unlikely to get a definitive answer here.
It is however true that you will get a literal String evaluated at compile time and stored in static memory and also one generated at runtime on the heap.
Whether you argue that this is one or two objects is less important than that you understand the process. For most practical purposes it is better to regard the answer as two objects.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please point to earlier thread of discussion about the same topic? I am curious about two string object creation ?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks 4 the reply:
So you mean two objects created. Thats what I am thinking. As long as we keep arguing, we will get a definite answer. We are here for the practical purposes of SCJP, so this is definitely equally important that we clarify the question and get the answer, cuz it will fail you in the exam!
victor

Originally posted by Graeme Brown:
As you say this has been argued many times, so you are unlikely to get a definitive answer here.
It is however true that you will get a literal String evaluated at compile time and stored in static memory and also one generated at runtime on the heap.
Whether you argue that this is one or two objects is less important than that you understand the process. For most practical purposes it is better to regard the answer as two objects.

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua Block has a WHOLE section of his book Effective Java devoted to this topic. Se page 13
Item 4: Avoid creating duplicate objects.


It is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable (Item 13).
As an extreme example of what not to do, consider this statement:
String s = new String("silly"); //DON'T DO THIS!
The statment creates a new String instance each time it is executed, and none of those object creates is necessary. The argument to the String constructor ("silly") is itself a String instance, functionally identical to all of the objects created by the construtor. etc.
(goes on to apply the same principle to other objects and discussed Static Factory Methods as solution).


That statement creates a String instance, then uses that as the arguement to the String constructor - silly.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Graeme,
I think it is better to see the statement
String a = new String("WHY");
as creating one object and not two. But it is important to make the difference between the effect of creating the String "WHY" using the former statement and the following one:
String b = "WHY";
This last statement is allowed for optimization purposes and uses interning by default, while the former (with new) is the normal way of creating garbage-collectible String objects.
This topic has been heavily discussed in this forum, so I guess a search wouldn't hurt either.
Here is one good link to a former discussion on the topic: http://www.javaranch.com/maha/Discussions/Garbage_Collection/Str ings_-_Khalid_-_JavaRanch_Big_Moose_Saloon.htm
victor,

BTW, "je pense que je suis" should be correct, que means donc in this case.


Well, frankly, "que" doesn't mean "donc", where did you read that? The one I gave you is the actual quote form the "Discours de la methode".
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, this is from JLS:
(3.10.5) A string literal is always of type String($4.3.3). A string literal always refers to the same instance of class String.
---------------------------------------------
If we assume that "for certification purposes, pretend the intern pool does not exist". Then we have one object created. Otherwise, we have two.
As for Rene Descartes' famous sentence, I read it from "DISCOURS DE LA METHODE POUR BIEN CONDUIRE SA RAISON, ET CHERCHER LA VERITE DANS LES SCIENCES" when I was in school. But my memory may fail me. The correct form shold be: Je pense, que
je suis. Que is like "therefore" in English, its
ole French style, I guess. "Donc" is like "then" in English, more like modern French style. I may be wrong. Sorry, I have no time to check out this now, I will appreciate if you can give a link point directly to that famous paragraph.
victor

Originally posted by Valentin Crettaz:
Graeme,
I think it is better to see the statement
String a = new String("WHY");
as creating one object and not two. But it is important to make the difference between the effect of creating the String "WHY" using the former statement and the following one:
String b = "WHY";
This last statement is allowed for optimization purposes and uses interning by default, while the former (with new) is the normal way of creating garbage-collectible String objects.
This topic has been heavily discussed in this forum, so I guess a search wouldn't hurt either.
Here is one good link to a former discussion on the topic: http://www.javaranch.com/maha/Discussions/Garbage_Collection/Str ings_-_Khalid_-_JavaRanch_Big_Moose_Saloon.htm
victor,

Well, frankly, "que" doesn't mean "donc", where did you read that? The one I gave you is the actual quote form the "Discours de la methode".

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Victor, I guess I'm too picky.
Moreover, this doesn't belong here but here is the link:
Discours de la methode.
Since I started the digression, I'm gonna stop it now, otherwise some may see it as a thread hijacking, which is not
[ February 04, 2002: Message edited by: Valentin Crettaz ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its fine to be "picky", we need to be extremely picky in technical fields. Its not a
digression in terms of the discussions of the exsistence of string objects.
I have no time to reread "Discours", but the following is also from Descartes:
----------------------------------------------
� Ne m'avouerez-vous pas que vous �tes moins assur� de la pr�sence des objets que vous voyez, que de la v�rit� de cette proposition : Je pense, donc je suis ? Or cette connaissance n'est point un ouvrage de votre raisonnement, ni une instruction que vos ma�tres vous aient donn�e ; votre esprit la voit, la sent et la manie. � Lettre de mars ou avril 1648 � Silhon
----------------------------------------------
I guess you are right, but my sentence is another
way to say the same thing. To quote the exact thing from Descartes, yours is better.
victor

Originally posted by Valentin Crettaz:
Sorry Victor, I guess I'm too picky.
Moreover, this doesn't belong here but here is the link:
Discours de la methode.
Since I started the digression, I'm gonna stop it now, otherwise some may see it as a thread hijacking, which is not
[ February 04, 2002: Message edited by: Valentin Crettaz ]

 
Sheriff
Posts: 16067
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that RHE explains it best:

...explicitly calling new String() results in the existence of two objects, one in the literal pool and the other in the program's space.


IMO, the crux of the matter is understanding when each instance actually comes into existence. The String literal is placed in the literal pool and gets created when the class is loaded, while the other is created and put in the heap when new String() is executed.
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to check what object exist on heap and/or on literal pool ? Is there some kind of list maintained by java runtime which we can tap into to find the "code certified" answer, although can it be also a JVM implementation specific thing ?
If someone can point me to API's that we can use to get into Heap and/or Literal pool, I would be more then happy to write the code and share it here.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is all spelled out very clearly in the Java Language Specification.
3.10.5 String Literals


.
.
Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
.
.


In:
String A=new String("WHY");
The "WHY" is a String literal and created at compile time and stored in the Constant Pool.
The object referenced by A is created at run time and points to the String literal "WHY" (which IS an instance of String itself) that is contained in the Constant String Pool.
Just because a String Literal is not on the heap with the rest of the objects, does not mean that it is not an object.
Note that in the Java Virtual Machine Specification, in 4.4 The Constant Pool the section on the
4.4.3 The CONSTANT_String_info Structure


The CONSTANT_String_info structure is used to represent constant objects of the type String:


It says that the Strings in the Constant Pool are constant objects.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic