• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in Creation of String Objects

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?
 
K. Tsang
Bartender
Posts: 3526
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation . Thanks.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3526
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic