aspose file tools*
The moose likes Beginning Java and the fly likes Difference in Creation of String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference in Creation of String Objects " Watch "Difference in Creation of String Objects " New topic
Author

Difference in Creation of String Objects

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Hi



I Know the difference between these two , but is there any proof that the second Line will be definitely creating an Object inside a Heap ?


Save India From Corruption - Anna Hazare.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

You want to find that out yourself. Do a equals test and a "==" test. The "==" should return false. The only reason the "==" returns true for Strings is that they are pointing to the "same" object.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Nice explanation . Thanks.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
K. Tsang wrote:You want to find that out yourself. Do a equals test and a "==" test. The "==" should return false. The only reason the "==" returns true for Strings is that they are pointing to the "same" object.


I thought about that, but I couldn't say whether or not this constituted proof of an object being created "inside a heap". I'm not to clear on that aspect.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Fred Hamilton wrote:I thought about that, but I couldn't say whether or not this constituted proof of an object being created "inside a heap". I'm not to clear on that aspect.


Hmm maybe the K&B SCJP excerpt (ch 3 OO) will convince:
For the most part, the various pieces (methods, variables, and objects) of Java
programs live in one of two places in memory: the stack or the heap. For now, we're
going to worry about only three types of things: instance variables, local variables,
and objects:
* Instance variables and objects live on the heap.
* Local variables live on the stack.


So what is String? a object so where is it created? on the heap. And in my code, how many string objects? 1, 2, or 3?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference in Creation of String Objects