Author Topic: How do you compare strings in Java?  (Read 31 times)

0 Members and 1 Guest are viewing this topic.

Offline arpee

  • EquiBerks
  • Jr. Member
  • *
  • Posts: 81
  • Karma: +1/-0
    • View Profile
How do you compare strings in Java?
« on: September 16, 2014, 01:26:37 AM »
How do you compare strings in Java?

Techronnati | where technology never sleeps

How do you compare strings in Java?
« on: September 16, 2014, 01:26:37 AM »

Mountain View

Offline LadyProgrammer

  • Full Member
  • *
  • Posts: 118
  • Karma: +0/-0
    • View Profile
Re: How do you compare strings in Java?
« Reply #1 on: September 16, 2014, 01:28:20 AM »
You can compare String objects in a variety of ways, and the results are often different. The correctness of your result depends largely on what type of comparison you need. Common comparison techniques include the following:

Compare with the == operator.
Compare with a String object’s equals method.
Compare with a String object’s compareTo method.
Compare with a Collator object.
Comparing with the == Operator

The == operator works on String object references. If two String variables point to the same object in memory, the comparison returns a true result. Otherwise, the comparison returns false, regardless whether the text has the same character values. The == operator does not compare actual char data. Without this clarification, you might be surprised that the following code snippet prints The strings are unequal.

For Example:

Code: [Select]
String name1 = “Michèle”;
String name2 = new String(”Michèle”);
if (name1 == name2) {
System.out.println(”The strings are equal.”);
} else {
System.out.println(”The strings are unequal.”);
}

The Java platform creates an internal pool for string literals and constants. String literals and constants that have the exact same char values and length will exist exactly once in the pool. Comparisons of String literals and constants with the same char values will always be equal.

Comparing with the equals Method

The equals method compares the actual char content of two strings. This method returns true when two String objects hold char data with the same values. This code sample prints The strings are equal.

For Example:

Code: [Select]
String name1 = “Michèle”;
String name2 = new String(”Michèle”);
if (name1.equals(name2) {
System.out.println(”The strings are equal.”);
} else {
System.out.println(”The strings are unequal.”);
}

Comparing with the compareTo Method

The compareTo method compares char values similarly to the equals method. Additionally, the method returns a negative integer if its own String object precedes the argument string. It returns zero if the strings are equal. It returns a positive integer if the object follows the argument string. The compareTo, method says that cat precedes hat. The most important information to understand about this comparison is that the method compares the char values literally. It determines that the value of ‘c’ in cat has a numeric value less than the ‘h’ in hat.

For Example:

Code: [Select]
String w1 = “cat”;
String w2 = “hat”;
int comparison = w1.compareTo(w2);

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
117 Views
Last post October 13, 2008, 02:58:40 PM
by h2obubbler
0 Replies
159 Views
Last post October 13, 2008, 03:07:51 PM
by h2obubbler
2 Replies
134 Views
Last post October 25, 2008, 08:45:00 AM
by Corps
0 Replies
78 Views
Last post November 27, 2008, 03:32:34 PM
by Corps
1 Replies
30 Views
Last post January 23, 2010, 01:28:35 PM
by JusticeLeague

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.