You are on page 1of 6

St

ri
ngi
nJav
a-
St
ri
ngi
sthecl
asst
hatr
epr
esent
ssequenceofchar
act
er.
Packagei
sJav
a.Lang.
St
ri
ngcl
assi
mpl
ement
sSer
ial
i
zabl
e,compar
abl
e,charsequencei
nter
face.
St
ri
ngi st
hei
mmut
abl
e,oncest
ri
ngobj
ecti
scr
eat
ed,
itcannotchangedbutnewst
ri
ngobj
ecti
s
cr
eated.
Formut
abl
ecl
ass,
youcanuseSt
ri
ngbuf
ferandSt
ri
ngbui
l
dercl
ass.
Howt
ocr
eat
ethest
ri
ngobj
ect
?
1.St
ri
ngl
i
ter
al
2.Bynewkey
wor
d
1.St
ri
ngl
i
ter
al-
I
tiscr
eat
edbyusi
ngdoubl
equot
es.

Exampl
e-St
ri
ngs=”v
eloci
ty”
;

Eacht
imewhenyoucreatestri
ngli
ter
al,
theJVM checkstri
ngconstantpoolfi
rst,
i
fthestri
ngobj
ecti
salr
eadypresentint
hepool,r
eferencetopooledi
nst
anceisr et
urned.
I
fstr
ingdoesnotpr
esenti
nthepool,newstri
nginstanceiscr
eatedandplacedinpool.

Exampl
e-St
ri
ngs1=”
vel
oci
ty”
;
St
ri
ngs2=”v
eloci
ty”
;//
wil
lnotcr
eat
ethenewi
nst
ance.

I
ntheaboveexampl e,onlyoneobjectwi l
lbecreated,
fir
stl
yJVM wi llnotf
ind
anyst
ringobj
ectwithvalue“veloci
ty”i
nthest r
ingconstantpool,soitwillcreatenew
obj
ect.Aft
ert
hatitwil
lfindstri
ngwithvalue=“v el
oci
ty”inpool,
itwil
l notcreatethenew
obj
ectbutwil
lret
urnreferencetosamei nstance.

v
eloci
ty
S1
Str
ing
S2 const
antpool

WhyJav
ausest
heconceptofst
ri
ngl
i
ter
al?
Tomakethejavamorememor
yef
fi
cient(
becausenonewobj
ecti
scr
eat
edi
fit
exi
stal
readyi
nstr
ingconst
antpool
.)
2.BynewKey
wor
d-
Exampl
e-St
ri
ngs=newSt
ri
ng(
“pune”
);
/
*cr
eat
etwoobj
ect
s*/

Insuchcase,JVM wil
lcr
eatethenewStr
ingobjecti
nnormal
(non-
pool
)heapmemory
andli
teral
“pune”wil
lbeplacedi
nstr
ingconst
antpool.Thev
ari
abl
esrefert
oobj
ecti
n
heap(non-
pool)
.

HowmanyObj
ect
swi
llbecr
eat
ed?
?

Str
ings1=newSt r
ing("
vel
oci t
y "
);/
/Twoobj ectscreated.Onei nheapandonei
n
SCP.S1poi
ntstoheapobj ect
Str
ings2="v
elocit
y";/
/Noobj ectcreat
ed.s2pointtoSCPv el
ocit
y
Str
ings3=newSt r
ing("
tr
aining");/
/TwoObj ect
scr eated.Oneinheapandonein
SCP.s3poi
nttoHeap
Str
ings5=newSt r
ing("
vel
oci t
y "
);/
/OneObj ectcreatedinHeap
Str
ings6="t
rai
ning";/
/Noobj ectcr eat
ed.s6pointtoSCPt r
aini
ng

Tot
al:
5obj
ect
swi
l
lbecr
eat
ed

Not
e:
Wheneverweareusingnewoperat
orcompul
soryanewobjectwil
lbecreatedontheHeap.
Theremaybeachanceofexisti
ngtwoobj
ectswithsamecont
entont heheapbutther
eisno
chanceofexi
sti
ngtwoobject
swithsamecontentonSCP.i
.e.
,dupl
icateobject
spossi
blei
nthe
heapbutnoti
nSCP
Forever
ySt r
ingConst
antoneobj
ectwil
lbecreat
edinSCP.Becauseofr
unt
imeoperat
ioni
fan
obj
ectisrequir
edtocr
eatecompul
sorythatobj
ectshoul
dbeplacedont
heheapbutnotSCP.
Exampl
e-1
packagecom.
vel
oci
ty;

publ
iccl
assSt
ri
ngDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ri
ngs1="vel
ocit
y";
St
ri
ngs2=newSt ri
ng("
vel
oci
ty")
;
Sy
st out
em. .pri
ntl
n(s1==s2);/ /
fal
se
Sy
st out
em. .pri
ntl
n(s1.
equal
s(s2))
;//t
rue

}
}

Exampl
e-2
packagecom.
vel
oci
ty;

publ
iccl
assSt
ri
ngDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

Str
ings1="vel
ocit
y";
Str
ings2=newSt ri
ng("
pune"
);
s2=s1;
Syst out
em. .pri
ntl
n(s1==s2);//t
rue
Syst out
em. .pri
ntl
n(s1.
equal
s(s2)
);/
/tr
ue

}
}

Out
put
-
Exampl
e-3
packagecom.
vel
oci
ty;

publ
iccl
assSt
ri
ngDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ri
ngs1=newSt
ri
ng(
"vel
oci
ty"
);
St
ri
ngs2=newSt
ri
ng(
"pune"
);
s2=s1;

Sy
st out
em. .pr
int
ln(
s1==s2);//t
rue
Sy
st out
em. .pr
int
ln(
s1.
equal
s(s2)
);//t
rue

}
}

Out
put
-

Exampl
e-4
packagecom.
vel
oci
ty;

publ
iccl
assSt
ri
ngDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

publ
iccl
assPr
oper
ti
esFi
l
eDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ri
ngs1="vel
ocit
y";
St
ri
ngs2=newSt ri
ng("
pune"
);
Sy
st out
em. .pri
ntl
n(s1==s2);//
fal
se
Sy
st out
em. .pr
int
ln(
s1.
equal
s(s2)
);//
false
Sy
st out
em. .pr
int
ln(
s1.
hashCode(
));/
/100
Sy
st out
em. .pr
int
ln(
s2.
hashCode(
));//101

}
}

Out
put
-
Exampl
e-5
packagecom.
vel
oci
ty;

publ
iccl
assSt
ri
ngDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ri
ngs1=newSt r
ing(
"vel
ocit
y")
;
St
ri
ngs2=newSt r
ing(
"pune"
);
Sy
st out
em. .pr
int
ln(
s1==s2) ;//f
alse
Sy
st out
em. .pr
int
ln(
s1.equal
s(s2)
);//fal
se

}
}

Out
put
-

I
nter
ningofSt
ri
ngObj
ect
s:
Byusingheapobjectr
efer
ence,
ifwewantt
ogetcor
respondi
ngSCPobj
ect,
thenweshoul
dgo
fori
nter
n()method.

publ
icst
aticvoidmain(Str
ing[
]ar
gs){
St
rings1=newSt ri
ng("
vel
oci
ty"
);
St
rings2=s1. i
nter
n();
System.out
.pri
ntl
n(s1==s2);
//fal
se
St
rings3=" vel
ocit
y";
System.out
.pri
ntl
n(s2==s3);
//t
rue
}

You might also like