Library class
Wrapper class
Wrapper classes are part of Java's standard library java.lang and these
convert primitive data types into an object. To be more specific, a
wrapper class wraps(encapsulates) a value of primitive type in an
object.
The primitive data types are all in lower-case letters and wrapper
class names' first letter is always capital letter e.g. byte is data type
whereas Byte is its wrapper class.
Java provides wrapper classes for corresponding primitive data types
Primitive data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
We can create an object from wrapper class as follows
Byte b=new Byte (5); // a Byte object created having value 5
Short s=new Short(170); // a Short object created having value170 .
Integer Wrapper Class
parse() methods convert a valid string representation of a number into
corresponding numeric type.Integer class has a parseInt() method,Float
class has a parseFloat() method,Double class has a
parseDouble()method and so
Method Return Argume Description
Type nt
parseInt () int String Converts the argument in
String format into int type:
Eg:String s=”120”;
int n=Integer.parseInt(s);
System.out.println(“Res
ult=”+n);
o/p: Result=120
valueOf():it is used to convert int String Same as parseInt() but
a string value into an integer: returns integer object
Eg:String s=”120”;
int n=Integer.valueOf(s);
System.out.println(“Resul
t=”+n);
o/p: 120
toString() String int Returns String after
converting the integer
Is used to convert an integer value argument
into string.
int a=12;
String
s=Integer.toString(a);
System.out.println(“Resul
t=”+n);
Result: 12
Double wrapper class
Description
Method Return Argu
type ment
parseDouble() double String Return double after
converting the String
[throws argument
NumberFormatExceptio
n]
valueOf() double String Same as parseDouble() but
returns double
toString () String doubl Return string after converting
e the double argument
Float Wrapper Class
Method Return type Argument Description
parseFloat() float String returns float
value after
( throws converting the
number String
format argument
exception)
valueOf() float String returns float
value after
converting the
string
argument
toString () String float converts the
float argument
to string
Character Wrapper Class
Method Return Argument Description
type
isDigit() boolean char returns true if character passed
as argument is a digit
isLetter() boolean char returns true if character passed
as argument is a letter
isLetterorDigit() boolean char returns true if character passed
as argument is a letter or aDigit
isLowerCase() boolean char returns true if character passed
is in LowerCase
isUpperCase() boolean char returns true if character passed
is in UpperCase
toLowerCase() char char converts the character passed
as argument to Lowercase
toUpperCase() char char converts the character passed
as argument to Uppercase
isWhitespace() boolean char returns true if character passed
is a whitespace
What is the difference between boolean and Boolean?
Ans. boolean is a primitive data type.
Boolean is a wrapper class that can wrap up a boolean value so
that it can be used as an object.
Uses of Wrapper class
1. We use wrapper methods to convert a number type value to a string
or vice versa
2. The wrapper constructors create class objects from the primitive
types
3. Converts object data into data of primitive type
Differences between Primitive Datatypes and User defined Datatypes
Primitive Data Types Composite Data
Types(USER DEFINED)
These are built in datatypes. These are created by the User.
Java provides these data types.
There are 8 primitive data
types.
The sizes of these data types are The sizes of these data types are
fixed. variable as their sizes depend
upon their constituent members.
These data types are available in The availability of these data
all parts of a Java program. types depends upon their scope.
Eg: Eg: classes, arrays, objects
byte,short,int,long,float,double,char,
boolean.