Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How we can find that how much string objects has been created?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body tell me how much object has been created implicitly or explicitly?
Here is any way (method or anything) to find out created object?

public class stringClass {

public static void main(String args[]){
String name="Hemant";
String sName=name+"Khandal";
String s=sName+"Java";
String ss=new String("Hemant");
}

}

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you create a string, a new string object gets created
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using a profiler such as VisualVM. This will allow you to easily see all the objects in memory.
 
Hemant Khandal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saahil gupta wrote:Whenever you create a string, a new string object gets created



Hi Saahil,
Please let me know how much object will be created as per your answer.
Thanks.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you care? What are you trying to accomplish by answering this question? In over 12 years of programming professionally with Java I have never once needed to know this. And you'll have to define what you mean by "implicitly" vs. "explicitly" in this context. Your question as stated is rather vague and pointless.
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:In over 12 years of programming professionally with Java I have never once needed to know this. And you'll have to define what you mean by "implicitly" vs. "explicitly" in this context. Your question as stated is rather vague and pointless.



Its probably an exam/interview question.

@OP - If its an exam question you need to quote the source for the exam, so we know its not a *copyrighted* question.
I seriously wish we move away from such questions in exams and interviews though ...
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a pretty common concept for folks to get hung up on. It's an important thing for newer programmers to think about -- to learn good coding habits in the future. No need to create unneeded objects (espescially Strings or Wrapper classes) when something you have already matches.

So how many get created? I think I count 6.

 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to what Janeice DelVecchio said.... While dealing with Strings we need to consider String Constant pool.When ever a new String object is created it is stored in String Constant Pool. When we try to create same string object again, java just uses the same one existing on POOL.(Note: STRING objects are immutable.)

Two ways of creating string object;

Coming to your example:



Hope this is helpful...

Thanks,
Don,Red..
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dona Red kumar wrote:Adding to what Janeice DelVecchio said.... While dealing with Strings we need to consider String Constant pool.When ever a new String object is created it is stored in String Constant Pool. When we try to create same string object again, java just uses the same one existing on POOL.(Note: STRING objects are immutable.)

Two ways of creating string object;



Hey Dona,
Just wanted to clarify something.. As per my knowledge (please refer comments in the code),



Please correct me if I'm wrong.
 
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to choose correct forum .Carefully Choose One Forum .
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saahil gupta wrote:Whenever you create a string, a new string object gets created


Not so (unless you're speaking literally).
String name="Hemant";
may or may not create a new object.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:This is a pretty common concept for folks to get hung up on. It's an important thing for newer programmers to think about -- to learn good coding habits in the future. No need to create unneeded objects (espescially Strings or Wrapper classes) when something you have already matches.

So how many get created? I think I count 6.




Nope. Executing that line does not create a String object.

This is why I hate these types of questions. They're usually too vaguely worded for what they're trying to address.


String sName=name+"Khandal"; // two here.... Khandal and HemantKhandal


Executing that line creates one StringBuilder object and one String object

String s=sName+"Java"; // two here.... Java and HemantKhandalJava


Executing that line creates one StringBuilder object and one String object

String ss=new String("Hemant"); // one here... Hemant


Correct.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

saahil gupta wrote:Whenever you create a string, a new string object gets created


Not so (unless you're speaking literally).
String name="Hemant";
may or may not create a new object.

Winston



In "normal" JVMs, executing that line never creates a String object. It simply assigns to our variable a reference to a String that was created in the constant pool when the class was initialized.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:This is a pretty common concept for folks to get hung up on. It's an important thing for newer programmers to think about -- to learn good coding habits in the future. No need to create unneeded objects (espescially Strings or Wrapper classes) when something you have already matches.



Yes and no.

It's good to know that the constant pools exist, and that


are preferable to


and why. But the typical "How many String objects are created here?" questions are not particularly good at testing that, IMHO, and even skilled programmers who know about the constant pools and write proper code never need to know the answer to those questions, and often get them wrong.

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to get in touch to String pool is through literal or intern() method.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Nope. Executing that line does not create a String object.



So what you're saying is that when you have a String literal (String d = "dog") -- no String object gets created on the heap and a reference to it floated around in the pool? There is no object?

String s=sName+"Java"; // two here.... Java and HemantKhandalJava


Executing that line creates one StringBuilder object and one String object



Okay, I'll give that StringBuilder to you.... but you still need the literal, "Java" (which gets created in the pool), and the new, concatenated String.

this line gets made to look something like this by the compiler:


I still see 2. The String that s has a reference to, and the String literal "Java". The literal may not be created at the time of this line's execution, it may live in the pool once the class is loaded, but "Java" still needs to be created at some time on the heap (with a reference to it from the pool), as well as the String that s has a reference to.

See references:
http://kaioa.com/node/59
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:

Jeff Verdegan wrote:

Nope. Executing that line does not create a String object.



So what you're saying is that when you have a String literal (String d = "dog") -- no String object gets created on the heap and a reference to it floated around in the pool? There is no object?



No, that's not what I'm saying. What I'm saying is exactly what I wrote. Namely, that executing that line does not create a String object. Since a String object obviously exists, it must be created somewhere else. String objects that are in the constant pool due to literals in the source code are created when the class is initialized. The line in question simply gets a reference to that already-existing String in the pool and assigns it to our variable.


String s=sName+"Java"; // two here.... Java and HemantKhandalJava


Executing that line creates one StringBuilder object and one String object



Okay, I'll give that StringBuilder to you.... but you still need the literal, "Java" (which gets created in the pool), and the new, concatenated String.



True. And again, it's created at class initialization, not as a result of executing that line.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Winston Gutkowski wrote:

saahil gupta wrote:Whenever you create a string, a new string object gets created


Not so (unless you're speaking literally).
String name="Hemant";
may or may not create a new object.

In "normal" JVMs, executing that line never creates a String object. It simply assigns to our variable a reference to a String that was created in the constant pool when the class was initialized.


I was speaking more figuratively than literally, but what you say definitely makes sense.

BTW, Happy New Year Jeff. Hope you had a good hols.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
BTW, Happy New Year Jeff. Hope you had a good hols.

Winston



I did, thanks. Happy New Year to you too!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic