Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Difference in Creation of String Objects

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation . Thanks.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic