How can I express that two values ​​are not equal to each other?

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

An example of what I'm trying to accomplish is given 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) .

+6
source share
4 answers

Just put a '!' before boolean expression

+19
source

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

 if (a.equals(b)) // a equals b if (!a.equals(b)) // a not equal to b 
+21
source
 if (!secondaryPassword.equals(initialPassword)) 
+3
source

If the class implements comparable values, you can also do

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

does not use ! , although not particularly useful or readable ....

0
source

Source: https://habr.com/ru/post/902983/


All Articles