Click to See Complete Forum and Search --> : int and Integer


Andrew McCallum
09-21-2000, 01:45 AM
I come from a C/C++ background.

I want to write a function where I pass in an int argument and the function
modifies the value. (in C, void func(int &arg1, int &arg2) )

From what I understand, basic types (int, long, char) are passed by value,
and Objects are passed by reference.

Therefore I can't use int, I should use an Integer object. But my function
doesn't seem to update the original object.


public void func(Integer arg1, Integer arg2, String arg3)
{
arg1 = Integer.valueOf(arg3);
arg2 = new Integer(arg1 + 1);
}

Also, I seems really hard to update the value of an Integer object. Shouldn't
there be a arg1.Set(int i) function somewhere?

Thanks,
Andrew

Nikhil
09-21-2000, 06:45 AM
Andrew,
Do you mind to clear the objective of your function.

Nikhil

"Andrew McCallum" <Andrew.McCallum@Objective.com> wrote:
>
>I come from a C/C++ background.
>
>I want to write a function where I pass in an int argument and the function
>modifies the value. (in C, void func(int &arg1, int &arg2) )
>
>From what I understand, basic types (int, long, char) are passed by value,
>and Objects are passed by reference.
>
>Therefore I can't use int, I should use an Integer object. But my function
>doesn't seem to update the original object.
>
>
>public void func(Integer arg1, Integer arg2, String arg3)
>{
> arg1 = Integer.valueOf(arg3);
> arg2 = new Integer(arg1 + 1);
>}
>
>Also, I seems really hard to update the value of an Integer object. Shouldn't
>there be a arg1.Set(int i) function somewhere?
>
>Thanks,
>Andrew

Paul Clapham
09-21-2000, 11:16 AM
All parameters to Java methods are passed by value. For primitive types
such as int, the value of the input parameter is passed. For objects, the
value of the input parameter is also passed, and this value is a reference
to the object. It is true that inside the method, you may change the
object, but you can only do that by calling one of the object's methods to
do that.

In your code, "arg2" receives a reference to an Integer object. The code
"arg2 = new Integer(arg1 + 1)" does not change that object. Instead, it
causes arg2 to refer to a newly created object. And it has no effect on the
input parameter, which continues to refer to the original object -- because
parameters are passed by value.

And no, you are correct that there are no functions that change the internal
state of an Integer object. You could create your own MutableInteger class
if you wanted.

The bottom line of this is, don't try to change parameters in Java methods.
This practice is of questionable taste in any programming language, as it
can lead to side-effects and inconsistencies. If you want to return
something from a method, let it be the result of the method:

public Integer func(Integer arg1, String arg3)
{
arg1 = Integer.valueOf(arg3);
return new Integer(arg1 + 1);
}

Andrew McCallum <Andrew.McCallum@Objective.com> wrote in message
news:39c9a079$1@news.devx.com...
>
> I come from a C/C++ background.
>
> I want to write a function where I pass in an int argument and the
function
> modifies the value. (in C, void func(int &arg1, int &arg2) )
>
> From what I understand, basic types (int, long, char) are passed by value,
> and Objects are passed by reference.
>
> Therefore I can't use int, I should use an Integer object. But my
function
> doesn't seem to update the original object.
>
>
> public void func(Integer arg1, Integer arg2, String arg3)
> {
> arg1 = Integer.valueOf(arg3);
> arg2 = new Integer(arg1 + 1);
> }
>
> Also, I seems really hard to update the value of an Integer object.
Shouldn't
> there be a arg1.Set(int i) function somewhere?
>
> Thanks,
> Andrew