• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

reference problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As stated by Corey McGlone in his article on String Literals.The moment we write a statement like:

String one = "anything";

a reference to string "anything" is created.But in Thinking In Java,Bruce Eckel says that a reference is only generated when we define anything using "new" and only then the variable's reference is created on the heap.

The how is it that here a reference is created for the string and that too on the heap?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you're confusing the words "reference" and "object". If you replace the word "reference" in your paraphrase of Eckel with the word "object", then it's correct. In the sentence from Corey's article, yes, a reference is "created", although this is really not such an event as an object being created, which is what Bruce is actually concerned with.

An "object" is a real, physical lump of memory, whereas a "reference" is a pointer to such a lump -- a "name" for it. Objects are created on the heap when you use "new"; references don't need creating, and they can live either on the stack or in the heap.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya like:

int i = 1000;
int j = i;
 
Samarth Barthwal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for making the concept clear to me.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic