Wrapper Class Concept Simplified in Java

0
35
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

 

Wrapper Class in Java
Wrapper Class in Java

 

Wrapper Class 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.

Wrapper classes:

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;
try{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter Principal Amount:");
System.out.flush();
String principalString=in.readLine();
//converting string to float object
principalAmount=Float.valueOf(principalString);
System.out.println("Enter rate of interest :");
System.out.flush();
String interestString=in.readLine();
//converting string to float object
interestRate=Float.valueOf(interestString);
System.out.println("Enter number of years :");
System.out.flush();
String yearString=in.readLine();
//converting string to int
numYears=Integer.parseInt(yearString);
}
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();
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;
while(year<=n)
{
sum=sum(1+r);
year=year+1;
}
return sum;
}
}
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here