Link to home
Start Free TrialLog in
Avatar of ew0kian
ew0kian

asked on

test two strings for equality

what is the best way to see if two strings are exactly the same in java? its for when people enter a password into a form twice, i want to check to see that they entered the same password string both times.

thanks!
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JavaInTheMorning
JavaInTheMorning

Since it's a password, the best thing is to use objects' suggestion.
But there is another possibility for other cases:
if (s1.equalsIgnoreCase(s2))
{
   // equal - case insensitive
}
Use equals indeed and certainly not ==
Illustration:

        StringBuffer buf1 = new StringBuffer("Hello");
        StringBuffer buf2 = new StringBuffer("Hello");

        if (buf1.toString()==buf2.toString())
            System.out.println("1) equal");
        else
            System.out.println("1) not equal");  // <<<<<<< This is printed (wrong)

        if ( buf1.toString().equals(buf2.toString()) )

            System.out.println("2) equal");      // <<<<<<<< This is printed (correct)
        else
            System.out.println("2) not equal");
Just to be complete: :-)

If you do this:

String pw1 = "Hallo".intern();
String pw2 = "Hallo".intern();

if ( pw1 == pw2 )
  System.out.println( "Equal" );
else
  System.out.println( "Not Equal" );

If you use intern() on both strings of a compare you can use == instead of equals().
But because intern() costs more than an equal it pays only if you do the compare very often.
Hi ew0kian,

If you're using a javax.swing JPasswordField to enter the passwords, you'll get a char[], not a string, which for security reason you can clear after use. If you want to compare 2 char[], use java.util.Arrays.equals(char_array1,char_array2).
In case you are NOT using JPasswordField you can use the hashCode() method from String class.

String.hashCode() returns a hash code for the string. The hash code for a String object is computed as  
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

Examples:

    int hash1 = "Hello".hashCode();
    int hash2 = "Hello".hashCode();
    System.out.println("Hash1 equals to Hash2? "+(hash1 == hash2)); //The result is TRUE

    int hash1 = "Hello".hashCode();
    int hash2 = "HellO".hashCode(); //Note the upper case 'O'
    System.out.println("Hash1 equals to Hash2? "+(hash1 == hash2)); //The result is FALSE


String.hashCode() is case-sensitive. This is the best way - and surest -  to do it.

Hope this can help you!!
c u arround! bye!!
> String.hashCode() is case-sensitive. This is the best way - and surest -  to do it.

Why should this be better or surer than using equals() ???

> Why should this be better or surer than using equals() ???

its not
agrees with objects.......
>> if (s1.equalsIgnoreCase(s2))

Since its a password, I'd prefer to do a case-sensitive comparision, rather than case-insensitive.