Professional Documents
Culture Documents
STRINGS
1. A String is a non-primitive data type
• One main point about the primitive and non-primitive that is primitive
data type have fixed size of memory and whereas non-primitives does
not have fixed size
• As String references a memory location where data is stored in the
heap memory or String Constant Pool(SCP)i.e, it references to a
memory where an object is actually placed.
• Thus the variable of a non-primitive data type is also called reference
variable. This reference variable lives on the stack memory and the
object which it points always lives on the heap memory.
2. A String is the sequence of characters or say
s1 Java Java
String Constant
Programming
Pool
srinivas
s2
s4
• As from the memory areas, we observed that inside heap and
SCP objects are created but 2 objects are created by the new
keyword and also SCP object
• But take a note that whether Garbage collector will come into
picture to free the space as we learnt about it.
• No, it won’t because the JVM internally creates reference to
the String object
• But for SCP it directly creates and Garbage collector is not
applicable
• Coming to the important point that when the String is created
with 2 objects it slows down the project when compared to
directly pointing the String object that is
String s=“Java”;
s Java
Java SCP
Javapython python
Python Python
s programmi programmi
ng ng
• In Strings, if we want to have some changes in our String it
creates separate object but it won’t alters or changes the
previous String.
• The above gives rise to immutable nature. In String Constant
Pool or String Literal Pool it creates the object again because it
looks like String literal.
• “String Immutability” always related to String objects.
What about + symbol
Used in our program?
• If suppose we use (+) symbol which is used for concatenation
in our programs, when we use the symbol instead concat()
method it also returns us the same result as of the method
SCP
• Strings and especially String literals are widely used in
typical Java code. And they are immutable.
• And being immutable allowed to cache String to save memory
and increase performance (less effort for creation, less
garbage to be collected).
Methods Description
replace() replaces the specified old character with the specified new character
String s=“Java”;
s.concat(“programming”);
S.o.p(s);//java
sb.append(“programming”);
S.o.p(sb);//java programming
• There is no separate object to be created for programming, it changes
in a single object.
• If we come to know that the String has frequent changes then we
choose StringBuffer class, whereas if we didn’t want any changes and
make the String object as safer one then we have to choose String
• Public class final StringBuffer implements
serializable,CharSequence,comparable
Class constructors
1 StringBuffer()
This constructs a string buffer with no characters in it and an initial
capacity of 16 characters.
2 StringBuffer(int capacity)
This constructs a string buffer with no characters in it and the specified
initial capacity.
3 StringBuffer(String str)
This constructs a string buffer initialized to the contents of the specified
string.
.equals() method and == operator:
• S1 s2 s3 s4 s5 s6…………..
• String s1=new String(“java”);
• String s2=new String(“java”);
• S.O.P(s1==s2);//false
Heap
SCP
s1
Java
Java s3
program
java
s2 s4
ensureCapacity()
This method is used to ensure minimum capacity
of StringBuffer object.
• If the argument of the ensureCapacity() method is less than the
existing capacity, then there will be no change in existing capacity.
• If the argument of the ensureCapacity() method is greater than the
existing capacity, then there will be change in the current capacity
using following rule: newCapacity = (oldCapacity*2) + 2.