Working Of String In Java

Introduction

 
In this article we discuss ho strings work in Java and also discuss immutable strings, string comparison, and string concatenation.
 

String

 
A string is a sequence of characters (also called a character array). In many other languages, a string is treated as a character array, but in Java strings are objects. After creating a string object we can't change it. So it's called immutable.
 
Java provides the String class to create and manipulate strings.
 

How to create a string?

 
The general way to create a string is:
String str=" welcome to the c-sharp world";
 
Now, we discuss some topics of strings, they are:
  1. Immutable String
  2. String comparison
  3. String concatenation

1. Immutable String

 
In Java, after creating an object of a string we can't change it. So that's why a string is sometimes called an immutable string.
 

What's the need of immutable string

  • Java uses the concept of a string literal. Suppose we have five reference variables in our program and all refer to a single object. If any reference variable changes the value of an object then it will affect all the referenced variables. That is why string objects are immutable in Java.
  • In many Java classes, it is widely used as a parameter.
  • It is also used for safety purposes.
  • It can be safely shared among many threads, that is very important in multithreaded programming and avoids synchronization issues in Java.

2. String comparison

 
In Java any one of the following can be used to compare strings:
  • equals() method
  • by == operator
  • by compareTo() method
1. Compare string using equals() method
 
This method compares the contents of both strings. The String class provides the two methods:
  • public boolean equals (Object another){} 
    compares this string to the specified object.
     
  • public boolean equalsIgnoreCase(String another){}
    compares this String to another String, ignoring case. 
Example of equals (objects) method
  1. class EqualObjEx {  
  2.  public static void main(String args[]) {  
  3.   String str1 = "John";  
  4.   String str2 = "JOhn";  
  5.   String str3 = new String("John");  
  6.   String str4 = "Carter";  
  7.   System.out.println(str1.equals(str2)); //false   
  8.   System.out.println(str1.equals(str3)); //true  
  9.   System.out.println(str1.equals(str4)); //false  
  10.  }  
  11. }  
Output
 
pic-1.jpg
 
Example of EqualsIgnoreCase(String) method
 
Let's compare s1 and s2:
s1.equalsIgnoreCase(s2);// they always print true for that.
 
2. Compare string using = = operator
 
This "= =" operator only compares references, not values.
  1. class CompareOperEx  
  2. {  
  3.  public static void main(String args[])  
  4.  {  
  5.   String str1 = "John";  
  6.   String str2 = "John";  
  7.   String str3 = new String("John");  
  8.   System.out.println(str1 == str2); //true  
  9.   System.out.println(str1 == str3); //false  
  10.  }  
  11. }  
Output
 
fig-2.jpg
 
3. String compare using compareTo() method
 
This method compares values and returns an integer value, that indicates if the values compared are lesser, equal, or greater. If they are equal then it returns 0, if greater then it returns a positive value and if it lesser then it will return a negative value.
 
Example
  1. class CompareToEx  
  2.   {  
  3.     public static void main(String args[])  
  4.       {  
  5.         String str1="John";  
  6.         String str2="John";  
  7.         String str3="Carter";  
  8.         System.out.println(str1.compareTo(str2));//0  
  9.         System.out.println(str1.compareTo(str3));//7  
  10.         System.out.println(str3.compareTo(str1));//-7  
  11.     }  
  12.  }   
Output
 
fig-3.jpg
 

3. String concatenation

 
Java provides two ways to concatenate strings; they are:
  • By concat() method
  • By + (String concatenation) operator
1.By concat method
 
This method concatenates (or joins) the specified string to the end of the current string:
 
Syntax:
 
public String concat(String another){}
 
Example
  1. class ConcatMethodEx  
  2. {  
  3.  public static void main(String args[])  
  4.  {  
  5.   String str1 = "John";  
  6.   String str2 = "Carter";  
  7.   String str3 = str1.concat(str2);  
  8.   System.out.println(str3); //Sachin Tendulkar  
  9.  }  
  10. }  
Output
 
fig-4.jpg
 
2. By + (String concatenation) operator 
 
This operator is used to add strings. For example:
  1. class ConcatenOpEx  
  2. {  
  3.  public static void main(String args[])  
  4.  {  
  5.   String str = "John Carter";  
  6.   System.out.println(str); //John Carter  
  7.  }  
  8. }  
Output
 
fig-5.jpg
 
The compiler changes this to:
 
String str=(new StringBuilder()).append("John").append("Carter").toString();
 
It is done using the StringBuilder (or StringBuffer) class and its append method.
 
Example
  1. class ConctOpEx1  
  2. {  
  3.  public static void main(String args[])  
  4.  {  
  5.   String str = 70 + 30 + "John" + 20 + 13;  
  6.   System.out.println(str); //100John2013  
  7.  }  
  8. }  
Output
 
fig-6.jpg-