Java String compareWe 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) String compare by equals() methodThe String equals() method compares the original content of the string. It compares values of string for equality. String class provides two methods:
Output:true true false Output: false true 2) String compare by == operatorThe = = operator compares references not values. Output:true false 3) String compare by compareTo() methodThe 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:
Output:0 1 -1 Next TopicString Concatenation in java
« prev next » |