Java String compare to determine Equality(Java String类型比较的三种方法)
2012-11-24 19:56
459 查看
java string compare can
be done in many ways as shown below. Depending on the type of java stringcompare you need, each of them is
used.
String object references. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false. Note that the ‘==’ operator does not compare the
content of the text present in
the String objects. It only compares the references the 2 Strings are pointing to. The following Program would print “The
strings are unequal” In the first case and “The strings are equal” in the second case.
Comparing
The
equals method is used when we need to compare the content of the text present in
the String objects.This
method returns true when two String objects hold the
same content (i.e. the same values). The following Program would print “The strings are unequal” In the first
case and “The strings are equal” in the second case.
Comparing
The
compareTo method is used when we need to determine the order of
Strings lexicographically. It compares char values similar to the equals method. The compareTo method returns
a negative integer
if the first String object precedes the second string. It returns zero if the 2 strings being compared are equal. It returns a positive integer if the first String object follows the second string. The following Program would print “name2
follows name1” In the first case and “name1 follows name3” in the second case.
Reference: http://www.javabeginner.com/learn-java/java-string-comparison
END
be done in many ways as shown below. Depending on the type of java stringcompare you need, each of them is
used.
Comparing using the == Operator
The == operator is used when we have to compare theString object references. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false. Note that the ‘==’ operator does not compare the
content of the text present in
the String objects. It only compares the references the 2 Strings are pointing to. The following Program would print “The
strings are unequal” In the first case and “The strings are equal” in the second case.
public class StringComparision1 { public static void main(String[] args) { String name1 = "Bob"; String name2 = new String("Bob"); String name3 = "Bob"; // 1st case if (name1 == name2) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } // 2nd case if (name1 == name3) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } }
Comparing
using the equals Method
Theequals method is used when we need to compare the content of the text present in
the String objects.This
method returns true when two String objects hold the
same content (i.e. the same values). The following Program would print “The strings are unequal” In the first
case and “The strings are equal” in the second case.
public class StringComparision2 { public static void main(String[] args) { String name1 = "Bob"; String name2 = new String("Bob1"); String name3 = "Bob"; // 1st case if (name1.equals(name2)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } // 2nd case if (name1.equals(name3)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } }
Comparing
using the compareTo Method
ThecompareTo method is used when we need to determine the order of
Strings lexicographically. It compares char values similar to the equals method. The compareTo method returns
a negative integer
if the first String object precedes the second string. It returns zero if the 2 strings being compared are equal. It returns a positive integer if the first String object follows the second string. The following Program would print “name2
follows name1” In the first case and “name1 follows name3” in the second case.
public class StringComparision3 { public static void main(String[] args) { String name1 = "bob"; String name2 = new String("cob"); String name3 = "Bob"; // 1st case if (name1.compareTo(name2) == 0) { System.out.println("The strings are equal."); } else if (name1.compareTo(name2) < 0) { System.out.println("name2 follows name1"); } else { System.out.println("name1 follows name2"); } // 2nd case. Comparing Ascii Uppercase will be smaller then Lower Case if (name1.compareTo(name3) == 0) { System.out.println("The strings are equal."); } else if (name1.compareTo(name3) < 0) { System.out.println("name3 follows name1"); } else { System.out.println("name1 follows name3"); } } }
Reference: http://www.javabeginner.com/learn-java/java-string-comparison
END
相关文章推荐
- java中判断string是否为空的三种方法——效率比较
- java对象转换String类型的三种方法
- Java String.compareTo()方法
- Java String.compareTo()方法
- string类型转int类型的三种方法速率比较
- String.compareTo()方法用于版本号比较的坑
- Java String.compareTo()方法
- java中int类型转换成String类型的三种方法
- Java String.compareTo()方法
- java对象转换String类型的三种方法
- JAVA 中int类型转String类型的通常方法,有三种:
- java对象转换String类型的三种方法
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
- java中compareTo和compare方法之比较
- java StringBuffer的length()和capacity()方法比较
- java 数组 、 for each循环, 数组的两种拷贝方法 ,Arrays.copyOf(),Arrays.toString[],匿名数组
- 比较Java Swing中三种注册事件的方法
- [置顶] Android开发之 当前日期String类型转date类型 java代码中实现方法
- 类型转换错误java.math.BigDecimal cannot be cast to java.lang.String
- 类型转换错误java.math.BigDecimal cannot be cast to java.lang.String