2

please help me i cant figure out how does the operators(AND, NOT, XOR ,,..ETC) work in java. I know the output of AND and OR but i am clueless at NOT. For example i don't completely understand statement such as a variable != integer(i!= 3). i mean how does the NOT operator work.for example how does NOT work here.

class Demo {

    public static void main(String args[]) throws java.io.IOException {
        char ch;

        do {

            System.out.print("Press a key followed by ENTER: ");

            ch = (char) System.in.read(); // get a char

        } while (ch != 'q');

    }

}
greg-449
  • 109,219
  • 232
  • 102
  • 145
Chris John
  • 25
  • 4

3 Answers3

0

if you make some System outs you will figure out:

char ch = 'l';
System.out.print(2 != 3);
--true, they are not equal
System.out.print('q' != 'q');
-- false, they are equals
System.out.print(ch != 'q');
-- true, they are not equals

it means, they != checks if they are exactly the same (be careful in this case is used for primitive types, such as int, char, bool, etc. this operator work differently in objects, such as String)

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

The ! operator is unary. When applied to Boolean values it switches, for example

bool on = true;
System.out.print(!on); //false
System.out.print(on); //true

When used next to an equal sign, it checks if the values are not equal. If they are not equal, it returns true. Otherwise, it returns false. For example,

int two = 2;
int three = 3;
System.out.print(two != three); 
//returns true, since they are not equal
System.out.print(two == three);
//returns false, since they are not equal
notphilphil
  • 385
  • 5
  • 16
  • "the `!` operator is binary." Actually, it's a unary operator http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15.6 – Lew Bloch Jun 17 '17 at 18:20
  • "When [`!` is] used next to an equal sign" Some might misconstrue this to mean that `!=` comprises two tokens, or two operators concatenated, which it does not. `!=` is a single operator and parsed as a single Java token. – Lew Bloch Jun 17 '17 at 18:25
  • "Just like `&&` and `||`, the `!` operator is unary." The `&&` and `||` operators are not unary. Neither is `!=`. Nor `==`. – Lew Bloch Jun 17 '17 at 21:00
0
int x = 4;
int y = 5;

!case a means not case a

x == y means x and y are referencing the same place in memory (to understand what that means, see this question).

x != y is the opposite of the above, and is the same as writing !(x == y) (not x equals y)

case a && case b And operator - Both case a and case b are true.

case a || case b Or operator - Either case a or case b are true.

Here are same examples so everything is clearer:

1==1 // true, as 1 is equal to 1
2==1 // false, as 1 is not equal to 2.
!true // false, as not true is false.
1!=1 // false, as 1 is equal to one.
!(1==1) // The same as above - exact same output.
// You need the brackets, because otherwise it will throw an error:
!1==1 // Error - what's '!1'? Java is parsed left-to-right.
true && false // false - as not all cases are true (the second one is false)
true || false // rue - as at least one of the cases are true (the first one)
NonameSL
  • 1,405
  • 14
  • 27