29/04/2008
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.














Java is strictly ‘Pass By Value’
Abhishek Khandelwal on November 3rd, 2009 9:30 pmFor Java newbies, this explanation on java pass by reference can be confusing. It would be
better to explain by example. The reason is because with Java
primitives, the results are different than with objects.
The problem comes in because, for primitives, the value of the
argument can be altered (in the method), but it doesn’t affect the
source variable. With an object reference being passed, that
object can be operated on (in the method) and its values changed.
Big difference.
mjt on December 17th, 2009 9:52 amvery good explanation on pass by value and pass by reference in java. thanks.
shiv on March 22nd, 2010 9:29 amIf the callee function can change the object’s values passed, then it should be called as pass by reference right?
Manoj on June 8th, 2010 6:19 pmYes Manoj. You are exactly right. If the passed object’s state can be changed so that it gets reflected in the caller method then it is called pass by reference. Java doesn’t has pass by reference. It is strictly pass by value.
Joe on June 8th, 2010 7:03 pmHi Moderator/Owner could you please explain a bit more, the answers here sounds confusing.
Thanks
Vishal on June 15th, 2010 11:04 pmGot the answer here..
CHeck this out.
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Vishal on June 15th, 2010 11:07 pmI WANTS TO KNOW THE OOPS CONCEPTS IN JAVA
RAGASUTHA on June 24th, 2010 6:28 amExplanation between pass by value and java pass by reference is confusing. can you give a sample java source code?
Priya on July 16th, 2010 6:56 pmjava does not support pointers, thus anything is always passed by value.
jeRrRKKKK on July 20th, 2010 1:21 pmYes There is pass by reference in java..
Anonymous on July 26th, 2010 5:14 amReference are passed by value to the function is nothing but pass by reference…. but the primitives are always passed by value
@Anonymous – everything in java is pass by value. NOTHING is pass by reference in java. Even the references are passed by value only.
Kindly read the whole article. Its simple and small!!
Joe on July 26th, 2010 5:42 pmGOOD
ssa on September 29th, 2010 12:50 pmGood Explanation to understand the concept.
Mathur on December 1st, 2010 5:53 amCAN U ABLE TO PROVIDE ME AN EXAMPLE THAT GIVES A DIFFERENCE BETWEEN PASS BY VALUE AND PASS BY REFERNCE
gopinath on December 2nd, 2010 12:40 pmDo more clear bcoz it is not more clear.Difference between Pass by value and Pass by Reference.
Arti Srivastav on March 16th, 2011 6:51 amPlease Keep One Example Program….
Why Java Does’nt Support pointers ????
RaamNaresh Reddy on April 29th, 2011 7:15 amthank you for your explanation
praveen on May 6th, 2011 8:46 amHere is the sample program
public class TestPassByReference {
/**
* @param args
*/
public static void main(String[] args) {
Reference ref = new Reference();
System.out.println(“The reference of ref=” + ref);
System.out.println(“The reference of ref=” + ref.name);
getValue(ref);
System.out.println(“The reference of ref=” + ref.name);
System.out.println(“The reference of ref=” + ref.getName());
}
public static void getValue(Reference r) {
r.setName(“Prashant”);
}
public static class Reference {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void Reference(String name) {
Anonymous on May 11th, 2011 10:28 pmthis.name = name;
}
}
}
[...] Programming Languages’, a first class object can be stored in a data structure, passed as a parameter, can be returned from a function, can be constructed at runtime and independent of any [...]
Java Closures&hellip on May 12th, 2011 12:08 amwhat is the pass by reference and pass by value and pass by address
azad on August 3rd, 2011 2:59 pmPassing by value: This method copies the value of an argument into the formal parameter of the subroutine.
Shanmukesh on August 26th, 2011 9:03 pmPassing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.
An arraylist and other types of collections do not neccesarly contain objects but it contains refrences to Objects, so you can use an ArrayList to pass refrences in Java, example:
public class Book {
String title;
public Book(String title){
this.title = title;
}
public void setTitle(String newTitle) {
this.title = newTitle;
}
public String getTitle() {
return this.title;
}
}
public class Example {
public static void main(String[] args){
Example example = new Example();
ArrayList bookList = new ArrayList();
bookList.add(0, new Book(“First book”));
bookList.add(1, new Book(“Second book”));
bookList.add(2, new Book(“Third book”));
//we pass an object containing refrences :)
example.demonstrate(bookList);
//proof that its not only pass by value
System.out.println(bookList.get(0).getTitle());
}
public void demonstrate(ArrayList aBookList){
Sayf on September 15th, 2011 11:30 amaBookList.get(0).setTitle(“Changed the First title by refrence”);
}
}
ljknjn
m on September 29th, 2011 6:52 amvery precise, and to the point, thanks a lot
mohammad on October 17th, 2011 3:15 pmi want to become a great java developer,also want to be master in java program.can u suggest some sites to learn java code properly.
parthi on October 21st, 2011 5:27 amThx you a lot, it’s clear.
Abdelhamid on November 2nd, 2011 6:39 pmAs I understand, Java is strictly supports pass by value .
If variable is passed as argument inside a method, copy of the value is passed. So, it will not affect the original value of variable (for primitive types).
But, if object reference is passed, copy of the value of memory address is passed. So, we able to change the value of an object.
Joe, Is my understanding right?
muthu on November 11th, 2011 6:00 am**muthu
jimshad on November 19th, 2011 4:58 amu r ryt…and dis was d correct explanation for d new ones…
Hy Iam Hacker
asdasd on November 22nd, 2011 7:13 amHello Guyz
asdasd on November 22nd, 2011 7:14 amits mean pass by reff actually using value of that reff or one copy of reff of object. m i right??
pallav rajput on December 23rd, 2011 2:22 pmI found this over the net. Quite a nice explanation.
This will give you some insights of how Java really works to the point that in your next discussion about Java passing by reference or passing by value you’ll just smile :-)
Step one please erase from your mind that word that starts with ‘p’ “_ _ _ _ _ _ _”, especially if you come from other programming languages. Java and ‘p’ cannot be written in the same book, forum, or even txt.
Step two remember that when you pass an Object into a method you’re passing the Object reference and not the Object itself.
Student: Master, does this mean that Java is pass-by-reference?
Master: Grasshopper, No.
Now think of what an Object’s reference/variable does/is:
A variable holds the bits that tell the JVM how to get to the referenced Object in memory (Heap).
When passing arguments to a method you ARE NOT passing the reference variable, but a copy of the bits in the reference variable. Something like this: 3bad086a. 3bad086a represents a way to get to the passed object.
So you’re just passing 3bad086a that it’s the value of the reference.
You’re passing the value of the reference and not the reference itself (and not the object).
This value is actually COPIED and given to the method.
In the following (please don’t try to compile/execute this…):
1. Person person;
2. person = new Person(“Tom”);
3. changeName(person);
4.
5. //I didn’t use Person person below as an argument to be nice
6. static void changeName(Person anotherReferenceToTheSamePersonObject) {
7. anotherReferenceToTheSamePersonObject.setName(“Jerry”);
8. }
What happens?
The variable person is created in line #1 and it’s null at the beginning.
A new Person Object is created in line #2, stored in memory, and the variable person is given the reference to the Person object. That is, its address. Let’s say 3bad086a.
The variable person holding the address of the Object is passed to the function in line #3.
In line #4 you can listen to the sound of silence
Check the comment on line #5
A method local variable -anotherReferenceToTheSamePersonObject- is created and then comes the magic in line #6:
The variable/reference person is copied bit-by-bit and passed to anotherReferenceToTheSamePersonObject inside the function.
No new instances of Person are created.
Both “person” and “anotherReferenceToTheSamePersonObject” hold the same value of 3bad086a.
Don’t try this but person==anotherReferenceToTheSamePersonObject would be true.
Both variables have IDENTICAL COPIES of the reference and they both refer to the same Person Object, the SAME Object on the Heap and NOT A COPY.
A picture is worth a thousand words:
Pass by Value
Note that the anotherReferenceToTheSamePersonObject arrows is directed towards the Object and not towards the variable person!
If you didn’t get it then just trust me and remember that it’s better to say that Java is pass by value. Well, pass by reference value. Oh well, even better is pass-by-copy-of-the-variable-value! ;)
Now feel free to hate me but note that given this there is no difference between passing primitive data types and Objects when talking about method arguments.
You always pass a copy of the bits of the value of the reference!
If it’s a primitive data type these bits will contain the value of the primitive data type itself.
If it’s an Object the bits will contain the value of the address that tells the JVM how to get to the Object.
Java is pass-by-value because inside a method you can modify the referenced Object as much as you want but no matter how hard you try you’ll never be able to modify the passed variable that will keep referencing (not p _ _ _ _ _ _ _) the same Object no matter what!
The changeName function above will never be able to modify the actual content (the bit values) of the passed reference. In other word changeName cannot make Person person refer to another Object.
Of course you can cut it short and just say that Java is pass-by-value!
venu on January 18th, 2012 11:02 amNot at all good explanation. I had to search on other sites / forum to get more detailed understanding.
Please elaborate with examples, like you do in other posts
Java Learner on February 8th, 2012 4:14 pmgood explanation of pass by value and pass by reference.
satish kumar yadav on February 16th, 2012 7:08 pmCan U explain pass By value and pass By reference with example? I have in confusion so.
Thanks in advance.
Sadik on February 21st, 2012 10:45 amPlease give easy example of pass by reference like swapping of a two number using pass by reference
Anonymous on March 7th, 2012 4:40 pmvery useful!!!
udhay on March 9th, 2012 6:01 pmit was up to mark.
Anonymous on March 15th, 2012 4:56 pmexcellent!!!!!!!
Thanks dude for your help in java
MamoonRazmal on March 18th, 2012 2:47 pmBut i also don’t understand the concept of”
Stack unwinding exception”in java
finally in java call-by-reference is not possible is it right?
ankush on March 21st, 2012 10:08 pmYes
Azhagumuthu on March 30th, 2012 4:29 pmExplanation is good.. Brief explanation about the topic from venu is also good.
Prabhakaran. N on April 10th, 2012 5:55 pmyour blog inter face is very nice i like it…
Anonymous on April 12th, 2012 9:56 pmThanx to Venu for this beautifull explanation.
Su on April 28th, 2012 11:06 am@Joe: Can you please update your comment section functionality so that we can write reply to the specific comment. :)
Thanks , it’s very useful and nice.
Sujay Mandal on April 28th, 2012 12:06 pmkindly give some simple examples of link list in Java
vinit saxena on May 15th, 2012 8:34 amExplain the statement ‘java is strictly pass by value’ consider the below piece of code:
import java.util.*;
public class A{
public static void main(String args[]){
ArrayList a=new ArrayList();
a.add(“1″);
System.out.println(a);
somemethod(a);
System.out.println(a);
}//main
public static void somemethod(ArrayList a){
a.add(“2″);
}
}//class
The output of this code will be:
[1]
[1, 2]
Since a reference to the arraylist was passed into the method….no?
Jay on May 24th, 2012 8:40 pmString s=”my name is nani”
nanaji on May 30th, 2012 11:49 amhow can pass a string in arraylist