String Handling -


  • String is a collection of characters.
  • In java string is implemented as objects of String class contained in java.lang package.
  • String class objects are immutable i.e. once you have defined a String object representing a certain string after that you cannot change its contents.
  • But this does not mean that you cannot perform any operations on these strings. Any time you make changes to a string, a new object representing that modified string will be created.
  • Java also supports two more classes StringBuilder and StringBuffer contained in java.lang package to create strings. Strings created by these two classes are mutable i.e. you are free to change contents of these strings.

Creating String -

Java provides the various ways to create strings. String class have several constructors to create strings -

1- To create an empty string you can call its default constructor like this -
String s1 = new String();
It will create an object of string containing no characters i.e. an empty string.

2- To create string from character array String class support the following constructor -
String(char CharArray[]);      
        
For example -
Char  ch[] = {'W' ,'E'', 'L', 'C', 'O', 'M', 'E'};  
String s1 = new String(ch);
        
Here s1 will represent a string - "WELCOME".

3- We can also create a string containing only defined subrange of array elements using the following constructor -
String(char CharArray[], int StartIndex, int NumOfChars);
Here StartIndex refers to index of array at which subrange starts and NumOfChars refers to number of characters you want to include in string.
For example -
char  ch[] = {'W' ,'E', 'L', 'C', 'O', 'M', 'E'};  
String s1 = new String(ch, 3, 4);
Here s1 will represent a string - "COME".

        

Example -

public class Test {

    public static void main(String[] args) {

        String s1 = new String(); 
        char ch[] = {'W','E','L','C','O','M','E'};
        String s2 = new String(ch);
        String s3 = new String(ch, 3, 4);
        System.out.println("s1 = "+s1);
        System.out.println("s2 = "+s2);
        System.out.println("s3 = "+s3);       
    }
}    
        

Output -

s1 = 
s2 = WELCOME
s3 = COME

4- We can also create string from a byte array containing ASCII values of characters using the following constructors -
String(byte ASCIIChars[]);
String(byte ASCIIChars[], int StartIndex , int NumOfChars);
For example -
byte b[] = {65, 66 , 67, 68, 69, 70};
String s1 = new String(b);
String s2 = new String(b, 3,2);
Here s1 will represent the string - "ABCDEF"
And s2 will represent the string - "DE"

    
5- We can also create a string from a previously existing string by using the following constructor -
String(String obj);
For example -
char ch[] = {'W','E','L','C','O','M','E'};
String s1 = new String(ch);
String s2 = new String(s1);
String s3  = new String("Hello");	// We can also pass String literals.
String s2 will represent the same string as s1 i.e. - "WELCOME". And s3 will represent the string "Hello".

        

Example -

public class Call {

    public static void main(String[] args) {

        byte b[] = {65,66,67,68,69,70};
        String s1 = new String(b);
        String s2 = new String(b, 3, 2);
        String s3 = new String(s1);
        String s4 = new String("Hello");
        System.out.println("s1 = "+s1);
        System.out.println("s2 = "+s2);
        System.out.println("s3 = "+s3);
        System.out.println("s4 = "+s4);
        
    }
}

        

Output -

s1 = ABCDEF
s2 = DE
s3 = ABCDEF
s4 = Hello

        

String Literals -

For each string literals you define in your program, Java automatically created a String object for them. So we can initialize a string object by any string literal. For example -
String s1 = "WELCOME";
is a valid string declaration.
But there is a minor difference between this declaration and other declaration of strings that we define by using constructors. Consider the following code -
 
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
    
It can be represented as follows-

If we define two strings with same value using string literal, then for second one there will be no separate value in string pool. In that case both will have the same reference.
However if we define using constructor then each string will have separate entries in string pool irrespective of their values.
So if we compare s1 and s2 using == operator i.e. s1 == s2 the answer will be true.
Similarly s1 == s3 will be false.
i.e. '==' compares their reference values not actual values contained in the string objects.

Length of String -

Java provides us a method named length() to calculate the length of any string.
Note - Do not confuse with length property of arrays.

For example -

String s1 = "WELCOME";
int length = s1.length();    // the output will be 7.