Objects of String class in java.lang package are immutable, which means that once we create the object, we cannot perform any changes to it. And if we do, then the program create a new object. For example:
String str = new String("Michael"); str.concate(" Jackson"); System.our.println("String is: "+str);
Output of the above code snippet is:
String is: Michael
In String class, equals() method has been override to do content comparison rather than address comparison. So when we will pass 2 objects of String class a comparison is made among them, and if they are equal the method will return true otherwise false. For example:
String str1 = new String("Michael"); String str2 = new String("Michael"); String str3 = new String("James"); System.out.println("Comparing str1 and str2 using equals :"+str1.equals(str2)); System.out.println("Comparing str1 and str3 using equals :"+str1.equals(str3)); System.out.println("Comparing str1 and str2 using == :"+(str1==str2)); System.out.println("Comparing str1 and str3 using == :"+(str1==str3));
Output of the above code will be
Comparing str1 and str2 using equals :true Comparing str1 and str3 using equals :false Comparing str1 and str2 using == :false Comparing str1 and str3 using == :false
We can assign string to an object or variable in 2 ways:
1) Creating object of String class: In this method, we create an object of class String of java.lang package. This method creates 2 objects, of which one in heap and other in String Constant Pool. Reference is always pointing to the object in heap. Syntax:
String str = new String("Any string can be written here.");
2) Creating variable of String datatype: In this method, the compiler creates only one object is in heap. Variable points towards the object in heap. For example:
String s = "Any string can be stored in it.";
When we create any string object at run time, it will only get space in heap memory and not in String Constant Pool.
Interning of String
By using heap object reference if you want to get its corresponding String Constant Pool object then to do such operation we have special method named as intern(). Example:
//str1 is object stored in heap String str1 = new String("Something"); String str2 = str1.intern(); //here str2 is object stored in SCP
Constructors of String class:
String class has 5 constructors that includes 1 default constructor and 4 overloaded_constructors. Prototypes of these constructors are as follows:
String str = new String();
String str = new String(String constant_string);
String str = new String(StringBuffer sb);
String str = new String(char []ch);
String str = new String(byte []b);
Important Methods of String class:
Methods in String class which finds most commonly use and have important functionalities are as follows:
1) charAt(): We use it to find the character at a given index in the string. Prototype:
public char charAt(int index)
Example:
String str = "James"; System.out.println(str.charAt[3]); //output will be "e" System.out.println(str.charAt[30]); //output will be StringIndexOutOfBoundsException
2) concat(): It is used to concatenate a string to another string. Prototype:
public String concat(String str)
Example:
String str = "James"; str = str.concat("Barnes"); System.out.println(str); //output will be "James Barnes"
3) equals(): It is used to compare the content of 2 strings. This method performs case sensitive comparison. Prototype:
public boolean equals(Object obj)
Example:
String str = "James"; String s = "James"; System.out.println(str.equals(s)); //output will be "true"