Swap Program demonstrating Call by Reference in Java

In Java When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-by-reference.

//Program demonstrating Call by Reference in Java

SwapCallByRefDemo.java

class Test

{

                int a, b;

                Test(int i, int j)

                {

                                a=i;

                                b=j;

                }

                void swap(Test ob)

                {

                                int c;

                                c=ob.a;

                                ob.a=ob.b;

                                ob.b=c;

                                System.out.println(“Inside swap method”);

                                System.out.println(“a = “+ob.a+” b = “+ob.b);

                }

}

class SwapCallByRefDemo

{

                public static void main(String[] args)

                {

                                Test ob = new Test(2,3);

                                System.out.println(“Before Swapping”);

                                System.out.println(“ob.a = “+ob.a+” ob.b = “+ob.b);

                                ob.swap(ob);

                                System.out.println(“After Swapping in main method”);

                                System.out.println(“ob.a = “+ob.a+” ob.b = “+ob.b);

                }

}


Swap Program demonstrating Call by Value in Java

//Program demonstrating Call by Value in Java

SwapCallByValDemo.java

class Test

{

                void swap(int a, int b)

                {

                                int c;

                                c=a;

                                a=b;

                                b=c;

                                System.out.println(“Inside swap method”);

                                System.out.println(“a = “+a+” b = “+b);

                }

}

class SwapCallByValDemo

{

                public static void main(String[] args)

                {

                                Test ob1 = new Test();

                                int x, y;

                                x=2;

                                y=5;

                                System.out.println(“Before Swapping”);

                                System.out.println(“x = “+x+” y = “+y);

                                ob1.swap(x,y);

                                System.out.println(“After Swapping in main method”);

                                System.out.println(“x = “+x+” y = “+y);

                }

}

Java Program Showing Stack using Array

 Stack is a linear data structure – it is linear in the vertical.

Here are some important points about Stack DS

 It represents a linear collection which is visualized in the vertical

 Bottom end of the stack is fixed.

Top end of the stack is operational.

 Process of adding a node in the collection represented by stack is called ‘PUSH’ operation.

Process of deleting a node from the collection represented by stack is called ‘POP’ operation.

Node PUSH-ed last is POP-ed first. Stack follows LIFO (Last In First Out) protocols in both PUSH and POP processes and in the implementations of its both size related specifications.

//Program demonstrating Stack in Java

StackDemo.java


//Program demonstrating Stack in Java

class Stack
{
	private int tos;
	private int stk[];

	Stack(int size)
	{
		tos=-1;
		stk=new int[size];
	}

	void push(int item)
	{
		if(tos==stk.length-1)
			System.out.println("Stack is Full");
		else
		{
			stk[++tos]=item;
			System.out.println(stk[tos]);
		}
	}

	int pop()
	{
		if(tos<0)
		{
			System.out.println("Stack Underflow");
			return 0;
		}
		else
			return stk[tos--];
	}

}

class StackDemo
{
	public static void main(String[] args)
	{
		Stack s1 = new Stack(5);

		System.out.println("Data Pushed is : ");
		for(int i=0; i<5; i++)
			s1.push(i);

		System.out.println("\nData Popped is : ");
		for(int i=0; i<5; i++)
		System.out.println(s1.pop());

	}
}

Screenshot of the o/p

StackDemo