How can I express that two values are not equal to eachother?

后端 未结 4 2031
傲寒
傲寒 2020-12-31 08:06

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

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

if (second         


        
相关标签:
4条回答
  • 2020-12-31 08:31

    "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
    
    0 讨论(0)
  • 2020-12-31 08:36
    if (!secondaryPassword.equals(initialPassword)) 
    
    0 讨论(0)
  • 2020-12-31 08:37

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

    0 讨论(0)
  • 2020-12-31 08:39

    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....

    0 讨论(0)
提交回复
热议问题