How can I express that two values are not equal to

2019-02-12 10:28发布

Is there a method similar to equals() that expresses "not equal to"?

An example of what I am trying to accomplish is below:

if (secondaryPassword.equals(initialPassword)) 
{
    JOptionPane.showMessageDialog(null, "You've successfully completed the program.");
} else {
    secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); 
}

I am trying to find something that will not require me to use if ( a != c).

4条回答
Juvenile、少年°
2楼-- · 2019-02-12 10:50

If the class implements comparable, you could also do

int compRes = a.compareTo(b);
if(compRes < 0 || compRes > 0)
    System.out.println("not equal");
else
    System.out.println("equal);

doesn't use a !, though not particularly useful, or readable....

查看更多
姐就是有狂的资本
3楼-- · 2019-02-12 10:57
if (!secondaryPassword.equals(initialPassword)) 
查看更多
Summer. ? 凉城
4楼-- · 2019-02-12 11:03

"Not equals" can be expressed with the "not" operator ! and the standard .equals.

if (a.equals(b)) // a equals b
if (!a.equals(b)) // a not equal to b
查看更多
Bombasti
5楼-- · 2019-02-12 11:14

Just put a '!' in front of the boolean expression

查看更多
登录 后发表回答