Wrapper Class Concept Simplified in Java

Traditional containers like vector can not handle primitive data types like int,float,long,char and double.Primitive data types can be converted to objects using the wrapper classes.Wrapper classes are contained in the java.lang package.

Simple type Wrapper class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
Wrapper classes have number of unique methods for handling primitive data types and objects.
Converting primitive numbers to object:

Constructor calling conversion action
Interger intval=new Integer(i); primitive integer i to Integer Object
Float floatval=new Float(f); primitive float f to Float Object
Double doubleval=new Double(d); primitive double d to Double Object
Long longval=new Long(l) primitive long l to Long Object
i,f,d,l are primitive data values denoting int,float,double,long data type. They may be constant or any variable.
Converting object numbers to primitive numbers using typeValue() method:

Method Action
int i=intVal.intValue() Object to primitive integer
float f=floatVal.floatValue() Object to primitive float
long l=longVal.longValue() Object to primitive long
double d=doubleVal.doubleValue() Object to primitive double
Converting numbers to String using toString() method:

Method Action
String str=Integer.toString(i); primitive integer to String
String str=Float.toString(f); primitive float to String
String str=Double.toString(d); primitive double to String
String str=Long.toString(l) primitive long to String

Converting String objects to Numeric objects using static valueOf() method:

Method Action
intVal=Integer.valueOf(str); String to integer object
longval=Long.valueOf(str); String to long object
floatVal=Float.valueOf(Str); String to float object
doubleVal=Double.valueOf(str) String to double object
Converting Numeric Strings to primitive Numbers:

Method Action
int intVal=Integer.parseInt(str) String to primitive integer
long longval=Long.parseLong(str); String to primitive long
float floatVal=Float.parseFloat(str) String to primitive float
double doubleVal=Double.parseDouble(str) String to primitive double
All these methods may throw the NumberFormatException if the value of the String(str) does not represent proper number.
An Example:

public class InterestCalculate{

     public static void main(String []args){
         //converting Number to object
         Float principalAmount=new Float(0);
         Float interestRate=new Float(0);
         int numYears=0;
             DataInputStream in=new DataInputStream(System.in);
             System.out.println("Enter Principal Amount:");
             String principalString=in.readLine();
             //converting string to float object
             System.out.println("Enter rate of interest :");
             String interestString=in.readLine();
             //converting string to float object
             System.out.println("Enter number of years :");
             String yearString=in.readLine();
             //converting string to int
             catch(IOException e)
                System.out.println("I/O Error has occured");
             catch(NumberFormatException e)
                System.out.println("Number format is not correct");
             float value=total(principalAmount,interestRate,numYears);
             System.out.println("The final amount you will get"+value);
     static float total(float p,float r,int n)
         int year=1;
         float sum=p;
         return sum;
Wrapper Class Concept Simplified in Java Wrapper Class Concept Simplified in Java Reviewed by Animesh Chatterjee on December 13, 2018 Rating: 5

No comments:

Powered by Blogger.