This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Hemant Khandal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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");
}

}

 
Ifteqar Ald
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you create a string, a new string object gets created
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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 ...
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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..
 
Al Apaji
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to choose correct forum .Carefully Choose One Forum .
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
The only way to get in touch to String pool is through literal or intern() method.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic