开发者

Function didn't change the value - JAVA 6 SE

I开发者_运维问答 can't understand why the overloaded function 'increase' does not change Integer but does change Point. The propuse of 'Integer' class is to wrap int so it will be a reference Type.

import java.awt.Point;

public class test2 {
    public static void main(String[] args) {
        ///1
        Integer i = new Integer(0);
        increase(i);
        System.out.println(i);
        ///2
        Point p = new Point(0,0);
        increase(p);
        System.out.println(p);

    }

    public static void increase(Integer i){
        i = 1;
    }

    public static void increase(Point p){
        p.setLocation(1, 1);
    }
}

the output is :

0
java.awt.Point[x=1,y=1]

Also, is their a simple way to pass a variable to a function by reference in Java?


Integer class is an immutable class, that means its content can't be changed after it's created.

Also, Java is pass-by-value so the variable i is passed by value, and the fact that it changes inside the function has no effect on the caller.

Read here: http://en.wikipedia.org/wiki/Immutable_object for more information on immutable objects.


The simple answer is that Java uses pass by value, not pass by reference.

In the Point case, the method is changing a field of the point object whose reference was passed into the method.

In the Integer case, the method is simply assigning a new value to the local variable i. This does not update the variable i in the calling method, because Java uses pass by reference.

The other issue is that Integer has no setValue methods because it is immutable. If you want to do the equivalent of what the Point version of the method is doing, you will have to define an IntegerHolder class that has both a getter and a setter, together with methods such as increase, that your application needs. (Alternatively, find such a class in a 3rd party library.)


Integer objects are immutable, i.e. you can't change them. If you could, the syntax would be like

i.setValue(1);

If you want to pass a non-object by reference, you can either wrap it in an array of length 1 or (better) create a trivial wrapper. However, there is little reason to do so - don't port your code from C 1:1. Usually, you should have a semantically loaded object, like an Account on which you can call the increase and decrease (or maybe just setBalance) methods.


In this function:

public static void increase(Integer i){
    i = 1;
}

autoboxing makes this equivalent to:

public static void increase(Integer i){
    i = new Integer(1);
}

i.e. it changes the reference that i contains, not the value that it contains. The Integer object is itself immutable, there's actually no way to change the value of one after it has been created.

Since that reference is a local variable, any changes to it will not affect the variable that was passed in.


When you write i = 1, you are changing the i parameter to point to a new boxed Integer instance.
The original Integer instance that you passed to the function is not—and cannot be—changed—Integers are immutable


Answer here: http://www.javaworld.com/javaworld/javaqa/2000-06/01-qa-0602-immutable.html This reference could be useful: http://javadude.com/articles/passbyvalue.htm


If you wanted the two methods to be equivalent, the second one would look like this:

public static void increase(Point p){
    p = new Point(1, 1);
}

And then you would see that it outputs the original point here, too.

There is no pass a variable to a function by reference in Java.

You can simulate it by passing an object which contains the variable (like you did in your increase(Point) method) - you'll have to be sure to assign to the variable, though, not to the object containing the variable.

As said before, there are several "mutable wrappers" around (for example org.omg.CORBA.IntHolder and java.util.concurrent.AtomicInteger in the standard API), but it is not difficult to create your own, and in most cases it would be better to use a sensible "Business object" like an "Account" instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜