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 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

class EqualObjEx
  {
    public static void main(String args[])
      {
        String str1="John";
        String str2="JOhn";
        String str3=new String("John");
        String str4="Carter";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equals(str3));//true
        System.out.println(str1.equals(str4));//false
      }
  }

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.

class CompareOperEx
 
{
   
public static void main(String args[])
     
{
       
String str1="John";
       
String str2="John";
       
String str3=new String("John");
       
System.out.println(str1==str2);//true
       
System.out.println(str1==str3);//false
     
}
  }

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

class CompareToEx
 
{
   
public static void main(String args[])
     
{
       
String str1="John";
        String str2="John";
        String str3="Carter";
        System.out.println(str1.compareTo(str2));//0
        System.out.println(str1.compareTo(str3));//7
        System.out.println(str3.compareTo(str1));//-7
      }
 } 

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

class ConcatMethodEx
  {
    public static void main(String args[])
      {
        String str1="John";
        String str2="Carter";
        String str3=str1.concat(str2);
        System.out.println(str3);//Sachin Tendulkar
      }
  }

Output

fig-4.jpg

2. By + (String concatenation) operator

This operator is used to add strings. For example:

class ConcatenOpEx
 
{
   
public static void main(String args[])
     
{
       
String str="John Carter";
       
System.out.println(str);//John Carter
     
}
  }

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

class ConctOpEx1
 
{
   
public static void main(String args[])
     
{
       
String str=70+30+"John"+20+13;
       
System.out.println(str);//100John2013
     
}
  }

Output

fig-6.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all