RE: Passing Primitive Types "By Reference" In Java

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 4 Feb 2008 00:12:42 -0500

If you mean passing an int to a method and having that method change the
value at that location ... it's pretty much not possible.

You can use the Integer object instead, and then you'd be able to do it, but
just accepting Integer won't work, because autoboxing will simply promote
your int to an Integer and then it will simply demote it back down to an int
upon usage.

There might be a way to use and pass around Integer objects everywhere;
thus, keeping the new value but that would be interesting, to say the least.

What is it that you're trying to do? Can you simply return the new value
instead of expecting the function to change it's parameter, so then you can
simply do x = change(x)

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
Sent: Sunday, February 03, 2008 11:54 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Passing Primitive Types "By Reference" In Java

Hi Folks, I am familiar with Java's passing all parameters to methods
strictly by value. I am also familiar with how it always uses references to
indicate objects, and so passing objects by reference is easily accomplished
from a practical standpoint--the values of the references are actually
passed, but the end result is much the same. I'm unsure though as to what
would be the most effective way of passing a primitive type  to a method in
such a way that the method can modify the original variable. Thanks in
advance for any thoughts.

Jared
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: