Wednesday, 21 October 2015

Java Thread example two


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second sec = new Second(1);
Thread t = new Thread(sec);
Thread t1 = new Thread(sec);
t.start();
t1.start();
}

}



public class Second extends Thread{
int avail=1;
int req;
Second(int i){
this.req=i;
}
public void run(){
synchronized(this){
if((avail>0)&&(req==1)){
System.out.println("Allocating the seat");
avail=req-1;
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
System.out.println("No Seats");
}
}
}
}


output:-
Allocating the seat
No Seats

Java Thread example

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second("I am a Tea cup");
Second obj1 = new Second("I am a Coffee cup");
Thread t = new Thread(obj);
Thread t1 = new Thread(obj1);
t.start();
t1.start();
}
}

public class Second implements Runnable {
String str1;
Second(String str){
this.str1=str;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
System.out.println("The value of string is " +str1);
}
    }
}

output:-
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Coffee cup
The value of string is I am a Tea cup
The value of string is I am a Tea cup
The value of string is I am a Tea cup

Thread implementation Runnable in java example

import java.io.IOException;


public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Second obj = new Second();
Thread t = new Thread(obj);
t.start();
System.in.read();
obj.i = true;
}

}



public class Second implements Runnable{
public boolean i = false;

@Override
public void run() {
// TODO Auto-generated method stub
for(int j=0;j<2000000;j++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("I am J " +j);
if(this.i)return;
}
}
}

output:-
I am J 0
I am J 1
I am J 2

I am J 3

Implementing threads in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Sec obj = new Sec();
Thread t = new Thread(obj);
t.start();
}

}


public class Sec extends Thread{

public void run(){
for(int i=0;i<10;i++){
System.out.println("The value of i is " +i);
}
}
}

output:-
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9

Thread example in java (Java Threads)

import java.lang.Thread;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Initial Thread demo example");
Thread t = Thread.currentThread();
System.out.println("The current thread is " +t);
System.out.println("Its name is " +t.getName());


}

}


output:-
Initial Thread demo example
The current thread is Thread[main,5,main]
Its name is main

Monday, 19 October 2015

Serialization example in java

import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//Employee e;
Employee j[] = new Employee[10];
int k;
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fout = new FileOutputStream("file.txt");
ObjectOutputStream obj = new ObjectOutputStream(fout);
FileInputStream fin = new FileInputStream("file.txt");
ObjectInputStream obj1 = new ObjectInputStream(fin);
//k = Integer.parseInt(br.readLine());
//System.out.println("How many objects do you need to creat?");

for(int i=0;i<10;i++){
j[i]=new Employee(10,"Sai");
obj.writeObject(j[i]);
}
obj.close();

Employee e;
try {
while((e = (Employee)obj1.readObject())!= null){
e.display();

}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("The value of Obj is");
e1.printStackTrace();
}

catch (EOFException e1) {
// TODO Auto-generated catch block
System.out.println("Reached EOF");
e1.printStackTrace();
}


}
}

import java.io.*;
public class Employee implements Serializable {
private int age;
private String name;
Employee(int i,String s){
this.age=i;
this.name=s;
}
void display(){
System.out.println("The values of the object are " +name +"\t" +age );
}
}

output:-

Writing into the file using FileWriter in java

import java.io.*;

public class FileRead{
public static void main(String args[])throws IOException{
String str = "This is Performance engineering blog";
FileWriter fw = new FileWriter("test.txt");
for(int i=0;i<str.length();i++){
fw.write(str.charAt(i));
}
fw.close();
}
}



output(file content):-
This is Performance engineering blog

Reading from a file using FileReader in java

import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int ch;
FileReader fr = null;
try{
fr = new FileReader("test.txt");
}
catch(FileNotFoundException fe){
System.out.println("File Not found");
}
//finally{
// fr.close();
// }
while((ch=fr.read())!=-1){
System.out.println((char)ch);
}
fr.close();
}

}


Output:-
P
e
r
f
o
r
m
a
n
c
e
 
B
l
o
g




Text file contents:-
Performance Blog

Writing into the file using BufferedOutputStream in java

import java.io.*;
public class First {

public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub

DataInputStream dis = new DataInputStream(System.in);
FileOutputStream fout = new FileOutputStream("test.txt",true);
BufferedOutputStream bout = new BufferedOutputStream(fout,1024);
char ch;
while((ch = (char)dis.read())!='*'){
bout.write(ch);
}
bout.close();
}
}


output:-
Sr.Performance engineer
Sai Nagendra Bhavani Prasad
Java Profiles
APM Tools
*

Text file contents:-
Sr.Performance engineer
Sai Nagendra Bhavani Prasad
Java Profiles
APM Tools

Reading from the file using FileInputStream in java

import java.io.*;
public class First {

public static void main(String[] args) throws IOException,FileNotFoundException {
// TODO Auto-generated method stub
FileInputStream fin = new FileInputStream("test.txt");
int ch;
while((ch=fin.read())!=-1){
System.out.println("the values in the file are " +(char)ch);

}
}

}


output:-
the values in the file are B
the values in the file are a
the values in the file are t
the values in the file are t
the values in the file are i
the values in the file are p
the values in the file are a
the values in the file are t
the values in the file are i
the values in the file are
the values in the file are s
the values in the file are a
the values in the file are i
the values in the file are
the values in the file are N
the values in the file are a
the values in the file are g
the values in the file are e
the values in the file are n
the values in the file are d
the values in the file are r
the values in the file are a
the values in the file are
the values in the file are B
the values in the file are h
the values in the file are a
the values in the file are v
the values in the file are a
the values in the file are n
the values in the file are i
the values in the file are
the values in the file are P
the values in the file are r
the values in the file are a
the values in the file are s
the values in the file are a
the values in the file are d
the values in the file are

the values in the file are

the values in the file are S
the values in the file are r
the values in the file are .
the values in the file are P
the values in the file are e
the values in the file are r
the values in the file are f
the values in the file are o
the values in the file are r
the values in the file are m
the values in the file are a
the values in the file are n
the values in the file are c
the values in the file are e
the values in the file are
the values in the file are E
the values in the file are n
the values in the file are g
the values in the file are i
the values in the file are n
the values in the file are e
the values in the file are e
the values in the file are r
the values in the file are

the values in the file are

writing data into the file usinf FileOutputStream in java

import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DataInputStream dis = new DataInputStream(System.in);
FileOutputStream fout = new FileOutputStream("test.txt");
char ch;
while((ch = (char)dis.read())!='*'){
fout.write(ch);
}
fout.close();
}

}


Output:-
q
w
e
r
t
y
u
i
*

In text file:-
q
w
e
r
t
y
u
i

Sorting using Arrays.sort()

import java.util.*;
import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
First oj = new First();
int arry[] = new int[10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++){
arry[i] = Integer.parseInt(br.readLine());
}
oj.display(arry);
Arrays.sort(arry);
System.out.println("After sorting the values");
oj.display(arry);

}

void display(int arr[]){
for(int k : arr){
System.out.println("The value of array is " +k);
}

}

}


output:-
11
33
55
77
33
21
54
00
56
40
The value of array is 11
The value of array is 33
The value of array is 55
The value of array is 77
The value of array is 33
The value of array is 21
The value of array is 54
The value of array is 0
The value of array is 56
The value of array is 40
After sorting the values
The value of array is 0
The value of array is 11
The value of array is 21
The value of array is 33
The value of array is 33
The value of array is 40
The value of array is 54
The value of array is 55
The value of array is 56
The value of array is 77

Thursday, 15 October 2015

Arraylist example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> obj = new ArrayList<String>();
obj.add("India");
obj.add("Bombay");
obj.add("Test");
Iterator it = obj.iterator();
while(it.hasNext()){
System.out.println("The value of arraylist is " +it.next());

}
}

}

output:-
The value of arraylist is India
The value of arraylist is Bombay
The value of arraylist is Test

HashMap example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,Integer> obj = new HashMap<String,Integer>();
obj.put("battipati", 1);
obj.put("Sai", 2);
obj.put("Nagendra", 3);
obj.put("Bhavani", 4);
obj.put("Prasad",5);
Iterator it = obj.entrySet().iterator();
while(it.hasNext()){
Map.Entry a = (Map.Entry) it.next();
System.out.println("The values are  " +a);
System.out.println(a.getKey() + " = " + a.getValue());
}
}
}


output:-
The values are  Bhavani=4
Bhavani = 4
The values are  Prasad=5
Prasad = 5
The values are  battipati=1
battipati = 1
The values are  Nagendra=3
Nagendra = 3
The values are  Sai=2
Sai = 2

Stack example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> st = new Stack<String>();
st.add("India");
st.add("Nepal");
st.add("Sri Lanka");
st.add("Country");
for (String s : st){
System.out.println("the value of st is " +s);
}
boolean i = st.empty();
System.out.println("The value of i is " +i);
String s1 = st.peek();
System.out.println("the value of top most element is " +s1);
String s2 = st.pop();
System.out.println("the value of top most element is " +s2);
int j = st.search("Country");
System.out.println("the value of j is " +j);
String s3 = st.peek();
System.out.println("the value of top most element is " +s3);
}
}


output:-
the value of st is India
the value of st is Nepal
the value of st is Sri Lanka
the value of st is Country
The value of i is false
the value of top most element is Country
the value of top most element is Country
the value of j is -1
the value of top most element is Sri Lanka


Vector example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Vector<String> obj = new Vector<String>();
obj.add("India");
obj.add("Digital");
obj.add("Message");
obj.add("Super");
Iterator it = obj.iterator();
while(it.hasNext()){
System.out.println("the values are " +it.next());

}

}
}

output:-
the values are India
the values are Digital
the values are Message
the values are Super

LinkedList example in java

import java.util.*;


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<Integer> obj = new LinkedList<Integer>();
obj.add(10);
obj.add(12);
obj.add(13);
obj.add(23);
obj.add(13);
Iterator it = obj.iterator();
while(it.hasNext()){
System.out.println("The value is  " +it.next());
}
obj.remove(2);
System.out.println("The value is  " +obj);
int l = obj.size();
System.out.println("the value of l is " +l);
}
}


output:-
The value is  10
The value is  12
The value is  13
The value is  23
The value is  13
The value is  [10, 12, 23, 13]
the value of l is 4

LinkedHashSet example in java

import java.util.*;


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashSet<String> hs = new LinkedHashSet<String>();
hs.add("Digital");
hs.add("India");
System.out.println("The value of hs is " +hs);
for(String s : hs){
System.out.println("the value of s is " +s);
}
Iterator<String> it = hs.iterator();
while(it.hasNext()){
System.out.println("the value through iterator is " +(String)it.next());
}
}
}


output:-
The value of hs is [Digital, India]
the value of s is Digital
the value of s is India
the value through iterator is Digital
the value through iterator is India

Hashset example in java (using for-each loop)

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();
hs.add("Digital");
hs.add("India");
hs.add("Hello");
hs.add("World");
for(String st: hs){
System.out.println("The string value is " +st);
}

}

}


output:-
The string value is Hello
The string value is Digital
The string value is World
The string value is India

HashSet example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();
hs.add("India");
hs.add("Amrica");
hs.add("Singapore");
hs.add("Nepal");
System.out.println("The value of HashSet is " +hs);
Iterator<String> it = hs.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println("the value is " +s);
}
}

}

output:-
The value of HashSet is [Amrica, Singapore, Nepal, India]
the value is Amrica
the value is Singapore
the value is Nepal
the value is India

Wednesday, 14 October 2015

Array example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Employee array[] = new Employee[5];
for(int i=0;i<5;i++){
array[i]=new Employee(i,"Sai"+i);
System.out.println("The array values are " +array[i].age +"\t" +array[i].name );
}
}

}



public class Employee {
int age;
String name;
Employee(int i,String s){
age=i;
name=s;
}
}


output:-
The array values are 0 Sai0
The array values are 1 Sai1
The array values are 2 Sai2
The array values are 3 Sai3
The array values are 4 Sai4

Wrapper Class examples in java

import java.io.*;

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Byte obj = new Byte((byte) 10);
Byte obj1 = new Byte("10");
Character obj2= new Character('A');
byte j=obj1.byteValue();
byte i=obj.byteValue();
int k = obj.compareTo(obj1);
System.out.println("The value of K is " +k);
System.out.println("The value of i is " +i);
System.out.println("The value of j is " +j);
char ch = obj2.charValue();
System.out.println("The value of char is " +ch);
Character obj3 = Character.valueOf(ch);
System.out.println("The value of char is " +obj3);


}
}


output:-
The value of K is 0
The value of i is 10
The value of j is 10
The value of char is A
The value of char is A

Tuesday, 13 October 2015

Custom exception handling example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
try{
System.out.println("The value is " +i);

if(i==0){
Myexception m = new Myexception("Less than 10");
throw m;
}
}
catch(Myexception m){
m.printStackTrace();

}
finally{
System.out.println("I am in final block");
}
}
}

}


public class Myexception extends Exception {
String j;
Myexception(){
System.out.println("Default constructed");
}
Myexception(String i){
super(i);
}
}

output:-
The value is 0
Myexception: Less than 10
at First.main(First.java:11)
I am in final block
The value is 1
I am in final block
The value is 2
I am in final block
The value is 3
I am in final block
The value is 4
I am in final block
The value is 5
I am in final block
The value is 6
I am in final block
The value is 7
I am in final block
The value is 8
I am in final block
The value is 9
I am in final block

Exception handling example in java (ArrayIndexOutOfBoundsException exception handling)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
obj.S();
}

}



public class Second {
int i[]=new int[5];
   void S(){
try{  i[7]=2;}
catch (ArrayIndexOutOfBoundsException a){
a.printStackTrace();
System.out.println("Catch Block.....");
}
finally{
 
System.out.println("Final Block .....");
}
  
   }
}

output:-
java.lang.ArrayIndexOutOfBoundsException: 7
at Second.S(Second.java:6)
at First.main(First.java:7)
Catch Block.....
Final Block .....


Exception handling example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int k = args.length;
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
Sec obj = new Sec();
obj.sum(i, j);
}

catch(ArrayIndexOutOfBoundsException a){

a.printStackTrace();
System.out.println("Please pass runtime arguments while executing the program");
}
finally{
System.out.println("Finally block");
}
}
}


public class Sec {
void sum(int i,int j){
int k = i+j;
System.out.println("The value is " +k);
}
}

output:-
java.lang.ArrayIndexOutOfBoundsException: 0
at First.main(First.java:8)
Please pass runtime arguments while executing the program
Finally block

Java Checked Exception example

import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("Enter your number");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(br.readLine());
System.out.println("The value of \"i\" is " +i);
}
}

output:-
Enter your number
10
The value of "i" is 10

Java Interface example


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj = new Second();
Four obj1 = new Second();
obj.F();
obj.S();
obj1.F();
obj1.M();
obj1.S();
obj1.T();
}

}


public class Second implements Four {
public void F(){
System.out.println("F");
}
public void S(){
System.out.println("S");
}
public void T(){
System.out.println("T");
}
public void M(){
System.out.println("M");
}

}



public interface Third {
void F();
void S();
}

interface Four extends Third{
void T();
void M();
}


output:-
F
S
F
M
S
T

Monday, 12 October 2015

Interface example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Third();
obj.display();
obj.fine();
}

}



public interface Second {
void display();
void fine();
}



public class Third implements Second{
public void display(){
System.out.println("One");
}
public void fine(){
System.out.println("Two");
}
}


output:-
One
Two

Abstract class example 2


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj = new four();
obj.First();
obj.like();
obj.display();
}

}


public class Second {
void display(){
System.out.println("Testing display");
}
}

abstract class Third extends Second{
abstract void First();
void like(){
System.out.println("like");
}
}

class four extends Third{
void First(){
System.out.println("Testing Third Display");
}
}

output:-
Testing Third Display
like
Testing display

Abstract class example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second reff = new four();
reff.display();
reff.first();
}

}


public abstract class Second {
abstract void display();
abstract void first();
}

abstract class Third extends Second{
//abstract void first();
void display(){
System.out.println("Third method");
}
}
class four extends Third{
void first(){
System.out.println("four method");
}
}

output:-
Third method
four method


Overriding example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Third();
obj.Common();
Second obj1 = new Four();
obj1.Common();
Second obj2 = new Second();
obj2.Common();
Third obj3 = new Four();
obj3.Common();
}

}



public class Second {
void Common(){
System.out.println("Second class Common");
}
}


class Third extends Second{
void Common(){
System.out.println("Third class Common");
}
}

class Four extends Third{
void Common(){
System.out.println("Four class Common");
}
}

output:-
Third class Common
Four class Common
Second class Common
Four class Common

Type casting class example in java (generalization and specialization example in java)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj =  new Third();
obj.s();
Third obj1 = new Four();
obj1.s();
obj1.T();
Second obj2 = new Four();
obj2.s();
Second obj3 = new Third();
Third obj4 = (Third) obj3;
obj4.s();
obj4.T();
}
}


public class Second {
void s(){
System.out.println("Parent Second");
}

}


class Third extends Second{

void T(){

System.out.println("Parent Third");
}

}

class Four extends Third{
void F(){

System.out.println("Parent Four");
}
}

output:-
Parent Second
Parent Second
Parent Third
Parent Second
Parent Second
Parent Third

Widening and Narrowing conversion example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
char ch = 'A';
int i = ch;
System.out.println("The value of \"i\" is " +i);
int j = 66;
char cha = (char)j;
System.out.println("The value of \"j\" is " +cha);


}

}


output:-
The value of "i" is 65
The value of "j" is B

Friday, 9 October 2015

Static polymorphism using private method in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
obj.show1();
}

}


public class Second {
private void show(){
System.out.println("Private method");
}

void show1(){
show();
}
}

output:-
Private method


Java Static polymorphism example


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Third();
obj.show();
}

}



public class Second {

static void show(){
System.out.println("Parent");
}
}


class Third extends Second{
static void show(){
System.out.println("Child");
}
}

output:-
Parent

Method overloading example in java (dynamic polymorphism)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
obj.show();

}

}


public class Second {
int i,k;
float j;
void show(){
System.out.println("No argument method");
}
void show(int k){
this.k=k;
System.out.println("The value of k is " +k);
}
void show(int i,float j){
this.j=j;
this.i=i;
System.out.println("The value of i,j is " +i +j);
}
}

output:-
No argument method

Dynamic overriding example in java (dynamic polymorphism)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
Third  obj1 = new Third();
obj.show();
obj1.show();
}
}



public class Second {

void show(){
System.out.println("Parent");

}

}

class Third extends Second{
void show(){


System.out.println("Child");

}


}

Output:-
Parent
Child

Thursday, 8 October 2015

"Super" keyword usage example in Java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj=new Third();
}

}



public class Second {
int i;
Second(int i){
this.i=i;
System.out.println("This is super class param constructor");
}
}



class Third extends Second{
Third(){
super(20);
System.out.println("The value of super calss variable i is " +super.i);
}
}

output:-
This is super class param constructor
The value of super calss variable i is 20

"This" keyword usage in Java


public class Acceptt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Third obj = new Third(10,20);
  obj.show();
  }

}



class Second {

 void show(){
 
  System.out.println("I am Parent");
 }

}


class Third extends Second{
 int i,j;

 void show(){

  System.out.println("I am child");
 }
 Third(){
  System.out.println("Third constructor");
 }

 Third(int i,int j){
  this();
  this.i=i;
  this.j=j;
  System.out.println("The values are "+i  +j);
  }

 void display(){
  this.show();
 }

}


output:-
Third constructor
The values are 1020
I am child

Java Inheritance example


public class Inheritance {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
First obj1 = new First();
obj.setage(10);
obj1.setage(20);
System.out.println("The value is " +obj.i);
System.out.println("The value is " +obj1.i);
}

}


public class First {
int i;
String S;
void setage(int i){
this.i=i;
}
void setname (String S){
this.S=S;
}
int getage(){
return i;
}
String getname(){
return S;
}
}

class Second extends First{
int j;
void setrollno(int i){
j=i;
}
}

output:-
Test one
Test Two

Java Program Example to demonstrtae First parent constructor is called followed by chiled class constructor

/**
 *
 */

/**
 *
 *
 */
public class Second {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
First obj = new First();
}

}


public class Demo {
Demo(){
System.out.println("Test one");
}
}

class First extends Demo{
First(){
System.out.println("Test Two");
}
}

Output:-
Test one
Test Two

Java Inheritance example Program


public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub

SubClass obj = new SubClass();

obj.display();

}

}




public class First {
protected String str;
First() {
str = "Java ";
}
}
 
class SubClass extends First {
SubClass() {
str = str.concat("Is My World ...!!!");
}
void display()
{
System.out.println(str);
}
}

output:-
Java Is My World ...!!!

Java Program - Relationship between Objects


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Sec obj1 = new Sec(20);
Demo obj2 = new Demo(obj1);
obj2.display();
}
}


public class Demo {
Sec t;
int x;
Demo(Sec t){
this.t=t;
x=10;
}
void display(){
System.out.println("The value of x is " +x);
System.out.println("The value of y in Other object is " +t.i);
}
}


public class Sec {
int i;
Sec(int i){
this.i=i;
}
void display(){
System.out.println("The value of i is " +i);
}
}


output:-
The value of x is 10
The value of y in Other object is 20

Wednesday, 7 October 2015

Java - Swapping values within Object

package passing_32_blog;

public class Pass {

public static void main(String[] args) {
// TODO Auto-generated method stub
First obj1 = new First(10,20);
First obj2 = new First(30,40);
System.out.println("The values before swap are " +obj1.i+"\t" +obj1.j);
obj2.swap(obj1);
System.out.println("The values after swap are " +obj1.i+"\t" +obj1.j);
}
}


package passing_32_blog;

public class First {
int i,j;
First (int k,int l){
i=k;
j=l;
}
void swap(First s){
int temp;
temp = s.i;
s.i=s.j;
s.j=temp;
}
}

Output:-
The values before swap are 10 20
The values after swap are    20 10

Java Program - Passing values to methords


public class Passing {

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo obj1 = new Demo(10);
Demo obj2 = new Demo(20);
Demo obj3 = new Demo(30);
System.out.println("The values before calling swap " +obj1.i + "\t" +obj2.i);
obj3.swap(obj1.i, obj2.i);
System.out.println("The values after calling swap " +obj1.i + "\t"  +obj2.i);
}

}


public class Demo {
int i;
Demo(int i){
this.i=i;
}

void swap(int i,int j){
int temp;
temp=i;
i=j;
j=temp;
}
}

Output:-
The values before calling swap 10 20
The values after calling swap 10 20

Java Program - Passing Objects to the methords

import java.io.*;
import java.util.*;
import java.lang.*;
class Acceptt{
public static void main(String args[]) throws IOException{
First obj1 = new First(10);
First obj2 = new First(20);
Second obj3 = new Second();
System.out.println("The HashCode value of Obj1 is " +obj1.hashCode());
System.out.println("The HashCode Value of Obj2 is " +obj2.hashCode());
System.out.println("The value of obj1 is " +obj1.i);
System.out.println("The value of obj2 is " +obj2.i);
obj3.check(obj1, obj2);
System.out.println("In the main Function");
System.out.println("The HashCode value of Obj1 is " +obj1.hashCode());
System.out.println("The HashCode Value of Obj2 is " +obj2.hashCode());
System.out.println("The value of obj1 is " +obj1.i);
System.out.println("The value of obj2 is " +obj2.i);

}
}

class First{

int i;

First(int j){

this.i=j;
}
}

class Second{
void check(First k,First d){
int temp;
temp=k.i;
k.i=d.i;
d.i=temp;
System.out.println("In the sub Function");
System.out.println("The HashCode value of Obj1 is " +k.hashCode());
System.out.println("The HashCode Value of Obj2 is " +d.hashCode());
System.out.println("In the sub Function");

System.out.println("The value of obj1 is " +k.i);
System.out.println("The value of obj2 is " +d.i);
}

}


Output :-
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
The value of obj1 is 10
The value of obj2 is 20
In the sub Function
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
In the sub Function
The value of obj1 is 20
The value of obj2 is 10
In the main Function
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
The value of obj1 is 20
The value of obj2 is 10