Java String and String Comparison

In this article, you will be learning about strings in Java and string comparison.

Java String

Strings are used for storing text. A String variable contains a collection of characters surrounded by double-quotes.  It is basically an object that represents a sequence of char values.

Java String class provides the following operations to perform on strings. They are:

compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.

The java.lang.String class implements SerializableComparable and CharSequence interfaces.

The Java String is immutable so it cannot be changed.

How to create a string object?

We can create string object in two ways. They are

  1. By string literal
  2. By new keyword

1) String Literal

Java String literal is created by using double quotes. For Example

String s="welcome";  

Whenever you create a string literal, the JVM checks the “string constant pool” first. If the string already exists in the pool, a reference to the pooled instance is returned. If not, a new string instance is created and placed in the pool.

2) By new keyword

In the part of a new keyword, JVM will create a new string object in normal (non-pool) heap memory, and the new literal will be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool).

Java String comparison

You can compare string in java with respect to its content and reference.

There are three ways to compare string in java:

  1. By equals() method
  2. By = = operator
  3. By compareTo() method

String compare by equals() method

The String equals() method compares the original content of the string. It compares the values of string for equality.

Sample program for String compare by equals() method

	public class Main {  
	 public static void main(String args[]){  
	   String s1="Coders";  
	   String s2="Editor";  
	   String s3=new String("Coders");  
	   String s4="Deverloperpublish";  
	   System.out.println(s1.equals(s2));
	   System.out.println(s1.equals(s3));  
	   System.out.println(s1.equals(s4));  
	 }  
	}  


Output:

true
true
false

String compare by == operator

In this method, the = = operator compares references not values.

Sample program for String compare by == operator

	public class Main{  
	 public static void main(String args[]){  
	   String s1="Coders";  
	   String s2="Coders";  
	   String s3=new String("Coderes");  
	   System.out.println(s1==s2);
	 System.out.println(s1==s3);
	 }  
	}  

Output:

true
false

String compare by compareTo() method

The String compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string.

Suppose s1 and s2 are two string variables. If:

  • s1 == s2 :0
  • s1 > s2   :1
  • s1 < s2   :-1

Sample program for String compare by compareTo() method

	public class Main{  
	 public static void main(String args[]){  
	   String s1="Coders";  
	   String s2="Coders";  
	   String s3="Editor";  
	   System.out.println(s1.compareTo(s2));
	   System.out.println(s1.compareTo(s3));
	   System.out.println(s3.compareTo(s1)); }  
	}  

Output:

0
1
-1
close

Copy and paste this code to display the image on your site

%d bloggers like this: