String
StringBuffer
Immutable
Mutable
String
s=
new
String
(
"karthik"
);
StringBuffer
sb=
new
StringBuffer
(
"karthik"
)
s.concat(
"reddy"
); sb.append(
"reddy"
);
System
.out.println(s);
System
.out.println(sb);
O/P:karthik O/P:karthikreddy
--->once we created a
String
object ---->once we created a
StringBuffer
object
we can
'
t perform any changes in the existing we can perform any changes in the existing
object.
If
we are trying to perform any object.
It
is nothing but mutablity of
changes with those changes a
new
object of a
StrongBuffer
object
will be created.
It
is nothing but
Immutability
of a
String
object
Use
String
--->
If
you require immutabilty
Use
StringBuffer
---->
If
you require mutable + threadsafety
Use
StringBuilder
--->
If
you require mutable + with out threadsafety
String
s=
new
String
(
"karthik"
);
--->here
2
objects will be created one is heap and the other is in stringconstantpool(scp) and s is always pointing to heap object
String
s=
"karthik"
;
--->
In
this
case
only one object will be created in scp and s is always pointing to that object only
Tuesday, 29 October 2013
difference between string and stringbuffer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment