Saturday, February 28, 2009

source code Boolean class



package wclasses;

public class SampleBoolean {
public static void main(String att[])
{

// constructors
Boolean b1= new Boolean("true");
Boolean bd=new Boolean(true);

// wrapper to primitive
boolean pb1=b1.booleanValue();
System.out.println(pb1);

//primitive to string
String s1=b1.toString();
String s2=Boolean.toString(true);
System.out.println(s1);
System.out.println(s2);

//String to primitive
boolean pb2=Boolean.parseBoolean(s2);
System.out.println(pb2);

//String to wrapper object
Boolean b2= new Boolean("10");
Boolean b3=Boolean.valueOf("100");
System.out.println(b2);
System.out.println(b3);

//wrapper to string
String s3=b3.toString();
}

}

output:
true
true
true
true
false
false



source code Character



package wclasses;

public class SampleCharacter {
public static void main(String att[])
{

// constructors
Character c1= new Character('j');

// wrapper to primitive
char pc1=c1.charValue();
System.out.println(pc1);

//primitive to string
String s1=c1.toString();
String s2=Character.toString('a');
System.out.println(s1);
System.out.println(s2);



//String to wrapper object
Character c2= new Character('a');

System.out.println(c2);

//wrapper to string
String s3=c2.toString();
}

}

output:
j
j
a
a



source code Double class



package wclasses;

public class SampleDouble {
public static void main(String att[])
{

// constructors
Double d1= new Double(10.5);
Double dd=new Double(10.5d);
Double ds=new Double("10.5");

// wrapper to primitive
double pd1=d1.doubleValue();
System.out.println(pd1);

//primitive to string
String s1=d1.toString();
String s2=Double.toString(10.57f);
System.out.println(s1);
System.out.println(s2);

//String to primitive
double pd2=Double.parseDouble(s2);
System.out.println(pd2);

//String to wrapper object
Double d2= new Double("10");
Double d3=Double.valueOf("100");
System.out.println(d2);
System.out.println(d3);

//wrapper to string
String s3=d3.toString();
}

}

output:
10.5
10.5
10.569999694824219
10.569999694824219
10.0
100.0



source code float



package wclasses;

public class Samplefloat {
public static void main(String att[])
{

// constructors
Float f1= new Float(10.5);
Float fd=new Float(10.5d);
Float fs=new Float("10.5");

// wrapper to primitive
float pf1=f1.floatValue();
System.out.println(pf1);

//primitive to string
String s1=f1.toString();
String s2=Float.toString(10.57f);
System.out.println(s1);
System.out.println(s2);

//String to primitive
float pf2=Float.parseFloat(s2);
System.out.println(pf2);

//String to wrapper object
Float f2= new Float("10");
Float f3=Float.valueOf("100");
System.out.println(f2);
System.out.println(f3);

//wrapper to string
String s3=f3.toString();
}

}

output:
10.5
10.5
10.57
10.57
10.0
100.0



source code Long class



package wclasses;

public class SampleLong {
public static void main(String att[])
{
//constructors
Long l1= new Long(10);
Long l2= new Long("10");

// wrapper to primitive
int pl1=l1.intValue();
long l0=l1.longValue();
System.out.println(pl1);

//binary,octa,hexa notations.
String bin=Long.toBinaryString(10);
System.out.println(bin);
String oct=Long.toOctalString(12);
System.out.println(oct);
String hex=Long.toHexString(12);
System.out.println(hex);

//primitive to string
String s1=l1.toString();
String s2=Long.toString(10);
System.out.println(s1);
System.out.println(s2);

//String to primitive
long pl2=Long.parseLong(s2);
System.out.println(pl2);

//String to wrapper object
Long l3= new Long("10");
Long l4=Long.valueOf("100");
System.out.println(l3);
System.out.println(l4);

//wrapper to string
String s3=l4.toString();


}
}

output:
10
1010
14
c
10
10
10
10
100


source code Integer



package wclasses;

public class SampleInteger {
public static void main(String att[])
{
//constructors
Integer i1= new Integer(10);
Integer i2= new Integer("10");

// wrapper to primitive
int pi1=i1.intValue();
System.out.println(pi1);

//binary,octa,hexa notations.
String bin=Integer.toBinaryString(10);
System.out.println(bin);
String oct=Integer.toOctalString(12);
System.out.println(oct);
String hex=Integer.toHexString(12);
System.out.println(hex);

//primitive to string
String s1=i1.toString();
String s2=Integer.toString(10);
System.out.println(s1);
System.out.println(s2);

//String to primitive
int pi2=Integer.parseInt(s2);
System.out.println(pi2);

//String to wrapper object
Integer i3= new Integer("10");
Integer i4=Integer.valueOf("100");
System.out.println(i3);
System.out.println(i4);

//wrapper to string
String s3=i4.toString();


}
}

output:
10
1010
14
c
10
10
10
10
100



source code Short class



package wclasses;

public class SampleShort {
public static void main(String att[])
{
//constructors
short s=10;
Short i1= new Short(s);
Short i2= new Short("10");

// wrapper to primitive
short s0=i1.shortValue();
int pi1=i1.intValue();
System.out.println(pi1);

//primitive to string
String s1=i1.toString();
String s2=Short.toString(s);
System.out.println(s1);
System.out.println(s2);

//String to primitive
short pi2=Short.parseShort(s2);
System.out.println(pi2);

//String to wrapper object
Short i3= new Short("10");
Short i4=Short.valueOf("100");
System.out.println(i3);
System.out.println(i4);

//wrapper to string
String s3=i4.toString();


}
}

output:
10
10
10
10
10
100



Good example code using Byte class methods.



package wclasses;

public class SampleByte {
public static void main(String as[])
{
//constructors
byte b=10;
Byte b1= new Byte(b);
Byte b2= new Byte("10");

// Byte to primitive byte
byte bi=b1.byteValue();
int bi1=b1.intValue();
System.out.println(bi);
System.out.println(bi1);

//byte to string
String s1=b1.toString();
String s2=Byte.toString(b);
System.out.println(s1);
System.out.println(s2);

//String to primitive byte
byte pi2=Byte.parseByte(s2);
System.out.println(pi2);

//String to wrapper Byte
Byte b3= new Byte("10");
Byte b4=Byte.valueOf("100");
System.out.println(b3);
System.out.println(b4);

//Byte wrapper to string
String s3=b4.toString();

}
}

output:
10
10
10
10
10
10
100



sample example to demonstrate has a relation



package oops;
class Engine
{
void m1()
{
.....
}
void m2()
{
.....
}
}
public class Car {
public static void main(String at[])
{
Engine a=new Engine();
a.m1();
a.m2();
}
}



Simple example to demonstrate java inheritance



package oops;
class Superclass
{
void display()
{
System.out.println("hi");
}
}
public class Subclass extends Superclass{
public static void main(String arg[])
{
Subclass ie=new Subclass();
ie.display(); // hi
}
}



static nested class using inner main,outer main method



package innerclass;


public class Test
{
static class Inner
{
public void m1()
{
System.out.println("hai");
}
public static void main(String att[])
{
Inner i=new Inner();
i.m1();
System.out.println("inner main");
}
}
public static void main(String art[])
{
Test.Inner in =new Test.Inner();
in.m1();
System.out.println("outer main");
}

}

here we can run by using outer class main and inner class main
output:
run by using outer class main: java Test$1:
hai
outer main
run by using outer class main: java Test$Inner:
hai
inner main



simple example static innerclass



package innerclass;

public class Test
{
static class Inner
{
public void m1()
{
System.out.println("hai");
}
}
public static void main(String art[])
{
Test.Inner in =new Test.Inner();
in.m1();
}

}

output:
hai


Friday, February 27, 2009

Anonymous inner class declared in method arguments



package innerclass;


class Test1{
public static void main(String arg[])
{
new Thread( new Runnable()
{
public void run()
{
System.out.println("run");
}
}).start();
}
}

output:
run


create thread Anonymous inner class implements Runnable interface



package innerclass;


class Test{
public static void main(String arg[])
{
Runnable r=new Runnable()
{
public void run()
{
System.out.println("run");
}
};
Thread t=new Thread(r);
t.start();
}
}

output:
run


Anonymous inner class implements some interface



package innerclass;

interface Inter
{
public void m2();
}
class Test
{
public static void main(String arg[])
{
Inter i=new Inter(){
public void m2()
{
System.out.println("Hai");
}
};
i.m2(); // hai
}
}


Anonymous inner classes to create thread



package innerclass;

public class Cthread {
public static void main(String arg[])
{
Thread t=new Thread()
{
public void run()
{
System.out.println("child thread job");
}
};
t.start();
System.out.println("main thread job");
}
}

ouytput:
main thread job
child thread job


example code Anonymous inner class

package innerclass;

class Popcorn
{
public void eat()
{
System.out.println("so sweet");
}
}
public class Sample
{
public static void main(String arg[])
{
Popcorn p=new Popcorn() // no semicolon (;) here
{
public void eat()
{
System.out.println("so spicy");
}
}; // with out semicolon (;) leads compile time error
p.eat(); // so spicy
}
}


sample code method local inner class with variables



package innerclass;

public class MethodOuter2 {
static int x=10;
public static void m1()
{
int y=20;
class MethodInner2
{
void show()
{
System.out.println(x); // 10
System.out.println(y); // Compile time error ( If y is final no error)
}
}
MethodInner2 in=new MethodInner2();
in.show();
}
public static void main(String ar[])
{
MethodOuter2 o=new MethodOuter2();
o.m1();
}
}





code methodlocal innerclass



package innerclass;

public class MethodOuter {
// int x=10;
public void m1()
{
int y=20;
class MethodInner
{
public void display(int x,int y)
{
System.out.println("SUM IS"+(x+y));
}
}
MethodInner in=new MethodInner();
in.display(10,20);
in.display(100,200);
}

public static void main(String ar[])
{
MethodOuter o=new MethodOuter();
o.m1();
}
}

output:
SUM IS30
SUM IS300


sample code to use this keyword in inner class

package innerclass;

public class Outerclass {
int x=10;
class Inner
{
int x=50;
public void m1()
{
System.out.println(x); // 50
System.out.println(this.x); // 50
System.out.println(Outerclass.this.x); // 10
}
}
public static void main(String ar[])
{
Outerclass o=new Outerclass();
Outerclass.Inner in=o.new Inner(); //Outer.Inner in=new Outer().new Inner();
in.m1();
}
}


access static and instance variables of outer class from inner class



package innerclass;
//To access static and instance variables of outer class from inner class
public class Outer {
int x=10;
static int y=20;
class Inner
{
int l=30;
public void m1()
{
System.out.println(x+"..."+y);
}
}
public static void main(String ar[])
{
Outerc o=new Outerc();
Outerc.Inner in=o.new Inner(); //Outer.Inner in=new Outer().new Inner();
in.m1();
//System.out.println(in.x+"..."+in.y);//error ( x and y are not instance variables of inner class)
System.out.println(in.l);

}
}

output:
10...20
30




simple example to show the inner class



package innerclass;

public class Outerclass {
private int x=10;
private static int y=20;
class Inner
{
public void m1()
{
System.out.println("instance variable..."+x+",static variable..."+y);
}
}
public static void main(String ar[])
{
Outerclass o=new Outerclass();
Outerclass.Inner in=o.new Inner();//Inner in=new Outer().new Inner();
in.m1();
}
}
output:
instance variable...10,static variable...20


example code using throw



package exceptions;
class MinBalance extends Exception
{
MinBalance()
{

}
MinBalance(String str)
{
super(str);
}
}
public class MyException {

private static int accno[]={1001,1002,1003,1004,1005};
private static String name[]={"raju","rani","ramu","sita","vinay"};
private static double bal[]={5000,12000,4540,1999,1200};
public static void main(String arg[])
{
try
{
System.out.println("accno"+"\t"+"name"+"\t"+"balance");
for(int i=0;i<5;i++)
{
System.out.println(accno[i]+"\t"+name[i]+"\t"+bal[i]);
if(bal[i]<2000)
{
MinBalance me=new MinBalance("Balance amount is less");
throw me;
}

}
}catch(MinBalance me)
{
me.printStackTrace();
}
}
}

output:
accno name balance
1001 raju 5000.0
1002 rani 12000.0
1003 ramu 4540.0
1004 sita 1999.0
exceptions.MinBalance: Balance amount is less
at exceptions.MyException.main(MyException.java:28)



partially checked exception



package exceptions;

import java.io.IOException;

public class FullyCheckedEx {

public static void doMoreStuff()
{
try
{
System.out.println("hai");
}catch(Exception e) //no error (Exception is Partially checked exception)

{
System.out.println("catch");
}

}
}


Example code using throws



package exceptions;
// throws keyword for delegating the responsibility of the Exception handling to the caller
public class ThrowsEx {
public static void main(String a[])throws Exception //without throws error
{
doStuff();
}
public static void doStuff()throws Exception //without throws error
{
doMoreStuff();
}
public static void doMoreStuff()throws Exception //without throws error
{
System.out.println("hai");
Thread.sleep(100); // compile time error
//if we are using above code which is possible to rise the checked exception.
//we must have to use eaither throws,try,catch.
}
}



source code issues FullyChecked exception



package exceptions;

import java.io.IOException;

public class FullyCheckedEx {

public static void doMoreStuff()
{
try
{
System.out.println("hai");
}
catch
(IOException e) //error (IOException is fully checked exception)
//exception java.io.IOException is never thrown in the body of the
//corresponding try statement

{
System.out.println("catch");
}

}
}


simple example to demonstrate Nested try catch

package exceptions;
// take for example Statement 1,statement 2..... some peace of code .
public class NestedTry {
public static void main (String arg[])
{
try
{
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
try
{
System.out.println("Statement 4");
System.out.println("Statement 5");
System.out.println("Statement 6");
}
catch(ArithmeticException e)
{
System.out.println("Statement 7");
}
System.out.println("Statement 8");
}
catch(Exception e)
{
System.out.println("Statement 9");
}
System.out.println("Statement 10");

}
}

Explanation:
Case 1:- If there is no Exception ->1, 2, and 3,4,5,6,8,10 is executes. So normal termination.
Case 2:- If an Exception raised at stmt2 and the corresponding catch block is matches then 1,9,10 are executes. So, normal termination.
Case 3:- If an Exception raised at stmt2 and the corresponding catch block not found. 1 fallowed by abnormal termination.
Case 4:- If an Exception raised at stmt5 and the corresponding catch block is matches then 1,2,3,4,7,10 and Normal Termination.
Case 5:- If an Exception raised at stmt5 and the corresponding catch block is not matched but outer catch block has matched 1,2,3,4,9,10 and Normal Termination.
Case 6:- An Exception raised at stmt2 and the corresponding catch block has matched but while executing that catch block another exception raised then fallowed by abnormal termination.
Case 7:- An Exception raised at stmt8 and the corresponding catch block has matched then 1,2,3,9,10 fallowed by Normal Termination. If the catch block has not matched abnormal termination



example code nested try catch finally



package exceptions;

public class NestedEx {
public static void main (String arg[])
{
try
{
System.out.println("outer try");
try
{
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("arithmetic exception");
}
finally
{
System.out.println("inner finally");
}
}
catch(Exception e)
{
System.out.println("exception");
}
finally
{
System.out.println("outer finally");
}

}
}


output:
outer try
arithmetic exception
inner finally
outer finally


example how to use try with finally with out catch



package exceptions;

public class TryFinally {
public static void main(String arg[])
{
try
{
System.out.println("try");
}

finally
{
System.out.println("finally");
}
}
}

output:
try
finally


example how to try ,catch , finally



package exceptions;

public class Trycatchfinally {
public static void main(String arg[])
{
try
{
System.out.println("fsdf");
}
catch(ArithmeticException e)
{
System.out.println("catch");
}
finally
{
System.out.println("finally");
}
}
}

output:
try
finally


Thursday, February 26, 2009

try multiple catch blocks



Demo 1:
package exceptions;

public class TryMultiCatchblock
{
public static void main(String arg[]){

try
{
System.out.println(10/0);
}
catch(ArithmeticException e) //child
{
//code
}
catch(Exception e1) //parent
{
//code
}
// Here exception parent class ArithmeticException child class
}
}

Demo 2:
package exceptions;

public class TryMultiCatchblock
{
public static void main(String arg[]){

try
{
System.out.println(10/0);
}
catch(Exception e) //parent
{
//code
}
catch(ArithmeticException e1) //child (compile time error)
{
//code
}
// Here exception parent class ArithmeticException child class
}
}


Example code uncheckedExceptions



package exceptions;

public class Unchecked {
public static void main(String ar[])
{
//below code rise the ArithmeticException,which is runtimeexception
//compiler will allow you with out error in case of unchecked.
System.out.println(10/0);
}
}


sorce code checkedExceptions



//example to demonstrate checked exceptions
package exceptions;

public class Checked {
public static void main(String arg[])
{
//if any code which is possible to write checked exception
//compiler will not allow you with out try catch or throws

Thread.sleep(100); //error
//unhandled exception type interruptedexception
}
}



source code trycatch



// Good example to use try and cautch blocks
package exceptions;

public class SimpleEx {
public static void main(String ar[])
{
try
{
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("caught"); // caught
// for see exception information these three methods


//to print exception stack this method
e.printStackTrace(); //don't put in the inside sop.


//to print exception name and message use this method
System.out.println(e.toString());


// to print only message
System.out.println(e.getMessage());

}
}
}

output:
java.lang.ArithmeticException: / by zerocaught

at exceptions.SimpleEx.main(SimpleEx.java:9)
java.lang.ArithmeticException: / by zero
/ by zero



source code defaultexceptionhandler



// Example to demonstrate Default exception handler
package exceptions;

public class De
{
public static void main(String a[])
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println("hai");
System.out.println(10/0); // get exception here
// if we are not keeping try,catch blocks
//default exception handler will take care to print exception stack
}
}