Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
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.
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.
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.
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