Sunday, 15 January 2012
JAVA : What is meant by pass by reference and pass by value in Java?
Do you like this Article?
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.
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: ");
doInt(i);
System.out.print(i);
}
public static void doInt(int i)
{
i--;
System.out.print(i+", ");
}
Output:
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.
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;
}
@Override
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){
objEmployee.setStrName("James");
objEmployee.setIntAge(22);
}
Then we will call this from a main method to test it, we
public static
void main(String[] args) {
Employee objEmployee = new Employee();
setEmployee(objEmployee);
System.out.println(objEmployee);
}
Output:
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.
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.
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
public - public means everyone can access it.That means it's in global scope. As, main method is called by JVM [ Java Virtual Machine...
-
throw is used to throw an exception in a program, explicitly . Whereas, throws is included in the method's declaration part, wi...
-
Singleton in one of the most popular yet controversial design pattern, in the world of object oriented programming. It's one of t...
-
Web Container / Servlet Container / Servlet Engine : In J2EE Architecture , a web container (also known as servlet container or ser...
-
Program compiles. But at runtime throws an error “NoSuchMethodError”.
-
Vector : It's synchronized. It's slower than ArrayList. It's generally used in ...
-
doGet(): protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException – is a met...
-
In Java Programming Language , we must declare a variable name and type, before using it. The data type of a variable defines the th...
1 Responses to “ JAVA : What is meant by pass by reference and pass by value in Java? ”
17 July 2012 at 18:14
kindly ,just explain it is in program ..
Post a Comment