PostHeaderIcon Wrapper Class

As of now, you have seen
How to take input as a string using buffered Reader?
How to read character?
How to read input using command-line arguments?
And now let’s see how to read integer, double, float etc. value in JAVA programming.
To read input as integer, double, float etc. value we use concept of wrapper class.
Which deal with converting string to datatype object and vice versa. This concept is also use in vectors in JAVA and also assists the various concept in java that need the data to be converted from one format to another. All wrapper class are included in the package java.lang.

The different wrapper classes available in java are as follow:

Datatype Wrapper Class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long

Primitive datatypes are handled in java using wrapper class concept with unique mehods:

 1. Converting Numeric strings to Primitive Numbers Using Parsing methods:

Method calling Conversion Action
int i=Integer.parseInt(str) Converts String to primitive integer
long l=Long.parseLong(str) Converts String to primitive long
double d=Double.parseDouble(str) Converts String to primitive double
float f-Float.parseFloat(str) Converts String to primitive float

2. Converting Numbers to strings Using to String( ) Method:

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

3. Converting Primitive Numbers to Object Number using constructor Method:

Constructor Calling Conversion Action
Integer IntVal=new Integer(i) Primitive integer to Integer object
Float FloatVal=new Float(f) Primitive float to Float object
Double DoubleVal=new Double(d) Primitive double to Double object
Long LongVal=new Long(l) Primitive long to Long object

4. Converting Object Numbers to primitive numbers Using typeValue() Method:

Method calling Conversion Action
int i=IntVal.intValue() Converts object  to primitive integer
long l=LongVal.longValue() Converts object to primitive long
double d=DoubleVal.doubleValue() Converts object to primitive double
float f-FloatVal.floatValue() Converts object to primitive float

5. Converting String Object to Numeric objects Using the static Method ValueOf():

Method calling Conversion Action
IntVal=Integer.ValueOf(str) Converts String to Integer Object
LongVal l=Long. ValueOf (str) Converts String to Long Object
DoubleVal d=Double. ValueOf (str) Converts String to Double Object
FloatVal f-Float. ValueOf str) Converts String to Float Object

Note:

  1. str used in the table is variable which accept string value from user.
  2. parseInt() and parseDouble() etc parsing Method throe NumberFormatException if the value of the str does not represent respective numeric value.
  3. i, f, d, l are primitive data value representing int. float, double, long datatype. They may be constant or variables.

Program illustrating concept of wrapper class.

import java.io.*;
class WrapperClass
{
public static void main(String[] args)
{
String principleString, interestString, yearsString;
Float principleAmount=new Float(0); // Converting Number to object
Float interestRate=new Float(0);
float value;
int numYears=0;
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Principle Amount");
principleString=in.readLine();
principleAmount=Float.valueOf(principleString); //String object to Numer object
System.out.println("Enter Interest Rate");
interestString=in.readLine();
interestRate=Float.valueOf(interestString);
System.out.println("Enter Number of years");
yearsString=in.readLine();
numYears=Integer.parseInt(yearsString); // Numberic string to numbers
}
catch(IOException e)
{
System.out.println("I/O Error");
}
value=loan(principleAmount.floatValue(), interestRate.floatValue(), numYears); //passing object after converting it into primitive type
System.out.println("Final Value = "+value);
}
static float loan(float p, float r, int n)
{
int yr=1;
float sum=p;
while(yr<=n)
{
sum=sum*(1+r);
yr=yr+1;
}
return sum;
}
}

Output as follows:

Wrapper Class in java

MoreTips

2 Responses to “Wrapper Class”

Leave a Reply

blogroll
Learning Is Just Beginning...!!!
Tech With Ric - Free Tech Tutorials, Downloads and Stuff!
Follow Us On!
Recommend on Google

Enter your email address:

Delivered by FeedBurner

Catch us on blogger.com
Review this blog on Bloggers.com