You are on page 1of 24

Fundamental Classes in java.

lang package :-
Introduction
The most commonly used and general purpose classes which are required for any java program are
grouped
into a package which is nothing but a java.lang.package.
All the classes and interfaces which are available in this package are by default available to any java
program. There is no need to import this class.
Object Class :
The most common general methods which can be applicable on any java object are defined in object class.
Object class is the parent class of any java class, whether it is predefined or programmer defined, hence all
the object class methods are by default available to any java class.
Object class define the following methods
clone!"
#reates a new object of the same class as this object.
equals!Object"
#ompares two Objects for equality.
finalize!"
#alled by the garbage collector on an object when garbage collection determines that there
are no more references to the object.
getClass!"
$eturns the runtime class of an object.
asCode!"
$eturns a hash code value for the object.
notif!!"
%akes up a single thread that is waiting on this object&s monitor.
notif!"ll!"
%akes up all threads that are waiting on this object&s monitor.
to#tring!"
$eturns a string representation of the object.
$ait!"
%aits to be notified by another thread of a change in this object.
$ait!long"
%aits to be notified by another thread of a change in this object.
$ait!long, int"
%aits to be notified by another thread of a change in this object.
to#tring%& :-
To return 'tring representation of an object.
(seudo code )*
*****************
public 'tring to'tring!"
+
return get#lass.get,ame!" - &.& - /nteger.to0e1'tring!0ash#ode"2
3
41)*
****
class 'tudent
+
'tring name2
int rollno2
'tudent!'tring name,int rollno"
+
this.name 5 name2
this.rollno 5 rollno2
3
public static void main!'tring arg67"
+
'tudent s 5 new 'tudent !8sai8, 9"2
'tudent s: 5 new 'tudent !8raghav8, 9:"2
'ystem.out.println!s"2 ;'tudent.9b<:c=
'ystem.out.println!s:"2 ;'tudent.>:ba?
3
3
%hen ever we are passing object reference as argument to s.o.p!" internally @AB will call to'tring!"
on that object.
/f we are not providing to'tring!" then Object class to'tring!" will be e1ecuted which is implemented
as follows )*
public 'tring to'tring!"
+
return get#lass.get,ame!" - C.D - /nteger.to0e1'tring!hash#ode"2
3
Eased on our requirement we go forward , to provide our own 'tring representation we have to
override to'tring!" method in our class.
41)*
****
/f we are printing 'tudent Object reference to return name F roll no we have to override
to'tring!" as follows )*
public 'tring to'tring!"
+
return name-G********-rollno2
3
/f we can place this to'tring!" in student class then the OH( is )
sai*****9
raghav*****9:
/t is highly recommended to override to'tring!" in our classes.
asCode%& :-
The hash#ode of an Object just represents a random number which can be used by @AB while
savingHadding Objects into 0ashsets, 0ashtables or 0ashmap.
hash#ode!" of Object class is implemented to return hash#ode based on address of an object, but based
on our requirement we can override hash#ode!" to generate our own numbers as hash#odes.
#ase)*
************
class Test
+
int i2
Test!int i"
+
this.i 5 i2
3
public int hash#ode!"
+
return i2
3
public static void main!'tring arg67"
+
Test t 5 new Test!99"2
Test t: 5 new Test!9"2
'ystem.out.println!t"2 ; <?
'ystem.out.println!t:"2 ; <e
3
3
#ase:)*
*********
class hash#odeIemo
+
int i2
hash#odeIemo!int i"
+
this.i 5 i2
3
public int hash#ode!"
+
return i2
3
public 'tring to'tring!"
+
return i - 882
3
public static void main!'tring arg67"
+
hash#odeIemo h 5 new hash#odeIemo!99"2
hash#odeIemo h: 5 new hash#odeIemo!9"2
'ystem.out.println!h"2 ; 99
'ystem.out.println!h:"2 ; 9
3
3
equals%& :-
--------
41)*
****
class 'tudent
+
'tring name2
int rollno2
'tudent!'tring name,int rollno"
+
this.name 5 name2
this.rollno 5 rollno2
3
public static void main!'tring arg67"
+
'tudent s 5 new 'tudent !8sai8, 9"2
'tudent s: 5 new 'tudent !8raghav8, 9:"2
'tudent sJ 5 new 'tudent !8raghav8, 9:"2
'ystem.out.println!s.equals!s:""2 ; false
'ystem.out.println!s:.equals!sJ""2 ;false
3
3
r 55 r: ; reference #omparision.
r.equals!r:" ; reference.
'ote:- /n this case Object class .equals!" has e1ecuted which is meant for reference comparison but
based on our requirement it is recommended to override .equals!" for content comparison.
Ey over loading .equals!" we have to consider the following J cases )*
Case(: The meaning of equality.
Case): /n the case of heterogeneous objects we have to return false. !i.e" we have to handle
#lass#ast41ception to return false.
Case*: /f we are passing null as the argument we have return false. !i.e" we have to handle
,ull(ointer41ception to return false.
Overridden metods of equals:-
41)*
****
class 'tudent
+
'tring name2
int rollno2
'tudent!'tring name,int rollno"
+
this.name 5 name2
this.rollno 5 rollno2
3
public boolean equals!Object obj"
+
try
+
'tring name 5 this.name2
int rollno 5 this.rollno2
'tudent s: 5 !'tudent"obj2
'tring name: 5 s:.name2
int rollno: 5 s:.rollno2
if!name.equals!name:" FF rollno 55 rollno:"
+
return true2
3
else
+
return false2
3
3
catch !#lass#ast41ception c"
+
return false2
3
catch !,ull(ointer41ception e"
+
return false2
3
3
public static void main!'tring arg67"
+
'tudent s 5 new 'tudent !8sai8, 9"2
'tudent s: 5 new 'tudent !8raghav8, 9:"2
'tudent sJ 5 new 'tudent !8raghav8, 9:"2
'ystem.out.println!s.equals!s:""2 ; false
'ystem.out.println!s:.equals!sJ""2 ; true
'ystem.out.println!s.equals!null""2 ; false
3
3
Comparison bet$een +,,- operator and +.equals%&- :-
55 .equals%&
" /t is an operator and applicable for
both primitive and Object references.
" /t is a method applicable for Only
Object reference.
:" Kor Object reference r, r: r 55
r: is true iff both r and r: pointing
to the same object on the heap i.e it is
meant for reference comparision.
:" Iefault implementation available in
Object class is meant for reference
comparison only i.e r.equals!r:" is true iff
both r,r: are pointing to the same object
on the heap.
J" %e canDt override 55 operator. J" %e can override equals!" for content
comparison.
?" /K r F r: are incompatible then
r 55 r: results #ompile Time 4rror.
?" /f r F r: are incompatible then
r.equals!r:" is always false.
L" Kor any Object reference r, r 55
null returns false.
L" Kor any Object reference r,
r.equals!null" returns false.
.elationsip bet$een +,,- and .equals%& :-
/f r 55 r: is true then r.equals!r:" is always true.
/f r.equals!r:" is true, then r 55 r: need not to be true.
Contract /et$een .equals%& and asCode%& :-
" /f two objects are equal by .equals!" then their hashcodes must be equal.
41)*
/f r.equals!r:" is true then
r.hash#ode!" 55 r:.hash#ode is also true.
:" /f two Objects are not equal by .equals!" then their hash#ode may or may not be same.
J" /f the hash#odes of two Objects are equal then the objects may or may not be equal by .equals!"
?" /f the hash#odes of two Objects are not equal then the Objects are always not equal by .equals!".
To satisfy above contract when ever we are overriding .equals it is highly recommended to override
hash#ode!" also.
clone%& :-
The process of creating e1actly duplicate Object is called #lonning.
Object class contains the clone method to perform this.
protected native Object clone() throws CloneNotSupportedException
41)*
***
class Test implements #loneable
+
int i 5 92
int j 5 :92
public static void main!'tring arg67"throws #lone,ot'upported41ception
+
Test t 5 new Test!"2
Test t: 5 !Test"t.clone!"2
t.i 5 992
t.j 5 :992
'ystem.out.println!t:.i-8****8-t:.j"2 ; 9****:9
'ystem.out.println!t.i-8****8-t.j"2 ; 99****:99
3
3
All the Objects canDt have the capability to produce cloned Objects. Only cloneable objects having
that capability.
An Object is said to be cloneable iff the corresponding class implements java.lang.cloneable
interface.
/t doesnDt contain any methods it is a marker interface.
(rotected members can be accessible from outside package in the child classes but we should invoke
them by using child class reference only. That is parent class reference is not allowed to invoke
protected members from outside package.
class Test implements #loneable
+
public static void main!'tring arg67"throws #lone,ot'upported41ception
+
Case(: Object o 5 new Object!"2
Object o: 5 o.clone!"2 HH wrong
Case): Object o 5 new Test!"2
Object o: 5 o.clone!"2 HH wrong
Case*: Test t 5 new Test!"2
Object o 5 t.clone!"2 HH right
3
3
/t is highly recommended to override clone!" in our class like doMet!", do(ost!" methods.
#tring Class :-
Once we created 'tring objects we are not allowed to perform any changes in the e1isting object. /f you
are
trying to perform any changes with those changes a new 'tring object will be created this behavior is
nothing but CimmutabilityD Of the 'tring Objects.
41)
'tring s 5 new 'tring!8sai8"2
s.concat!8$aghav8"2
'ystem.out.println!s"2
O01:- sai

Once we created a 'tringEuffer object we are allowed perform any changes in the e1isting object only.
This behavior is nothing but CmutabilityD of the 'tringEuffer object.
41)*
****
'tringEuffer sb 5 new 'tringEuffer!8sai8"2
sb.append!8$aghav8"2
'ystem.out.println!sb"2
O01:- sai.agav
41)
sai
sai$aghav
s
$eady for Marbage
collection
41)*
****
'tring s 5 new 'tring!8sai8"2
'tring s: 5 new 'tring!8sai8"2
'ystem.out.println!s 55 s:"2 ; false
'ystem.out.println!s.equals!s:""2 ; true
/n 'tring class .equals method is overridden for content comparision.
'tringEuffer sb 5 new 'tringEuffer!8sai8"2
'tringEuffer sb: 5 new 'tringEuffer!8sai8"2
'ystem.out.println!sb 55 sb:" 2false
'ystem.out.println!sb.equals!sb:""22 false
/n the 'tringEuffer .equals method is not overridden for content comparison.
0ence Object class .equals method will be e1ecuted which is meant for reference comparison.
3ifference /et$een (String s = new String(sai)) and (String s = sai)?
String s = new String(sai):-
/n this case : objects will be created one is on the heap and the second one is on the scp!'tring
constant pool" and CsD is referring to heap object.

; Object #reation in '#( is Optional.
; /f the object is not available then only a new object will be created.
String s = sai:-
/n this case only one object will be created if it is not already available and CsD is referring to that
object. /f an object is already available in scp then CsD will refer that e1isting object only
instead of creating new object.
0eap
'#(
sai
sai
s
sai$aghav
sb
'ote:- The Object present in the scp is not eligible for garbage collection even though the object doesnDt
have any reference variables.
All the scp objects will be destroyed at the time of @AB shutdown.
41)*
****
'tring s 5 new 'tring!8sai8"2
'tring s: 5 new 'tring!8sai8"2
'tring sJ 5 8sai82
'tring s? 5 8sai82
'ote:- /n the scp there is no chance of : string objects with the same content i.e duplicate string objects
wonDt be present in scp.
Eut in the case of heap there may be a chance of duplicate string Objects.
41)*
***
'tring s 5 new 'tring!8sai8"2
s.concat!8$aghav8"2
'tring s: 5 8sai$aghav82
'ystem.out.println!s"2
'ystem.out.println!s:"2
O01:- sai
sai$aghav
0eap
'#(
sai
sai
sai
s
s:
sJ
s?
sai
'#(
'tring s 5 8spring82
'tring s: 5 s-8summer82
s.concat!8fall8"2
s:.concat!s"2
s5s-8winter82
'ystem.out.println!s"
O01:- springwinter
springsummer
Importance of #tring Constant 1ool%#C1&
/n our program if any string object is repeatedly going to use, we can create only one object in the
string constant pool and shared by several required references.
/nstead of creating several string objects with the same content creating only one object improves
performance of the system and memory utiliNation also. This is biggest advantage of '#(.
As the 'everal references pointing to the same object in '#( by using one reference if u r allowed to
change the content the remaining references may be effected. 0ence once we created a 'tring object we
r not allowed to change itDs content. /f u r trying to change with those changes a new object will
be created and there is no impact on the remaining references. This behavior is nothing but
immutability of the string objects.
'#( is the only reason why the 'tring Objects are immutable.
0eap
'#(
springsummer spring
springfall
summer
springsummerwinter
fall
springwinter
winter
s:
s
0eap
'#(
sai
sai$aghav
sai
$aghav
sai$aghav
s
s:
Important questions:
" %hat is the difference Eetween 'tring and 'tringEufferO
:" %hat is the meaning of immutabilityO
J" %hy the 'tring Objects are declared as immutableO
Interning of #tring Objects
class 'tringTest
+
public static void main!'tring67 args"
+
'tring s 5 new 'tring!8sai8"2
'tring s: 5 s.intern!"2
'tring sJ 5 8sai82
'ystem.out.println!s: 55 sJ"2
3
3
At any point of time if we have heap object reference we can find itDs equivalent '#( Object by
using intern!" method.
/f the equivalent '#( Object is not already available then intern!" method will create a new object in
the '#(.
41)
class 'tringTest
+
public static void main!'tring67 args"
+
'tring s 5 new 'tring!8sai8"2
'tring s: 5 s.concat!8raghav8"2
'tring sJ 5 s:.intern!"2
'tring s? 5 sairaghavG2
'ystem.out.println!sJ 55 s?"2
3
3
0eap
'#(
sai
sai
s
s:
#tring Class Constructor :-
" 'tring s 5 new 'tring!"2
#reates an empty 'tring Object.
:" 'tring s 5 new 'tring!'tring literal"2
J" 'tring s 5 new 'tring!'tringEuffer s"2
?" 'tring s 5 new 'tring!char 67 ch"2
char67 ch 5 +CaD,DbD,DcD,DdD32
'tring s 5 new 'tring!ch"2
'ystem.out.println!s"2
O01:--abcd
L" 'tring s 5 new 'tring!byte67 b"2
byte67 b 5 +99,9,9:,9J,9?32
'tring s 5 new 'tring!b"2
'ystem.out.println!s"2
O01:--defgh.
Important 4etods of #tring Class :-
" public char charAt!int inde1"2
41)*
'tring s 5 saiG2
'ystem.out.println!s.chatAt!:""2 OH(****i
'ystem.out.println!s.charAt!9""2 OH(****'tring/nde1OutOfEounds41ception.
:" The overloaded C-D and C-5D operators acts as concatenation operation only.
(ublic 'tring concat!'tring s"
41)* 'tring s 5 GsaiG2
s 5 s.concat!lestoG"2
s 5 s-GlestoG2
s -5 lestoG2
'ystem.out.println!s"2
0eap
'#(
sai
sairaghav
sai
raghav
sairaghav
s
s:
sJ
s?
J"
!i" (ublic Eoolean equals!Object o"
/t checks for content comparision. in this case of heterogenous objects this method returns
false.
'tring s 5 8lesto82
'ystem.out.println!s.equals!8P4'TO8""2
'ystem.out.println!s.equals!8lesto8""2
!ii" (ublic Eoolean equals/gnore#ase!'tring s"2
'tring s 5 8lesto82
'ystem.out.println!s.equals/gnore#ase!8P4'TO8""2
'ote:- Qsually for validating username!where the case is not important method, we can use equals
ignore
case method" Eut for validating passwords we can use eauals method where the case is important.
?" public int length!"2
'tring s 5 8lesto82
'ystem.out.println!s.length"2 OH()*#.4
'ystem.out.println!s.length!""2 OH()*L
length is the variable applicable for array objects,Eut length!" is the method applicable for 'tring
Object.
L" public 'tring replace!char old, char new"2
'tring s 5 8ababa82
'ystem.out.println!s.replace!&a&,&b&""2 OH()*bbbbb
<" !i"public 'tring substring!int begin"2
'tring s 5 8abcdefgh82
'ystem.out.println!s.substring!J""2OH()*defgh
!ii"public 'tring substring!int begin, int end"2
$eturns the characters from !begin" inde1 to !end*" inde1.
'tring s 5 8abcdefgh82
'ystem.out.println!s.substring!J,<""2 OH()*def.
R" public 'tring toPower#ase!"2
public 'tring toQpper#ase!"2
41)* 'tring s 5 $oyal#hallangeG2
'ystem.out.println!s.toPower#ase!""2
'ystem.out.println!s.toQpper#ase!""2
>" public 'tring trim!"
to remove blank spaces present at 'tarting and 4nding of the 'ting. Eut not middle blank
spaces.
'tring s 5 8 raju : 82
'ystem.out.println!s.trim!""2 OH()*raju :
=" public int inde1Of!char ch"2
returns first occurance inde1
public int last/nde1Of!char ch"2
5&Ceck te follo$ing question
#tring s( , 6goldendrop67
#tring s) , s(.to8pperCase%&7
#tring s* , s(.to9o$erCase%&7
#!stem.out.println%s( ,, s)&7
#!stem.out.println%s( ,, s*&7
:at is te output;
O01 :- false< true
Eecause of some runtime operation, if a 'tring object is required to create it should always create on
the heap only.
Iue to this runtime operation if there is no change in the content then new object wonDt be created.
#tring/uffer Class
Constructors of StringBuffer:
'tringEuffer sb 5 new 'tringEuffer!"2
#reates an empty 'tringEuffer object with default initial capacity <.
/f it reaches ma1 capacity then a new 'tringEuffer object will be created with new capacity is
41)*
'tringEuffer sb 5 new 'tringEuffer!"2
'ystem.out.println!sb.capacity!""2
sb.append!8abcdefghijklmnop8"2
sb.append!8q8"2
'ystem.out.println!sb.capacity!""2 *; J?
41)*
'tringEuffer sb 5 new 'tringEuffer!?9"2
capacit! , %currentcapacit! = (& > )
MOPI4,I$O( goldendrop
0eap
'#(
s
sJ
s:
'ystem.out.println!sb.capacity!""2
sb.append!8abcdefghijklmnop8"2
.
.
.
.
#reates an empty 'tringEuffer with the required initial capacity.
'tringEuffer sb 5 new 'tringEuffer!'tring s"2
#reates an equivalent 'tringEuffer object for the given string with
41)*
'tringEuffer sb 5 new 'tringEuffer!PestoG"2
'ystem.out.println!sb.capacity!""2 ; < - L 5 :
5& :ic of te follo$ing is a valid constructor for te #tring/uffer but not for te #tring.
(& % &
)& %int capacit!&
*& %#tring s&
Important 4etods of #tring/uffer class
"public int length!"2
:"public int capacity!"2
J"public char charAt!int inde1"2
41)*
'tringEuffer sb 5 new 'tringEuffer!PestoG"2
'ystem.out.println!sb.charAt!J""2
'ystem.out.println!sb.chatAt!9""2 ; 'tring/nde1OutOfEounds41ception.
?"public void set chatAt!int inde1, char ch"2
$eplaces the character present at specified inde1 with the provided char.
41)*
'tringEuffer sb 5 new 'tringEuffer!G"2
sb.set#harAt!J,DaD"2
'ystem.out.println!sb"2
L"public 'tringEuffer append!'tring s"
Allows all overloaded methodsS.int, float, byte, boolean, char67, byte67,S.etc
<"public 'tringEuffer insert!int inde1, 'tring s"
41)*
'tringEuffer sb 5 new 'tringEuffer!PestoG"2
sb.insert!:,GrajuG"2
capacit! , s.lengt%& = (?
'ystem.out.println!sb"2 'tringEuffer sb 5 new 'tringEuffer!'tring s"2
R" public 'tringEuffer delete!int start, int end"
delete characters from start to %end-(&
'tringEuffer sb 5 new 'tringEuffer!abcdefghG"
sb.delete!:,<"2 O01:-abg
>" public 'tringEuffer delete charAt!int inde1"
deleting #haracter located at specified inde1.
=" public 'tringEuffer reverse!"2
41)*
'tringEuffer sb 5 new 'tringEuffer!rajuG"2
'ystem.out.println!sb.reverse!""2 O01:-ujar.
9" public void setPength!int requiredlength"2
41)* 'tringEuffer sb 5 new 'tringEuffer! aishwaryaabhishakG"2
sb.setPength!>"2
'ystem.out.println!sb" O01:-ais$ar!a
'ote:-All the methods which are available in the 'tringEuffer are 'unchroniNed or it may effect the
performance of the system.
%e can overcome this problem by using 'tringEuilder.
#tring/uilder
'tringEuilder class e1actly similar to 'tringEuffer !including constructors and methods" 41cept the
following....
#tring/uilder #tring/uffer
,o method is synchroniNed All methods are 'ynchroniNed
'tringEuilder is not thread safe 'tringEuffer is thread safe
(erformance is high (erformance is very low
Available only in .L version Available from .9 version
Caining of 4etods :-
Kor most of the methods in 'tring and 'tringEuffer the returntypes are the same 'tring and
'tringEuffer objects only.
After Applying a method we are allowed to call another method on the result which forms a method
change.
sb.m!".m:!".mJ!".m?!".mL!"S..2
All these method calls will e1ecute from left to right.
'tringEuffer sb 5 new 'tringEuffer!rajuG"2
sb.append!8software8".reverse!".insert!:,8abc8".delete!:,L".append!81yN8"2
:rapper Classes :-
The main objectives of wrapper classes are)
" To %rap primitives into object form. 'o that we can handle primitives also just like objects.
:" To Iefine several utility functions for the primitives!like converting primitive to the string form etc."
valueOf!"
111Aalue!"
parseT11!"
to'tring!"
Constructing :rapper objects b! using constructors:
4very %rapper class contains : constructors one can take the corresponding primitive as the
argument and the other can take the corresponding string as the argument.
41)
/nteger / 5 new /nteger!9"2
/nteger / 5 new /nteger!9G"2
/f the 'tring is unable to convert into the number form then we will get runtime e1ception saying
,umberformat41ceptionG.
41) /nteger / 5 new /nteger!tenG"2
Float class contains : constructors which can take double 'tring as argument.
Kloat f 5 new Kloat!9.Lf"2
Kloat f 5 new Kloat!9.LfG"2
Kloat f 5 new Kloat!9.L"2
Kloat f 5 new Kloat!9.L"2
Caracter class contain only one constructor which can take char as the argument i.e character class
doesnDt contain a constructor which can take string as the argument.
41)*
****
#haracter ch 5 new #haracter!&a&"2 ; valid.
#haracter ch 5 new #haracter!8a8"2 2 not valid.
Eoolean class contains : constructors one can take boolean primitive. Other can take string
argument. /f u r providing boolean primitive as the argument the. The allowed values are true or
false.
#ase is not important if the content contains CT$Q4D then it is considered as true other wise it
considered as false.
5& Wic of te following are !alid ?
Eoolean b 5 new Eoolean!true"2 OH()*true
Eoolean b 5 new Eoolean!KAP'4"2 T
Eoolean b 5 new Eoolean!falseG"2 OH()*false
Eoolean b 5 new Eoolean!TrQ4G"2 OH()*true
Eoolean b 5 new Eoolean!rajuG"2 OH()*false
Eoolean b 5 new Eoolean!yesG"2 OH()*false
5& Consider "e #ollowing:
Eoolean b 5 new Eoolean!yesG"2
Eoolean b: 5 new Eoolean!,oG"2
'ystem.out.println!b.equals!b:""2
" #.4
:" $.4
J" true
?" false
Ans) J !true"
:rapperClasses constructor arguments
Eyte byte !or" string
'hort short !or" string
/nteger int!or" string
Pong long !or" string
Kloat float !or" string !or" double
Iouble double !or" string
#haracter char
Eoolean boolean !or" string
valueOf% & metods :-
version(:
All the wrapper classes e@cept caracter class contains the valueOf!" method for converting
string to corresponding %rapper Object.
public static wrapper valueOf!'tring s"2
41)
/nteger / 5 /nteger.valueOf!C9D"2
Kloat K 5 Kloat.valueOf!9.LG"2
Eoolean E 5 Eoolean.valueOf!rajuG"2
#haracter ch 5 #haracter.valueOf!9G"2 T; #.4
Aersion):
All /ntegral wrapper classes Eyte, 'hort, Pong, /ntegerG #ontains the following valueOf!"
method.
public static wrapper valueOf!'tring s, int radi1"2
The allowed base of radi1 is to J<.Eecause ,umerics!9" ,Alphabets!:<" finally 9-:< 5J<
41)
/nteger / 5 /nteger.valueOf!99G, :"2
'ystem.out.println!/"2 O01:- B*
Aersion*:
4very %rapper class including character class contains the following valueOf!" method to
convert $rimiti!e to wrapper object form.
public static wrapper valueOf!primitive p"2
41)
/nteger / 5 /nteger.valueOf!9"2
#haracter ch 5 #haracter.valueOf!CaD"2
Eoolean E 5 Eoolean.valueOf!true"2
Aersion, Aersion: ; 'tring to wrapper object.
AersionJ ; primitive to wrapper object.
@@@Aalue%& metod :-
4very wrapper class 41cept character and Eoolean classes contains the following 111Aalue!" methds
for converting wrapperObject to primitive.
public int intAalue!"2
public byte byteAalue!"2
public short shortAalue!"2
public long longAalue!"2
public float floatAalue!"2
public int doubleAalue!"2
41)
/nteger / 5 /nteger.valueOf!J9"2
'ystem.out.println!/.byteAalue!""2
'ystem.out.println!/.shortAalue!""2 ;*:<
'ystem.out.println!/.intAalue!""2 ;J9
'ystem.out.println!/.langAalue!""2 ;J9
'tringH(rimitive
%rapper object
valueOf!"
'ystem.out.println!/.floatAalue!""2 ;J9.9
'ystem.out.println!/.doubleAalue!""2 ;J9.9
Caracter class contain carAalue%& method to return char primitive for the given character
wrapper object.
public char charAalue!"2
41)*
character ch 5 new character!CaD"2
char ch 5 ch.charAalue!"2
'ystem.out.println!ch"2 O01:- a
/oolean class contains booleanAalue!" method to return boolean primitive for the given boolean
objective.
Eoolean E 5 Eoolean.valueOf!8Tea Ereak8"2
boolean b 5 E.booleanAalue!"2
'ystem.out.println!b"2 O01:-false
'ote:- /n total J> 111Aalue methods are possible !!< T <" - - " 5 J>.
parseC@@%& metods :-
version(:
4very wrapper class e1cept character class contains the following parseT11!" method for
converting String to $rimiti!e type
public static primitive parseT11!'tring s"2
41)
int i5 /nteger.valueOf!9G"2
Eoolean b 5 Eoolean.parseEoolean!trueG"2
Aersion):
/ntegral wrapper classes!Eyte, 'hort, /nteger and long " contains the following parseT11!"
method.
public static primitive parseT11!'tring s, int radi1 "2
41)*
%rapper object (rimitive Type
111Aalue!"
int i 5 /nteger.parse/nt!99G, :"2
'ystem.out.println!i"2 O01:-B*
to#ting%&metods :-
version(:
All wrapper #lasses contains an instance method to'tring!" for converting wrapper object to
'tring type. This is overriding method of object class to'tring!" method.
$ublic String toString()%
/nteger / 5 new /nteger!9"2
'tring s 5 /.to'tring!"2
'ystem.out.println!s"2 O01:-(D
Eoolean E 5 new Eoolean!rajuG"2
'tring s 5 E.to'tring!"2
'ystem.out.println!s"2 O01:-false
Aersion):
4very wrapper class contains the following static to'tring!" for converting primitive to 'tring
representation..
$ublic static String toString($rimiti!e $)%
'tring s 5 /nteger.to'tring!9"2
'ystem.out.println!s"2 O01:-(D
'tring s 5 Eoolean.to'tring!true"2
'ystem.out.println!s"2 O01:-true
%rapper object
'tring
to'tring!"
'tring
(rimitive Type
parseTTT!"
Aersion*:
/nteger and long classes contains the following to'tring!" to return the 'tring in the specified
radi1.
$ublic static String toString(int&long' int radi( )%
'tring s 5 /nteger.to'tring!?J, :"2
'ystem.out.println!s"2 O01:-E(D(D((
'tring s 5 /nteger.to'tring!?J, >"2
'ystem.out.println!s"2 O01:-EF*
AersionB:
/nteger and Pong classes contains the following methods to return specified radi1 'tring
form.
$ublic static String toBinaryString(int&long' l)%
$ublic static String to)ctalString(int&long' l)%
$ublic static String to*e(String(int&long' l)%
'tring s 5 /nteger.toEinary'tring!?J"2
'ystem.out.println!s"2 O01:-E(D(D((
'tring s 5 /nteger.toOctal'tring!?J"2
'ystem.out.println!s"2 O01:-EF*
'tring s 5 /nteger.to0e1'tring!?J"2
'ystem.out.println!s"2 O01:-E)?)
1artial Gierarc! of java.lang 1ackage
Object
Aoid is also considered as wrapper class.
'tring 'tringEuff
er
'tringEu
ilder
Bath Aoid
Eoolean
#haract
er
S......
,umber
Eyte
'hort
/nteger
Pong Kloat

Iouble
'tring, 'tingEuffer, 'tringEuilder and all wrapper class are final.
/n Addition to 'tring Object all wrapper class objects also immutable.
Eoolean and #haracter wrapper classes are not child classes or ,umber class

You might also like