[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

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!
0
ew0kian
Asked:
ew0kian
1 Solution
 
objectsCommented:
if (s1.equals(s2))
{
  // equal
}
0
 
JavaInTheMorningCommented:
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
}
0
 
zzynxSoftware engineerCommented:
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");
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JugglerWCommented:
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.
0
 
WebstormCommented:
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).
0
 
lowenbrausatCommented:
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!!
0
 
JugglerWCommented:
> 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() ???

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

its not
0
 
JohnnyAffaCommented:
agrees with objects.......
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> if (s1.equalsIgnoreCase(s2))

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now