0

As the title states, when does a method alter the value of a variable/argument of the parameter? ex:

public void someMethod(int a, int b){
a = 5;
b = 6;
}

//.. imagine a main method is written here..
{
int x = 23;
int y = 14;
someMethod(x,y);
System.out.println(x + " " + y);

}

When this is compiled, the result is 23 14 being printed. However, when are the values of the variables modified?

I know that it can be modified by usage of mutators (assuming that the argument passed is an object), but are there any other ways?

Rahul
  • 15,979
  • 4
  • 42
  • 63
mudda yard
  • 11
  • 3

7 Answers7

3

Java is pass by value!

What this means in effect is that a copy of the value is made, which is what is passed to the function.

Where the confusion arises is when you pass an object to a method. In this case it is the reference to the object that is copied.

For example, given a object of type X with a attribute Y, this would be seen be the calling code:

public void doSomthing(X obj)
{
  obj.setY("some value");
}

But this would not be:

public void doSomthing(X obj)
{
  obj = new X(); 
}
Nick Holt
  • 33,455
  • 4
  • 52
  • 58
2

Integers are so-called primitive types. Passing the integer copies its value and assigns it to the parameter too.

But that value is not a reference to the actual data, but is the data itself.

So changes to the parameter in the function will affect the parameter (a), but not the argument passed in the calling function (x).

Please Note : When we talk about mutators, we are talking about modifying/updating the values of class level variables. The scope of the variable is class level.

So even when you pass some value to the mutator, the actual parameter value will remain unmodified, unless it is a non-primitive type.

If you wish to mutate the value of some primitive variable inside a method, the scope of the variable should be available outside the method as well and the method should be able to access that. Only then the changes to the variables will be effective outside as well.

Rahul
  • 15,979
  • 4
  • 42
  • 63
1

Primitives

x and y are local variables (and since they are passed by value), you'd need to store them outside of the method in order to notice any changes by someMethod().

int x;
int y;

public void someMethod(){
x = 5;
y = 6;
}

//.. imagine a main method is written here..
{
int x = 23;
int y = 14;
someMethod();
System.out.println(x + " " + y);

}

This will print "5 6" instead of "23 14". Primitives don't have references, that's why ;)

Objects

Objects are passed by reference:

//main method
{ 
    Dog dog = new Dog("Max");
    someMethod(dog);
    System.out.println(dog.getName()); // Yay, dog is named Fifi
}

void someMethod(Dog dog) {
    dog.setName("Fifi");
}
Menno
  • 12,175
  • 14
  • 56
  • 88
  • assuming that global variables won't be used :P – mudda yard May 02 '13 at 07:09
  • In that case you should use objects instead of primitives. Objects are passed by reference instead of value. – Menno May 02 '13 at 07:10
  • (my fault on not being specific enough on my first post, but I'm drowsy but need to study for this exam :P) I do understand that objects can be altered via using mutators (the setName method) but I'd like to know if there is any other way that the value of a variable can be altered without the usage of a mutator. (assuming no global variables are used too) – mudda yard May 02 '13 at 07:16
  • Well, in case `name` of `Dog` would be public, you could say `dog.name = "Fifi";` is not using any `set` methods. I think an `Integer` would behave in the same way as this `Dog`. Which you can easily set like an `int`: `Integer x = 5;`. Though I'm not 100% sure whether this is passed by value or reference, you should test this if interested. – Menno May 02 '13 at 08:47
1

Variables are passed by value in Java. So when you pass x and y, the values of x and y are passed into it, and internally whatever happens in the method is localised to it. Whereas x and y when printed the references contains the same values 23, 14.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
1

Whenever a method is called with some parameters passed, then at runtime it is decided, which method to be executed by matching the signature of the method.

You can refer the link fro more information about methods calling.

Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
0

Primitives can not be altered from the callers perspective.

Only (non-primitive) mutable object parameters can have their value(s) changed.

Eg, the Date object passed to this method:

public static void example(Date date) {
    date.setTime(0);
}

Will be altered when the caller resumes execution.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Integers, floats,doubles etc are not objects and so are never modified by passing them into a method and then changing them within a method. However an object that is modified within a method will keep that change in all places it is refererenced. However a "replaced" change will not persit, see what I mean below:

public class Test{
    int x;



    public Test(int x) {
        this.x = x;
    }
    public void setX(int x){
        this.x=x;
    }
    public int getX(){
        return x;
    }


    public static void main(String[] args){

        Test test=new Test(5);
        System.out.println(test.getX()); //its 5

        testChange(test); 
        System.out.println(test.getX()); //its 7 because the change persists

        testChange(test); 
        System.out.println(test.getX()); //its still 7! Because we changed the object for a new one within the method, we didn't modify it

    }

    private static void testChange(Test aToBeChanged){
        aToBeChanged.setX(7);
    }
    private static void testReplace(Test aToBeReplaced){
        aToBeReplaced=new Test(10);
    }
}
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77