It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing a Primitive By reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing a Primitive By reference" Watch "Passing a Primitive By reference" New topic
Author

Passing a Primitive By reference

Whiz Kid
Greenhorn

Joined: Jul 02, 2002
Posts: 6
Thanks in advance for your replies
I dont clearly Follow how to pass a primitve data type by reference ?Can anyone explain this to me?
Thanks
Gowri
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
In Java, everything is passed by value. Primitives are copied and passed on to the method and references are copied also but they still reference the same object on the heap.
If you want to pass a primitive by reference, the only I know of is to wrap it in an array and pass the array to the method. For instance,


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Whiz Kid
Greenhorn

Joined: Jul 02, 2002
Posts: 6
Thanks so much.
have a good day
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi Valentin,
I'm surprised the name 'Whiz Kid' is acceptable with the naming policy and all.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It's not
I've already sent him an email and dropped my comments in two of his recent posts... I didn't want to spoil all threads with naming policy enforcement messages.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Sorry 'bout that Valentin, I'm just used to seeing the naming policy response in all the threads posted by weird names.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
No problem Paul, just don't hesitate to let me know, usually I look at all posts but some may slip through
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Val:
The following code:

Should it not be:

This code also provide same result as yours but
it proves the point you are trying to make. Or
am I off-the-base here?
Thanks
Barkat
[ August 18, 2002: Message edited by: Barkat Mardhani ]
[ August 18, 2002: Message edited by: Barkat Mardhani ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You're right Barkat. Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Passing a Primitive By reference