24
public class program1{

    public static void main(String args[]){

        java.util.Vector vc=new java.util.Vector();

        vc.add("111");
        vc.add("222");

        functioncall(vc);

        vc.add("333");

        System.out.println(vc);

    }

    public static void functioncall(java.util.Vector vc){     

        vc=null;    

    }
}

The output of above program is [111,222,333]. but, when I run the following program the output is [333]. Confused when we pass an reference , how it works whether it is call by value or call by reference? and why

public class program1{

    public static void main(String args[]){

        java.util.Vector vc=new java.util.Vector();

        vc.add("111");
        vc.add("222");

        functioncall(vc);

        vc.add("333");

        System.out.println(vc);

    }

    public static void functioncall(java.util.Vector vc){

        vc.removeAllElements();  

    }
}
Tharwen
  • 3,057
  • 2
  • 24
  • 36
user617597
  • 788
  • 3
  • 9
  • 21
  • 2
    why negative vote?? dont understand.. – user617597 May 25 '12 at 07:21
  • 1
    I was tempted to down-vote for that "dog's breakfast" of code indenting, but let it slide. I **suspect** it was down-voted by someone who feels there are many duplicates. Check the 'Related' threads shown in the right hand column for better hits now the [tag:pass-by-reference] & [tag:pass-by-value] tags have been added. – Andrew Thompson May 25 '12 at 07:24
  • 3
    Because it has been asked a million times already, which a search engine would quickly show. Now there's gonna be endless debates again, because most people are wrongly convinced that it's call by reference. If it were call by reference then your first example would throw NullPointerException. – RokL May 25 '12 at 07:25
  • The vector is just a container of the numbers.So in your first example the function functioncall() just set's the variable vc to null, but the container itself still exists in main. In the second function you tell the container to empty itself. It's still the same container and no copy of the contend are made. Java is always pass by value, never pass by reference. – R. van Twisk Nov 09 '13 at 17:35

11 Answers11

24

It passes the value of the reference.

To shamelessly steal an analogy I saw posted on here a while ago, imagine that each identifier you use is a piece of paper with an address written on it. The address points to a house.

You can change the house (for example, by adding objects to the vector or clearing it), but you're still holding the same piece of paper, and the address still takes you to the same house.

If you set the vector to null, all you're doing is rubbing out the address.

This article explains it in much more detail.

Tharwen
  • 3,057
  • 2
  • 24
  • 36
3

You pass vc as a reference copy (always). Then doing vc = null; or vc = new Vector(), you just modify the reference from the vc local attribute and so it's normal that the main one didn't change.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
2

It is call by value. In both cases you put value of reference to reference in method's argument which is local reference of method.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

Java programming language always uses call by value. In Java, all parameters to methods are call by value or pass by value. Cay S. Horstmann and Garry Cornell have mentioned in their very famous book "Core Java Volume - I Fundamentals" that the Java programming language always uses call by value. That means the method gets a copy of all parameter values, and the method cannot modify the contents of any parameter variables that are passed to it. Java uses two kinds of method parameters:

  • Java primitive types
  • Java object references

It looks very straight forward and simple when you experiment passing primitive types to a method but becomes obscure when it comes to passing objects to a method. Interestingly, when an object reference is passed to a method, the method gets a copy of the object reference, and both the original and the formal copy refer to the same object, therefore within from the method the state of an object parameter can be changed.

Following article explains well Call by value and call by reference.

Krishan
  • 317
  • 4
  • 8
1

Java uses Object references. The argument is reference value. So it's call by value, where value is a reference for objects.

RokL
  • 2,663
  • 3
  • 22
  • 26
1

vc is a new variable which contains reference of the vector that was used to call the method. Changing it to null does not affect the original vector because this reference is copy of original vector reference.

But since this is a reference to original vector any modification to vector actually changes the original vector. So java always uses call by value, here the value happens to be a reference.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
1

Java and C is always call by value. The term call by reference strictly applies to C++ where we use the & operator in the formal argument. In case of object references the references are copied from the actual to the formal argument.

arnabkaycee
  • 1,634
  • 13
  • 26
1

Pass by value in java means passing a copy of the value to be passed. Pass by reference in java means the passing the address itself. In Java the arguments are always passed by value. Java only supports pass by value.

With Java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Java primitives too are passed by value.

0

With Java "pass by reference", the reference itself is passed by value.

So, you cannot change the reference itself, but you can change the object pointed to by the reference.

So your removeAll call acts on the Vector so you see the results. However if you change the reference itself like :

vc = null

or,

vc = new Vector();

Those changes point to a new ( or null ) object, so any changes after that will not be reflected in the object in main

Chip
  • 3,226
  • 23
  • 31
  • "As usual with any pass by reference..." That's not what "pass-by-reference" means at all. That's pass-by-value. In true pass-by-reference, e.g. in C++, you can pass a variable into the function, and the function can assign to that variable *as if* it was assigned in the calling scope. That's "as usual" with pass by reference, and is not possible in Java. – user102008 May 25 '12 at 17:55
  • @user102008 Ok.. I edited it. You're right.. my answer sounded like I was talking about all languages. – Chip May 25 '12 at 18:14
0

Java works withs "Call by Value concept". If its stack and heap is visualized then java tries to find the values of any variable in local workspace, if its not found in local then it tries to find out in object which is in heap.

Example

class Foo
{

int x;

public void call(int x)

{

    x++;

}

public static void main(String[] args)

{

       Foo foo = new Foo();

       foo.x = 5;

       System.out.println(foo.x);

       foo.call(foo.x);

       System.out.println(foo.x); 


}


}

Output of above program would be : 5 , 5 Desciption : In main method, value of x is assigned 5 on reference of "foo: In call method, there is local variable named "x"(passed as argument) in workspace. so it's value would be changed in its workspace only. when control from this function returns to main method. In main's workspace value of "x" is still 5.

Example

class Foo

{

int x;

public void call(int y)

{

    x++;

}

public static void main(String[] args)

{

       Foo foo = new Foo();

       foo.x = 5;

       System.out.println(foo.x);

       foo.call(foo.x);

       System.out.println(foo.x); 

}


}

Output of above program would be : 5 , 6

Desciption : In main method, value of x is assigned 5 on reference of "foo: In call method, there is no local variable named "x"(passed as argument) in workspace. So java finds it in reference through which "call" function was called and value of "x" is there 5, call method increments its value to "6" so it's value would be changed reference i.e. "foo". when control from this function returns to main method. Now In main's workspace value of "x" is 6 because here we printed "x" on foo reference.

I hope this would help you to clear your concepts.

Regards, Sushil Jain

Brett Gregson
  • 5,867
  • 3
  • 42
  • 60
Sushil Jain
  • 477
  • 1
  • 5
  • 13
0

Pass-by-value

The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.

Pass-by-reference The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter. Java is strictly pass-by-value