• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question about Strings in memory

 
Al Razor
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Al Razor
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it "temporary"? It's an object, you created it. It has to exist *somewhere*, yes?
 
Al Razor
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Al Razor
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree up,look java api for String, it's final!!!
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic