aspose file tools*
The moose likes Java in General and the fly likes How we can find that how much string objects has been created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How we can find that how much string objects has been created?" Watch "How we can find that how much string objects has been created?" New topic
Author

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

Hemant Khandal
Greenhorn

Joined: May 29, 2011
Posts: 11
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

Joined: Dec 26, 2011
Posts: 73
Whenever you create a string, a new string object gets created
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Try using a profiler such as VisualVM. This will allow you to easily see all the objects in memory.
Hemant Khandal
Greenhorn

Joined: May 29, 2011
Posts: 11
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 08, 2009
Posts: 606

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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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.



When you do things right, people won't be sure you've done anything at all.
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

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

Joined: Jan 08, 2012
Posts: 1
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

Joined: Sep 30, 2010
Posts: 290

You need to choose correct forum .Carefully Choose One Forum .
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 30, 2010
Posts: 290

The only way to get in touch to String pool is through literal or intern() method.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 17, 2011
Posts: 8223
    
  23

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Winston


I did, thanks. Happy New Year to you too!
 
Don't get me started about those stupid light bulbs.
 
subject: How we can find that how much string objects has been created?