You are on page 1of 16

String Concept

• In java, string is basically an object that represents sequence of


char values. An array of characters works same as java string.

• Java String class provides a lot of methods to perform


operations on string such as compare(), concat(), equals(),
split(), length(), replace(), compareTo(), intern(), substring() etc.
• The java.lang.String class implements:
• 1.Serializable
• 2.Comparable
• 3.CharSequence
• 1.Serializable:
• Serialization in java is a mechanism of writing
the state of an object into a byte stream.
• It is mainly used in Hibernate, RMI, JPA, EJB
and JMS technologies.
• Serializable is a marker interface (has no data
member and method). It is used to "mark"
java classes so that objects of these classes
may get certain capability.
• 2.Comparable :
• Java Comparable interface is used to order the
objects of user-defined class. This interface is
found in java.lang. package and contains only
one method named compareTo(Object). It
provide single sorting sequence only i.e. you
can sort the elements on based on single data
member only. For example it may be rollno,
name, age or anything else.
• 3.CharSequence:
• The Char Sequence interface is used to
represent sequence of characters. It is
implemented by String, StringBuffer and
StringBuilder classes. It means, we can create
string in java by using these 3 classes.
How to create String object:
• There are two ways to create String objects:
• 1.By using literals
• 2.By new keyword
• By using literals:

• Java String literal is created by using double quotes. For Example:


• String s="welcome"; 
•  
• Each time you create a string literal, the JVM checks the string constant
pool first. If the string already exists in the pool, a reference to the pooled
instance is returned. If string doesn't exist in the pool, a new string
instance is created and placed in the pool. For example:

• String s1="Welcome";  
• String s2="Welcome";//will not create new instance  

• Note: String objects are stored in a special memory area known as string
constant pool.
Why java uses concept of string literals

• To make Java more memory efficient (because


no new objects are created if it exists already
in string constant pool).
• By new keyword:
• String s=new String("Welcome");//creates two
 objects and one reference variable.  
• In such case, JVM will create a new string
object in normal(non pool) heap memory and
the literal "Welcome" will be placed in the
string constant pool. The variable s will refer
to the object in heap(non pool).
String Methods:
• Methods(): • Description
• char charAt(int index) • returns char value for the
particular index.
• returns substring for given
• String substring(int
begin index.
beginIndex)
• checks the equality of string
• boolean with object.
equals(Object another)
• concatinates specified
• String concat(String str) string.
Immutable String
• In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
• Once string object is created its data or state can't be changed but a new string object is created.

• For example:
• String s="Sachin ";  
•  s. concat(" Tendulkar ");
• Output:Sachin

• Here Sachin is not changed but a new object is created with Sachin Tendulkar. That is why string is
known as immutable.

•  two objects are created but s reference variable still refers to "Sachin" not to "Sachin Tendulkar".
• But if we explicitely assign it to the reference variable, it will refer to "Sachin Tendulkar" object.
•   String s="Sachin";  
•   s=s.concat(" Tendulkar"); 
• Output:Sachin Tendulkar
Why Strings are immutable in java:
• Because java uses the concept of string literal.
Suppose there are 5 reference variables , all
refers to one object “Sachin". If one reference
variable changes the value of the object, it will
be affected to all the reference variables. That
is why string objects are immutable in java.
Java String Compare:
• There are three ways to compare string in
java:
• 1.By equals() method
• 2.By = = operator
• 3.By compareTo() method
Java String Buffer:
• Java StringBuffer class is used to create
mutable (modifiable) string. The String Buffer
class in java is same as String class except it is
mutable i.e. it can be changed.
• Note: Java String Buffer class is thread-safe
i.e. multiple threads cannot access it
simultaneously. So it is safe and will result in
an order.
What is mutable string:
• A string that can be modified or changed is
known as mutable string. String Buffer and
String Builder classes are used for creating
mutable string.
• For example:
• String Buffer sb =new String Buffer("Hello ");  
• sb.append ("Java");//now original string is
changed.  
Java String Builder
• Java String Builder class is used to create
mutable (modifiable) string. The Java String
Builder class is same as String Buffer class
except that it is non-synchronized.

You might also like