Back to Top

Sunday, 15 January 2012

JAVA : What is meant by pass by reference and pass by value in Java?

Pass by reference means, passing the address itself rather than passing the value.
While, on the other hand, pass by value means passing a copy of the value.

Java, actually works as pass by value. Let’s check this with the help of 2 examples.
1.     By passing primitive data type:

In the below code, we have written a method called doInt(), which takes a int value and decrease the value by one and printing it there. Now, from main() method, we are passing the int i of value 10, then we are printing from main().

public static void main(String[] args) {
              int i= 10;
              System.out.print("OUTPUT: ");
       public static void doInt(int i)
              System.out.print(i+", ");

And the output of the above code is, 9 is printed from the doInt() method, whereas, it is not reflected in main method, because, it is still printing 10.
OUTPUT: 9, 10
Therefore, the conclusion over here is, it is passing the value, instead of reference.
2.     By Passing Object:
Now, the second example, by passing an object.
Say, we have a class named, Employee, which has 2 fields strName and intAge.

class Employee{
       private String strName;
       private int intAge;
       public String getStrName() {
              return strName;
       public void setStrName(String strName) {
              this.strName = strName;
       public int getIntAge() {
              return intAge;
       public void setIntAge(int intAge) {
              this.intAge = intAge;
       public String toString() {
              return "Employee [strName=" + strName + ", intAge=" + intAge + "]";
Now, we will create a method (with return type void), which will set the name and age of an employee object.

public static void setEmployee(Employee objEmployee){
Then we will call this from a main method to test it, we
public static void main(String[] args) {

       Employee objEmployee = new Employee();
And, in the output we can see the value of the attributes of employee object, is what we set in the setEmployee() method.
Employee [strName=James, intAge=22]

Therefore, it looks like the reference has been passed.
Yes, but still, it follows the concept of pass by value, because, the value of  objEmployee is, the memory reference of the actual object.

Therefore, we can say, Java only supports, Pass By Value, not, Pass By Reference.

3 Responses to “ JAVA : What is meant by pass by reference and pass by value in Java? ”

Padmanaban said...
17 July 2012 at 18:14

kindly ,just explain it is in program ..

Naviya Nair said...
24 October 2016 at 15:16

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai

riyan rt said...
5 July 2018 at 19:03

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Python Training in electronic city

DataScience with Python Training in electronic city

AWS Training in electronic city

Big Data Hadoop Training in electronic city

Devops Training in electronic city

Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs