添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Method 1: using == operator

Double equals operator is used to compare two or more than two objects, If they are referring to the same object then return true, otherwise return false. String is immutable in java. When two or more objects are created without new keyword, then both object refer same value. Double equals operator actually compares objects references.


The below example illustrates the use of == for string comparison in Java:

// Compare s1 and s2
// It should return true as they both
// refer to same object in memory
System.out.println(s1 + " == " + s2 + ": "
+ (s1 == s2));
// Compare s1 and s3
// It should return true as they both
// refer to same object in memory
System.out.println(s1 + " == " + s3 + ": "
+ (s1 == s3));
// Compare s2 and s3
// It should return true as they both
// refer to same object in memory
System.out.println(s2 + " == " + s3 + ": "
+ (s2 == s3));
// Compare s1 and s4
// It should return false as they both
// refer to different object in memory
System.out.println(s1 + " == " + s4 + ": "
+ (s1 == s4));

Method 2: Using equals() method

In Java, string equals() method compares the two given strings based on the data / content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.

Below example illustrate the use of .equals for string comparison in Java:

// It should return true as they both
// have the same content
System.out.println(s1 + " .equals " + s2
+ ": " + s1.equals(s2));
// Compare s1 and s3
// It should return false as they both
// have the different content
System.out.println(s1 + " .equals " + s3
+ ": " + s1.equals(s3));
// Compare s2 and s3
// It should return false as they both
// have the different content
System.out.println(s2 + " .equals " + s3
+ ": " + s2.equals(s3));
// Compare s1 and s4
// It should return true as they both
// have the same content
System.out.println(s1 + " .equals " + s4
+ ": " + s1.equals(s4));

Method 3: Using compareTo() method

In java, Comparable interface compares values and returns an int, these int values may be less than, equal, or greater than 0. The java compares two strings based on the Unicode value of each character in the strings. If two strings are different, then they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.

Assuming index ‘i’ is where characters are different then compareTo() will return firstString.charAt(i)-secondString.charAt(i) .

Below example illustrate the use of .compareTo for string comparison in Java:

// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 + " .compareTo " + s2
+ ": " + s1.compareTo(s2));
// Compare s1 and s3
// It should return -32 as they both
// have the different ASCII value
System.out.println(s1 + " .compareTo " + s3
+ ": " + s1.compareTo(s3));
// Compare s3 and s2
// It should return 32 as they both
// have the different ASCII value
System.out.println(s3 + " .compareTo " + s2
+ ": " + s3.compareTo(s2));
// Compare s1 and s4
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 + " .compareTo " + s4
+ ": " + s1.compareTo(s4));

Method 4: Using equalsIgnoreCase() method

Java String equalsIgnoreCase() method is much similar to equals() method, except that case is ignored like in above example String object s4 compare to s3 then equals() method return false, but here in case of equalsIgnoreCase() it will return true. Hence equalsIgnoreCase() method is Case Insensitive.

Below example illustrate the use of .equalsIgnoreCase for string comparison in Java:

// It should return true as they both
// have the same content
System.out.println(s1 + " .equalsIgnoreCase " + s2
+ ": "
+ s1.equalsIgnoreCase(s2));
// Compare s1 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s1 + " .equalsIgnoreCase " + s3
+ ": "
+ s1.equalsIgnoreCase(s3));
// Compare s2 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s2 + " .equalsIgnoreCase " + s3
+ ": "
+ s2.equalsIgnoreCase(s3));
// Compare s1 and s4
// It should return true as they both
// have the same content
System.out.println(s1 + " .equalsIgnoreCase " + s4
+ ": "
+ s1.equalsIgnoreCase(s4));

Method 5: Using compare() method

In Java for locale specific comparison, one should use Collator class which is in java.text package. The one most important feature of Collator class is the ability to define our own custom comparison rules.

Below example illustrate the use of compare() method in Java to compare Strings:

// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 + " collator.compare " + s2
+ ": " + collator.compare(s1, s2));
// Compare s1 and s3
// It should return 1
System.out.println(s1 + " collator.compare " + s3
+ ": " + collator.compare(s1, s3));
// Compare s3 and s2
// It should return -1
System.out.println(s3 + " collator.compare " + s2
+ ": " + collator.compare(s3, s2));
// Compare s1 and s4
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 + " collator.compare " + s4
+ ": " + collator.compare(s1, s4));
Java String equalsIgnoreCase() Method with Examples
In Java, equalsIgnoreCase() method of the String class compares two strings irrespective of the case (lower or upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false. Syntax of equalsIgnoreCase()str2.equalsIgnoreCase(str1);Parametersstr1: A string tha
Difference Between ordinal() and compareTo() in Java Enum
Enumerations serve the purpose of representing a group of named constants in a programming language. If we want to represent a group named constant then we should go for Enum. Every enum constant is static. Hence, we can access it by using enum Name. Example: enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }ordinal() Metho
Java Integer compareTo() method
The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer
Java long compareTo() with examples
The java.lang.Long.compareTo() is a built-in method in java that compares two Long objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object. Sy
Double.compareTo() Method in Java with Examples
The java.lang.Double.compareTo() is a built-in method in java that compares two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object
BigDecimal compareTo() Function in Java
The java.math.BigDecimal.compareTo(BigDecimal bg) method checks for equality of this BigDecimal and BigDecimal object bg passed as parameter. The method considers two equal BigDecimal objects even if they are equal in value irrespective of the scale. Syntax: public int compareTo(BigDecimal bg) Parameters:This function accepts only one BigDecimal ob
ByteBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another. Two byte buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of byte elements are compared as if by invoking Byte.compare
BigInteger compareTo() Method in Java
The java.math.BigInteger.compareTo(BigInteger value) method Compares this BigInteger with the BigInteger passed as the parameter. Syntax: public int compareTo(BigInteger val)Parameter: This method accepts a single mandatory parameter val which is the BigInteger to compare with BigInteger object. Return Type: This method returns the following: 0: if
FloatBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of float elements are compared as if by invoking Float.com
CharBuffer compareTo() method in Java
compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elements are compared as if by invoking Char.compare(c
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
Please go through our recently updated Improvement Guidelines before submitting any improvements.
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
Please go through our recently updated Improvement Guidelines before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.