String Class in Java

String is a sequence of characters. The String class implements immutable character strings, which are read only once the string has been created and initialized. In the Java programming language, strings are objects. String Class is defined in java.lang package so it is implicitly available for all programs.
Following features:-
  • It is Final class.
  • Due to Final, String class cannot be inherited.
  • By default It extends Object class.
  • It implements Serializable, Comparable and CharSequence interfaces.
  • It is immutable.
  • It is also a reference datatype in java

The Java platform provides the String class to create and manipulate strings.
Creating and Initializing Strings
There are two main ways to create Java Strings.

  • One is by creating a String literal.
  • Second is to explicitly create a new Object in memory.

String str=”Hello welcome to nsstudent.com”; // String Literal
String name=new String(“Sunil Kumar Singh”); // Create a new object in memory

Note: - A String literal is a reference to a String object.
Comparing Strings
Java String class has following methods to compare String objects, including:
public int compareTo( String str )
  • Compares the current String object to str, and returns 0 only if the two strings contain the same sequence of characters (case sensative).
  • A  value less than 0  is returned if the current String is lower in the Unicode set than the String str argument.
  • A value greater than 0 is returned if the current String is higher in the Unicode set than the String str agrument.
public boolean equals( Object obj )
Check the equality as two String object having the same sequence of character.
  • returns true if obj is another String containing the same sequence of characters;  otherwise return false.
public boolean equalsIgnoreCase( String str )
Compare (but ignores the case of characters) of the current String object with str and returns true if str is another String containing the same sequence (ignoring case) of characters; otherwise return false.
What’s the difference between equals() and == ?
The “==” operator
In Java, when the “==” operator is used to compare two String objects references, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the two String object name are basically references to the same memory location.
The == operator does not test whether the contents of the two Strings are equal.
Example == operator
		           
class StringCompare { public static void main(String s[]) { String str1="Java"; String str2="Java"; String str3=str2; String str4=new String("Java"); String str5=new String("Java"); String str6=str4; System.out.println("str1==str2 : "+(str1==str2)); System.out.println("str4==str5 : "+(str4==str5)); System.out.println("str4==str6 : "+(str4==str6)); System.out.println("str4.equals(str5) : "+(str4.equals(str5))); } }
Output
  C:\JavaProgram>java StringCompare  
  str1==str2    :true
  str4==str5    : false
  str4==str6    : true   
  str4.equals (str5)    : true
The equals () method
The equals () method is actually meant to compare the contents of two String objects, and not their location in memory.
class StringCompare { public static void main(String s[]) { String str4=new String("Java"); String str5=new String("Java"); String str6=str4; System.out.println("str4==str5 : "+(str4==str5)); System.out.println("str4==str6 : "+(str4==str6)); System.out.println("str4.equals(str5) : "+(str4.equals(str5))); } }
Output
  C:\JavaProgram>java StringCompare  
  str4==str5    : false
  str4==str6    : true   
  str4.equals (str5)    : true
Caution
  • Use of the == operator only tests whether two String object references refer to the same object (memory space). The == operator does not test whether the contents of the two Strings are equal.
  •  Never use == to test equality of String contents.
  •  If there is no use new to assign a value to a String object, then all other Strings created similarly with the same literal value will refer to the same object in memory. And in this case == to test equality of String contents (case sensitive).
 



 

Latest Tutorial


Most Visited Tutorial