Sie sind auf Seite 1von 4

Android Training Java Basics

PASS BY VALUE VS. PASS BY REFERENCE


In java all primitive type variables are always passed by value and variables of type class, interface or array are always passed by
reference.
If a variable is passed by value, any modification done to the parameter does not effect the value of the variable used for calling
the method. Only variable of primitive type can be passed by value.
If a variable in passed by reference then any modification done to the state of the object, then the changes will be visible in the
state of the object at the calling place. Only class, interface or array type variables can be passed by reference.
For example look into the following programs
Example 1
class A
{
void meth(int x)
{
x *= 2;
// x = x* 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
int y = 2;
System.out.println(y = + y);
a.meth(y);
System.out.println(y = + y);
}
}
This is an example of pass by value. Value of variable y is copied into the parameter x, Hence any
modification done to variable x it does not effects the value of variable y.

Example 2
class A
{
int x;
void method(int x)
{
x *= 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a.x);
System.out.println(a.x = + a.x);
}
}
This is also an example of pass by value. The datatype of a.x is int which is a primitive type.

Android Training Java Basics


Example 3
class A
{
int x;
void method(int x)
{
this.x *= 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a.x);
System.out.println(a.x = + a.x);
}
}
This is also an example of pass by value. The datatype of a.x is int which is a primitive type. But here the
value of the instance variable is getting changed as this represents the current object using whose
reference we are calling the method. Hence a.x is getting updated.

Example 4
class A
{
int x;
void method(int x)
{
x = 2 *this.x;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a.x);
System.out.println(a.x = + a.x);
}
}
This is also an example of pass by value. The datatype of a.x is int which is a primitive type.

Example 5
class A
{
int x;
int method(int x)
{
x *= 2;
return x;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
int y = a.method(a.x);
System.out.println(y = + y);
}
}
If a method has any other return type other than void then unconditional return statement is
compulsory. This is again an example of Pass by value and return by value.

Android Training Java Basics


Example 6
class A
{
int x;
void method(A t)
{
x *= 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a);
System.out.println(a.x = + a.x);
}
}
This is an example of pass by reference. The datatype of a is class type which is a reference type.

Example 7
class A
{
int x;
void method(A t)
{
t.x = x* 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a);
System.out.println(a.x = + a.x);
}
}
This is an example of pass by reference. The datatype of a is class type which is a reference type.

Example 8
class A
{
int x;
void method(A t)
{
t = new A();
t.x = x * 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a);
System.out.println(a.x = + a.x);
}
}
This is also an example of Pass by Reference. But here the reference in t is modified within the method.
Since the reference modified is local, any changes done to t.x now will not effect the value at the calling
place.

Android Training Java Basics


Example 9
class A
{
int x;
void method(A t)
{
t = null;
t.x = x * 2;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
a.method(a);
System.out.println(a.x = + a.x);
}
}
This code complies but fails at runtime. As t is referring to null t.x does not exists at runtime.

Example 10 Return By Reference


class A
{
int x;
A method()
{
A t = new A();
t.x = x * 2;
return t;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
A b = a.method();
System.out.println(b.x = + b.x);
}
}
This is an example of return by Reference.

Example 11 Return By Reference


class A
{
int x;
A method()
{
return this;
}
}

class B
{
public static void main(String args[])
{
A a = new A();
a.x = 2;
System.out.println(a.x = + a.x);
A b = a.method();
System.out.println(b.x = + b.x);
}
}
This is an example of return by Reference. this is a object reference literal and it refers to the object using
whose reference we have called the method. After the call to the method a and b refers to the same
object

Das könnte Ihnen auch gefallen