wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Creating reference to a primitive - Roberts-Heller's cert guide? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating reference to a primitive - Roberts-Heller Watch "Creating reference to a primitive - Roberts-Heller New topic
Author

Creating reference to a primitive - Roberts-Heller's cert guide?

divyasheel sharma
Greenhorn

Joined: Feb 15, 2001
Posts: 19

How to create reference to a primitive :
This is from Page 21 RH Guide for JCP Exam,
I couldn't make out what this means:
Can someone explain the code for me please :
public class PrimitiveReference {
public static void main(String args[]) {
int[] myValue = {1};
modifyIt(myValue);
System.out.println("myValue contains"+myValue[0]);
}
public static void modifyIt(int [] value) {
value[0]++;
}
}
Thanks
Divya

Eat Java,Drink Java...surf only Javaranch!
Scott Pedigo
Greenhorn

Joined: Feb 15, 2001
Posts: 15
I think the point that the example is trying to illustrate is that a copy of the *reference* to the integer array is passed into the method on the stack, not a separate copy of the array itself. So when in the method the value of the array element is changed, the original is changed. If a copy of the array contents were passed in on the stack, then only the copy on the stack would be changed, and the original would remain unchanged (which is not the case). The output should therefore show the change.
divyasheel sharma
Greenhorn

Joined: Feb 15, 2001
Posts: 19
Hi Steve!
But how do I know that its the copy or the separate copy that is passed to the method?
Please explain bit more.
thanx
Divya
divyasheel sharma
Greenhorn

Joined: Feb 15, 2001
Posts: 19
Hi Steve!
But how do I know that its the copy or the separate copy that is passed to the method?
Please explain bit more.
thanx
Divya
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
When an array is passed into a method it is a reference, not a copy of a reference.
HTH
Pat B.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Primitives always pass a copy of the primitive. Objects always pass a copy of the reference to the object.
myValue holds a reference to the array (that is an object). modifyIt(myValue) is taking in a copy of that reference.

This campfire story explains it in such amuzing detail:
http://javaranch.com/campfire/StoryPassBy.jsp
[This message has been edited by Cindy Glass (edited February 16, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
divyasheel sharma
Greenhorn

Joined: Feb 15, 2001
Posts: 19
Thanks Cindy!
I shall check up with campfire!
Rgds,
Divya
------------------
Eat Java,Drink Java...surf only Javaranch!
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
If you have any doubts, just compile/run the code...you will see that the output is
myValue contains 2

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
divyasheel sharma
Greenhorn

Joined: Feb 15, 2001
Posts: 19
Thanx Cindy!
Campfire stories are great for understanding, I shall try out more on this and if I have doubts I shall come back.
Rgds
Divya

------------------
Eat Java,Drink Java...surf only Javaranch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating reference to a primitive - Roberts-Heller's cert guide?