Comparing Strings
Java provides a variety of methods to compare String objects, including: public int compareTo( String str ). Compares the current String object to str, and returns 0 only if the two strings contain the same sequence of characters (case sensative). A negative value is returned if the current.
String is lower in the Unicode set than the String str. A positive value is returned if the current String is higher in the Unicode set than the String str.
public boolean equals( Object obj )
Compares the current String object with obj and returns true if obj is another String containing the same sequence of characters; false is returned otherwise.
public boolean equalsIgnoreCase( String str )
Performs a case-insensitive comparison of the current String object with str and returns true if str is another String containing the same sequence (ignoring case) of characters; false is returned otherwise.
Use of the == operator only tests whether two String object references refer to the same object (memory space). The == operator does not test whether the contents of the two Strings are equal.
Example == operator
String s1 = new String( "Hello" );
String s2 = new String( "Hello" );
String s3 = s1;
System.out.println( " String s1 == s2 : " + (s1==s2) );
System.out.println( " String s1 equals s2 : " + s1.equals(s2) );
System.out.println( " String s1 == s3 : " + (s1==s3) );
Output
String s1: Hello
String s2: Hello
String s3: Hello
String s1 == s2 : false
String s1 equals s2 : true
String s1 == s3 : true
The object contents of s1 and s2 are equal thus s1.equals(s2) is true.
s1 and s3 reference the same memory space, thus s1 == s3 evaluates to true.
s1 and s2 do not reference the same memory space, thus s1 == s2 evaluates to false.
Never use == to test equality of String contents. If new is not used to assign a value to a String object, then all other Strings constructed similarly with the same literal value will refer to the same object in memory.
String s4 = "Hello";
String s5 = "Hello";
System.out.println( " String s4 == s5 : " + (s4==s5) );
System.out.println( " String s4 equals s5 : " + s4.equals(s5) );
Output
String s4 == s5 : true
String s4 equals s5 : true
Java provides a variety of methods to compare String objects, including: public int compareTo( String str ). Compares the current String object to str, and returns 0 only if the two strings contain the same sequence of characters (case sensative). A negative value is returned if the current.
String is lower in the Unicode set than the String str. A positive value is returned if the current String is higher in the Unicode set than the String str.
public boolean equals( Object obj )
Compares the current String object with obj and returns true if obj is another String containing the same sequence of characters; false is returned otherwise.
public boolean equalsIgnoreCase( String str )
Performs a case-insensitive comparison of the current String object with str and returns true if str is another String containing the same sequence (ignoring case) of characters; false is returned otherwise.
Use of the == operator only tests whether two String object references refer to the same object (memory space). The == operator does not test whether the contents of the two Strings are equal.
Example == operator
String s1 = new String( "Hello" );
String s2 = new String( "Hello" );
String s3 = s1;
System.out.println( " String s1 == s2 : " + (s1==s2) );
System.out.println( " String s1 equals s2 : " + s1.equals(s2) );
System.out.println( " String s1 == s3 : " + (s1==s3) );
Output
String s1: Hello
String s2: Hello
String s3: Hello
String s1 == s2 : false
String s1 equals s2 : true
String s1 == s3 : true
The object contents of s1 and s2 are equal thus s1.equals(s2) is true.
s1 and s3 reference the same memory space, thus s1 == s3 evaluates to true.
s1 and s2 do not reference the same memory space, thus s1 == s2 evaluates to false.
Never use == to test equality of String contents. If new is not used to assign a value to a String object, then all other Strings constructed similarly with the same literal value will refer to the same object in memory.
String s4 = "Hello";
String s5 = "Hello";
System.out.println( " String s4 == s5 : " + (s4==s5) );
System.out.println( " String s4 equals s5 : " + s4.equals(s5) );
Output
String s4 == s5 : true
String s4 equals s5 : true
0 comment(s):
Post a Comment