aspose file tools*
The moose likes Java in General and the fly likes Simple question about Strings in memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Simple question about Strings in memory" Watch "Simple question about Strings in memory" New topic
Author

Simple question about Strings in memory

Al Razor
Greenhorn

Joined: Mar 08, 2010
Posts: 16
Hi!

I got one simple question about Strings. Imagine we got the following code:


The question I am interested in is "How many objects will be created in the memory?"
One book contained the information about 3 objects being created after executing a similar code.
But does it really mean that there will be created a separate String object for " Machine" ?
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 53
I think there String object will be created ,
first for "Virtual"
second for " Machine" and
third for "Virtual Machine".
Please correct me if I am wrong.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ravishanker kumar wrote:I think there String object will be created ,
first for "Virtual"
second for " Machine" and
third for "Virtual Machine".
Please correct me if I am wrong.


Just wanted to say the same. It goes back to String immutability.


JDBCSupport - An easy to use, light-weight JDBC framework -
Al Razor
Greenhorn

Joined: Mar 08, 2010
Posts: 16
There was a typo in my original post - the book contained information about 3 objects.
I just thought that JVM won't create a new object for "temporary" String (like " Machine") and wanted to make sure if this answer was correct.

Thanks for help, guys!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How is it "temporary"? It's an object, you created it. It has to exist *somewhere*, yes?
Al Razor
Greenhorn

Joined: Mar 08, 2010
Posts: 16
David Newton wrote:How is it "temporary"? It's an object, you created it. It has to exist *somewhere*, yes?


There is no need to jump on me :).
The above-mentioned String (" Machine") was temporary used for concatination.
I thought that, if the string was not explicitly allocated using the "new" word or assigned to some reference variable, JVM would not create a separate object for it, but would create the object with "Virtual Machine" at once.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Al Razor wrote:
There is no need to jump on me .


I didn't think David was "jumping" on you, but more like trying to get you to think it out for yourself.

Al Razor wrote:
The above-mentioned String (" Machine") was temporary used for concatination.
I thought that, if the string was not explicitly allocated using the "new" word or assigned to some reference variable, JVM would not create a separate object for it, but would create the object with "Virtual Machine" at once.


There is mechanism is the compiler that does this. If you do concatenation, when it is clear that what the results will be, the compiler will generate code that does not include the temporary strings.

However, for that mechanism to work, it has to be compile time constants, which in this case, are not.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Al Razor
Greenhorn

Joined: Mar 08, 2010
Posts: 16
Henry Wong wrote:
Al Razor wrote:
There is no need to jump on me .


I didn't think David was "jumping" on you, but more like trying to get you to think it out for yourself.

Al Razor wrote:
The above-mentioned String (" Machine") was temporary used for concatination.
I thought that, if the string was not explicitly allocated using the "new" word or assigned to some reference variable, JVM would not create a separate object for it, but would create the object with "Virtual Machine" at once.


There is mechanism is the compiler that does this. If you do concatenation, when it is clear that what the results will be, the compiler will generate code that does not include the temporary strings.

However, for that mechanism to work, it has to be compile time constants, which in this case, are not.

Henry


I was just joking (hence, the smile at the end of the sentence).

Thanks for your comment about constants.
roi yal
Greenhorn

Joined: Mar 09, 2010
Posts: 4
I agree up,look java api for String, it's final!!!
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

DELETED wrote:

As already mentioned to you earlier in this thread, Please edit your name to use a real first and last name. See the Naming Policy for more details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple question about Strings in memory