There are many situations, where we need to required
to represent the primitive data types in the form of objects.
Some of classes have been defined to represent the
primitive data types in the form of objects.
These classes are known as Wrapper Classes
These classes wrap the concepts of object on the
primitive data types.
Uses
of Wrapper classes:
a.
To represents primitive
data types in the form of their equivalent objects.
b.
Wrapper classes have some
functions which are useful in representing the contents of a String class object
of a primitive data type in its original form (Primitive data type).
Ex: int
x=10;
String s1 =
x+””;
S1 =
s1.trim();
System.out.println(s1);
//10 is primted
Now, this
String class object can be converted back into its equivalent primitive data
type by using the functions of wrapper classes.
Note: Since we have eight primitive data types. Obviously,
we have eight wrapper classes as shown below.
Byte
Short
Integer
Long
Float
Double
Character
Boolean
The wrapper classes are present in the
java.lang.package.
Every wrapper class has two constructor.
i)
One constructor to accept
primitive data argument.
Ex:
int x = 10;
Integer i1
= new Integer(x);
ii)
Another constructor to
accept String class object of primitive data type object of primitive data type
as argument.
Ex:
int x = 10;
String
s1 = x+””;
Integer i2
= new Integer(s1);
Note: No wrapper
class supports a constructor defined to accept Zero arguments.
Since there
are two constructors available, we can pass either the primitive data type or
String class object of the primitive
data type as argument .
To get back
the primitive data types from their equivalent object forms, we have some
static and non-static methods shown below.
Non-static methods
int Value();
float
Value();
double
Value();
Ex: int j =
i1.intValue();
Static methods
parse xxx(String
class object)
ex:
parse
Int(S1);
parse
Float(s2);
parse
Double(s3);
ex: int y
=Integer.parseInt(s1);
Note : The functions of the Wrapper classes can represent
the String class object of primitive data type In its equivalent primitive data
type only if the String class object contains numbers as characters.
But,
normally a string class object can have spaces as characters, which cannot be represented
in the form of their primitive data types(numbers).
It is
highly recommended to call the trim() method on the String class object of the
primitive data type before passing it as argument to the constructor of the
wrapper class or the static methods of the wrapper class.
The following program illustrate the
use of wrapper class.
Class
SimpleInterest
{
Void getsi(double
p,float r,int t)
{
Double SI =
(p*r*t)/100;
System.out.println(“Interest
is : ”+SI);
Double ta =
p+SI;
System.out.println(“Total
amount is : ”+ta);
}
Public static
void main(String args[])
{
SimpleInterest
si = new SimpleInterest();
If(args.length
>= 3)
{
String s1 =
args[0].trim();
Double d1 =
new Double(s1);
double p = d1.doubleValu();
float r =
Float.parseFloat(args[].trim());
int I =
Integer.parseInt(args[2].trim());
si.gets(p,r,t);
}
else
System.out.primtln(“Improper
command line arguments supplied”);
}
}
Try this:
Integer I =
new Integer(77);
System.out.primtln(i);
// Instead of address of I,it prints 77 on the console.
No comments:
Post a Comment