Difference between != and !(boolean) in java

Whats difference between != and !(boolean) in java?

int a = 1;
int b = 2;
boolean c = a != b; //true

vs

int a = 1;
int b = 2;
boolean c = !(a == b); //true

1 answer

  • answered 2020-11-24 00:57 Thudani Hettimulla

    int a = 1;
    int b = 2;
    boolean c = a != b; //true
    

    In this one you are assigning whether a is not equal to b to the variable c. which is true as a is not equal to b

    int a = 1;
    int b = 2;
    boolean c = !(a == b); //true
    

    Here you are assigning the inverse of whether a equal to b. Where a is not equal to b and inverse of it is true.