This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reference problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "reference problem" Watch "reference problem" New topic
Author

reference problem

Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Tony pony
Greenhorn

Joined: Mar 18, 2006
Posts: 2
Ya like:

int i = 1000;
int j = i;


Need java video tutorials?<br />http://arkserver.servehttp.com/index.php?board=311.0<br />not mine, just pointing
Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
Thank you for making the concept clear to me.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: reference problem
 
Similar Threads
Number of Object Created?
How many String objects ... ?!
String literal Pool doubt
Garbage collect with String Objects
Constant String Pool