wellnwill Marathahalli Bangalore

String Handling

In java, string is a predefined final class in java.lang package whose object represents character array.

Ex- String s="raj';

String class is immutable class i.e. once string object is created the same object cannot be modified.

Once we create a string object we cannot perform any changes in the existing object if we are trying to modify the same string object then JVM creates a new object with those changes. String object is unchangeable i.e. immutable.

Creation of string object

By using String Literal

If we create string object by using string literal then string object will be created in SCP area (string constant pool).

Ex- String st1=”Raj”;
String st2=”Raj”;  // new object will not be created.

String constant pool doesn’t allow duplicate object.

class Test219
{
public static void main(String args[])
{
 String s1="java";
 System.out.println(s1);
 s1="language";
 System.out.println(s1);
 s1=s1.concat(" package");
 System.out.println(s1);
}
}
Output-java, language, language package

If we create string object by using new keyword then string object will be created in heap area as well as string constant pool area.

Ex- String st=new String(“Ram”);


Commonly used constructors of String class

Constructor description
public String() creates an empty string object
public String(String s) creates a string object on the heap for the given string literal
public String(byte b[]) creates an equivalent string object for the given byte array
public String(char c[]) creates an equivalent string object for the given char array
class Test220{
public static void main(String args[])
{
 String s1=new String("java9");
 String s2=new String("java9");
 String s3="java9";
 String s4="java9";
 System.out.println(s1==s2);//false
 System.out.println(s3==s4);//true
 System.out.println(s1.equals(s2));//true
 System.out.println(s1.equals(s4));//true
}
}

Commonly used methods in String class


public int length()
String charAt()
public Boolean equals(Object o)
public Boolean equalsIgnoreCase(String s)
public int indexOf(int char)
public int lastIndexOf(int char)
public String substring(int startindex)
public String substring(int startindex, int endindex)
public String toLowerCase()
public String toUpperCase()
public String trim()
public char[] toCharArray()

Concept

String st1=new String("dheeraj");
st1.concat("bajpai");
String st2=st1.concat("dheeraj1");
st1=st1.concat("bajpai1");
String st3=new String(“RAM”);
st3=st3.concat(“SITA”)
System.out.println(st1);
System.out.println(st2);
System.out.println(st3);
Heap SCP
dheeraj dheeraj
dheerajbajpai (because of runtime operation) bajpai (constant,literal)
dheerajdheeraj1 dheeraj1
dheerajbajpai1 bajpai1
RAM RAM
RAMSITA SITA

Example 2


String s1=new String(“java”);
String s2=s1.toLowerCase();
String s3=s1.toUpperCase();
String s4=s3.toLowerCase();

Important point


Because of runtime operation if there is a change in the content then with those changes a new object will be created on heap.

If there is no change in the content then existing object will be reused and new object will not be created. Whether the object present in heap or scp, the rule is same.

Heap String constant pool
java (s1,s2) java
JAVA (s3) ---------
java (s4) ----------

compareTo() method

compareTo() method compares two strings character by character.

compareTo() returns three values:

if object1 > object2 => return positive number  
if  object1 < object2 => return negative number  
if  object1 == object2 => return zero  
class Test223
{
public static void main(String a[]){
String s1="dheeraj";
String s2="atul";
String s3="atul";
String s4="dheeraj";
String s5="DHEERAJ";
System.out.println(s1.compareTo(s2));//3
System.out.println(s3.compareTo(s1));//-3
System.out.println(s2.compareTo(s3));//0
System.out.println(s1.compareTo(s4));//0
System.out.println(s1.compareToIgnoreCase(s5));//0
}
}







class Test222
{
public static void main(String args[])
{
 String s1="java";
 String s2="JAVA";
 System.out.println(s1.length());//4
 System.out.println(s1.equals(s2));//false
 System.out.println(s1.equalsIgnoreCase(s2));//true
 System.out.println(s1.indexOf('a'));//1
 System.out.println(s1.lastIndexOf('a'));//3
 System.out.println(s1.charAt(3));//a
 System.out.println(s1.substring(2));//va
 System.out.println(s1.substring(2,3));//v
 System.out.println(s2.toLowerCase());//java
 System.out.println(s1.toUpperCase());//JAVA
 String s3=" java program ";
 System.out.println(s3.trim());//java program
 char ch[]=s1.toCharArray();
 System.out.println(ch[2]);//v
 }
}

StringBuffer class


It is mutable class i.e. we can modify the object of StringBuffer class. All the methods of StringBuffer class are synchronized i.e. thread safe (at most one thread can access this class methods at a time ).

StringBuffer s1=new StringBuffer()—creates an empty buffer with default capacity 16.
New capacity=(current capacity+1)*2;
StringBuffer s2=new StringBuffer(String s);

Creates an equivalent StringBuffer object for the given string with capacity =s.length()+16;

Ex- StringBuffer s2=new StringBuffer(“Dheeraj”); Capacity=7+16=23

StringBuilder class


It is mutable class i.e. we can modify the object of StringBuilder class.

All the methods of StringBuilder class are non-synchronized i.e. thread unsafe (at a time all thread can access this class methods simultaneously).

Footer with Map

Top