Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

2.string Comparison in Java - Javatpoint

Uploaded by

techstack901
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

2.string Comparison in Java - Javatpoint

Uploaded by

techstack901
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

3/28/24, 11:57 AM String Comparison in Java - javatpoint

ADVERTISEMENT

Java String compare

We can compare String in Java on the basis of content and reference.

It is used in authentication (by equals() method), sorting (by compareTo()


method), reference matching (by == operator) etc.

There are three ways to compare String in Java:

1. By Using equals() Method

2. By Using == Operator

3. By compareTo() Method

https://www.javatpoint.com/string-comparison-in-java 2/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

1) By Using equals() Method


The String class equals() method compares the original content of the string. It
compares values of string for equality. String class provides the following two
methods:

ADVERTISEMENT

ADVERTISEMENT ADVERTISEMENT

https://www.javatpoint.com/string-comparison-in-java 3/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

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.

Teststringcomparison1.java

class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
}
}

Test it Now

https://www.javatpoint.com/string-comparison-in-java 4/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

Output:

true
true
false

In the above code, two strings are compared using equals() method of String
class. And the result is printed as boolean values, true or false.

Teststringcomparison2.java

class Teststringcomparison2{
public static void main(String args[]){
String s1="Sachin";
String s2="SACHIN";

System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}

Test it Now

Output:

https://www.javatpoint.com/string-comparison-in-java 5/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

false
true

In the above program, the methods of String class are used. The equals()
method returns true if String objects are matching and both strings are of same
case. equalsIgnoreCase() returns true regardless of cases of strings.

Click here for more about equals() method

2) By Using == operator
The == operator compares references not values.

Teststringcomparison3.java

class Teststringcomparison3{

https://www.javatpoint.com/string-comparison-in-java 6/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

public static void main(String args[]){


String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in no
}
}

Test it Now

Output:

true
false

3) String compare by compareTo() method


The above code, demonstrates the use of == operator used for comparing two
String objects.

https://www.javatpoint.com/string-comparison-in-java 7/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

3) By Using compareTo() method


The String class 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 objects. If:

s1 == s2 : The method returns 0.

s1 > s2 : The method returns a positive value.

s1 < s2 : The method returns a negative value.

Teststringcomparison4.java

class Teststringcomparison4{

https://www.javatpoint.com/string-comparison-in-java 8/15
3/28/24, 11:57 AM String Comparison in Java - javatpoint

public static void main(String args[]){


String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}

Test it Now

Output:

0
1
-1

Click me for more about compareTo() method

« prev next »

https://www.javatpoint.com/string-comparison-in-java 9/15

You might also like