Featured Article

Wednesday, 23 July 2014

Wrapper Classes in Java

Wrapper Classes in Java

Introduction to wrapper class in java:

Wrapper Class :
  • Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.
  • Sometimes it is required to create an object representation of these primitive types.
  • These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.
  • To satisfy this need, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class.
  • Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.
  • The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.
These classes offer a wide array of methods that allow to fully integrate the primitive types into Java’s object hierarchy.


Wrapper Classes:The primitive data types in Java such as Int,Char,Float etc has a class dedicated to it.Sometimes it is required to create an Object for these data types.The classes which provided this functionality in Java are known as 'Wrapper classes' in Java,because basically they 'wrap' the data type of the class to an Object.The wrapper classes are present in Java.lang package.

Advantages of Wrapper Classes in Java:


  • They provide mechanism to wrap primitive data types,so that primitives can do the activities reserved for the objects like being added to Array lists,Hash sets,Hash Maps etc.
  • They provide utilities functions for primitives like converting them to and from String objects,converting them to various bases like Binary,Hexadecimal and comparing various objects.
  • They are used basically for Type Casting.


Following statements illustrate the use of Wrapper classes:

int x=10;
Integer y=new Integer(x);

The first statement declares an int variable x,initialized with value 10,whereas the second statement creates an object for Integer y.The object initializes and reference to the object is assigned to the variable y.

In the above two statements,we had wrapped the data type Integer and now we are going to unwrap it,it is illustrated by following statements:

int k=y.intValue();
System.out.println(k*k); // Result is100



In the above codes,we had used Wrapping and Un-Wrapping of data types in Java.Each data type has a wrapper class associated to it,the picture shows the list



The wrapper classes perform wrapping and un-wrapping of data types,here is the program of implementing wrapper classes in Java.

WAP to implement Wrapper classes in Java

class wrappler
{
public static void main(String args[])
{
byte grade=2;                                               //Byte data type
int marks=50;                                               //Integer data type
float pric=5.6f;                                             //Float data type
double rate=50.6;                                        //Double data type

Byte g1=new Byte(grade);                          //Object created for Byte data type
Integer m1=new Integer(marks);                // Object created for Integer data type
Float f1=new Float(pric);                            //Object created for Float data type
Double r1=new Double(rate);                      //Object created for Double data type

System.out.println("Values are");
System.out.println("Byte object g1:"+g1);
System.out.println("Integer object m1:"+m1);
System.out.println("Byte object f1:"+f1);
System.out.println("Double object r1:"+r1);

}
}

The output for the program is:





Wrapper classes are not basically used in programs these days,because Type Casting is also supported by Java.But still,we can use Wrapper class according to our requirement.







Thursday, 10 July 2014

Exception Handling

Exception Handling

 It deals with the way errors can be handled. Error can be complie time ,run time ,logical. In exception handling we deal with errors caused at run time. For eg. in division of 2 no's if denominator is assigned a value 0 this will affect the output, an error will be generated. Exception handling deals with such type of errors. Exception classes can be in built or they can be user defined.

Some in built classes are-
1.Arithmetic Exception- to handle arithmetic errors like 0 in denominator.
2.Array Index Out Of Bounds- used when the position assigned to some data in array is not present.
3.Array Store Exception- assignment to an array of an incompatible type.
4.Class Cast Exception- for invalid casting.
5.Illegal Argument Exception- use of illegal arguments.
6.Illegal State Exception- environment or application is in incorrect state.
7.Index Out Of Bounds- Some type of indexes are out of bounds.
8.Negative Array Size Exception- Array created with a negative size.
9.Null pointer exception- use of null reference.
10.Number format exception-invalid conversion of string to numeric format.
Programs regarding some in built exceptions:-
1.Array Out Of Bounds Exception:
import java.io.*;
class Arroutbounds
{
public static void main(String arg[])
{
int a[]= new int[10];
int b,i;
DataInputStream dis = new DataInputStream(System.in);
try
{
try
{
System.out.println("enter no of integer's uh want to enter");// output:enter no of integer's uh want to              
b=Integer.parseInt(dis.readLine());            //15                                                                         //enter
for(i=0;i<b;i++)
{
a[i]=Integer.parseInt(dis.readLine());//output:1234567891112
}
for(int j=0;j<b;j++)
{
System.out.println(a[j]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Beyond the limits");    //output:Beyond the limits
}
}
catch(IOException io)
{}
}

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.Number Format Exception:
import java.io.*;
class Noformats
{
public static void main(String arg[])
{
int a;
DataInputStream dis= new DataInputStream(System.in);
try
{
try
{
System.out.println("enter int value");    //output:enter a value
a=Integer.parseInt(dis.readLine());        //output:struy
}
catch(NumberFormatException e)
{
System.out.println("data entered is incompatible");    //output:data entered is incompatible
}
}
catch(IOException io){}
}

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.Arithematic Exception:
import java.io.*;

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

DataInputStream dis = new DataInputStream(System.in);


try
{
int a,b,c;
a=Integer.parseInt(dis.readLine());       //output: 4
b=Integer.parseInt(dis.readLine());      //output:0
try
{
c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("denominator cannot be 0");   //output:denominator cannot be 0
}
finally
{
System.out.println("this is the final block");        //output:this is the final block
}
}
catch(IOException io){}

}

}

Friday, 4 July 2014

FINAL KEYWORD IN JAVA

final as the name suggest fixed whatever that is either the function or the variable
its a keyword used to fix the vaue of a vareable or body of a function
FINAL VARIABLE  ----->like static if fixes the value of variable which can not be changed
value is given when variable is defined
FINAL METHOD----->if we donot want to change the body of derived class then final keyword is used with base class
FINAL CLASS ---->these classes can be inherited


FINAL---------------------
import java.io.*;
import java.util.*;
class Finals
{
public static void main(String arg[])
{
final int hr=24;
System.out.println(hr);
}}