Call by value Vs Call by Reference

Call by value Vs Call by Reference:

Call By Value Vs Call By reference

Call By Value Vs Call By Reference

Call by value:

public class CallBy_Value
{
public static void main(String[] args)
{
Value v = new Value(10,20);
System.out.println("a and b before call............");
System.out.println("a = "+v.a);
System.out.println("b = "+v.b);
v.call(v.a,v.b); // CALL BY VALUE
System.out.println("a and b after call............");
System.out.println("a = "+v.a);
System.out.println("b = "+v.b);
}
}


class Value
{
int a,b;
Value(int i,int j)
{
a = i ;
b = j;
}
void call(int a, int b)
{
a = a * 2;
b = b * 2;
}
}

Call by Reference:

public class CallBy_Reference
{
public static void main(String[] args)
{
Reference r = new Reference(10,20);
System.out.println("a and b before call............");
System.out.println("a = "+r.a);
System.out.println("b = "+r.b);
r.call(r); // CALL BY REFERENCE
System.out.println("a and b after call.............");
System.out.println("a = "+r.a);
System.out.println("b = "+r.b);
}
}


class Reference
{
int a,b;
Reference(int i,int j)
{
a = i ;
b = j;
}
void call(Reference r)
{
r.a = a * 2;
r.b = b * 2;
}
}

Learn With theory and output of above examples.
Call by value in java and Call by Reference in java

Advertisements
Posted in Core Java | Tagged , , | Leave a comment

Constructor overloading in java

Constructor overloading in java

 

using System; 

public class CParent 
{ 
	public int PubA; 
	private int PriA; 
	protected int ProA; 
	
	public CParent() 
	{ } 
	
	public CParent(int a, int b, int c) : this() 
	{ 
		this.PriA = a; 
		this.ProA = b; 
		this.PubA = c; 
	} 
} 

public class CChild : CParent 
{ 
	public int pubB; public CChild() //First calls parent class constructor. 
	{ } 

	public CChild(int a, int b, int c, int d) : base(a, b, c) 
	{ 
		//this.PriA = a; 
		this.ProA =b; 
		this.PubA = c; 
		this.pubB = d; 
	} 
	
	public void Foo() 
	{ 
		CParent p = new CParent(); 
		CChild c = new CChild(); 
		ProA = 10; //Allowed 
		c.ProA = 10; //Allowed 
		//p.ProA = 10; //Compilation Error because “p” is of type parent. 
	} 
} 

class Program 
{ 
	static void Main(string[] args) 
	{ 
		CChild b; b = new CChild(1, 2, 3, 4);
		b.Foo(); 
	} 
}
Constructor overloading in java

Constructor overloading in java

Output :

–Center at 5 and 5
Radius = 3
–Center at 10 and 20
Radius = 5
–Center at 15 and 25
Radius = 10

 

Learn Constructor overloading in java with theory and examples.

Posted in Core Java | Tagged , , | Leave a comment