AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-programming: Java supports pass by value or pass by reference or both?

Please explain java supports pass by value or pass by reference or both.

java x 211
programming x 169
Posted On : 2014-08-18 17:02:37.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250050
up-rate
9
down-rate

Answers


Java supports only to pass everything by value, and not by reference and everything means everything like- objects, arrays (which are objects in Java), primitive types (like ints and floats), etc. These all are passed by value in Java.

When passing an argument (or even multiple arguments) to a method, Java will create a copy or copies of the values inside the original variable(s) and pass that to the method as arguments - and that is why it is called pass by value. The key with pass by value is that the method will not receive the actual variable that is being passed - but just a copy of the value being stored inside the variable. So, how does this affect the code you write? Well, this is best illustrated by a simple and easy to understand example.

Example of pass by value in Java:
-------------------------------------------------------
Suppose we have a method that is named "Receiving" and it expects an integer to be passed to it:

public void Receiving (int var)
{
var = var + 2;
}

Note that the "var" variable has 2 added to it. Now, suppose that we have some code which calls the method Receiving:

public static void main(String [] args)
{
int passing = 3;

Receiving (passing);

System.out.println("The value of passing is: " + passing);
}

In the main method, we set the variable "passing" to 3, and then pass that variable to the Receiving method, which then adds 2 to the variable passed in.

What do you think the "System.out.println" call will print out?

Well, if you thought it would print out a "5" you are wrong. It will actually print out "3". Why does it print out a 3 when we clearly added a 2 to the value in the Receiving method?

The reason it prints out "3" is because Java passes arguments by value- which means that when the call to "Receiving" is made, Java will just create a copy of the "passing" variable and that copy is what gets passed to the Receiving method- and not the original variable stored in the "main" method. This is why whatever happens to "var" inside the Receiving method does not affect the "passing" variable inside the main method.

Lat´s take another example:
-----------------------------------------------

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Max"); // true

public void foo(Dog d) {
d.getName().equals("Max"); // true
d = new Dog("Fifi");
d.getName().equals("Fifi"); // true
}


In this example aDog.getName() will still return "Max". d is not overwritten in the function as the object reference is passed by value.

Likewise:

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true

public void foo(Dog d) {
d.getName().equals("Max"); // true
d.setName("Fifi");
}


Java doesn´t support pass by reference to make it more secure.. Like it doesn't allow pointers explicitly

Posted On : 2014-08-18 17:22:50
Satisfied : 2 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
Reply This Thread
up-rate
5
down-rate

Yes, Java only supports pass by value.

For primitives, the value is passed and the original variable cannot be affected by the method being called.

However, the value that it passes for objects and arrays is a reference to that object or array. If that object is mutable, the method called can change the object. It can change the values in arrays passed as well. It can´t change the reference.

Posted On : 2014-08-20 21:32:27
Satisfied : 1 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939909
Reply This Thread
up-rate
2
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in