Answer: ! operator and int value : Questions « Operators « SCJP






B.

The use of ! is illegal because x is of int type, not boolean.

public class MainClass {
  public static void main(String[] argv) {

    int x = 1;
    x = !x;

  }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The operator ! is undefined for the argument type(s) int

	at MainClass.main(MainClass.java:5)








2.11.Questions
2.11.1.What are the values of the variables x, a, and b?
2.11.2.Answer: pre-increament operator
2.11.3.Is it legal for int x = 1; x = !x;
2.11.4.Answer: ! operator and int value
2.11.5.Is it legal for int x = 1; if (!(x > 3)) {System.out.println(x);}
2.11.6.Answer: ! operator and boolean expression
2.11.7.Is it legal: int x = 1; x = ~x;
2.11.8.Answer: ~ binary operator and int value
2.11.9.What is printed out value (int x = -1; x = x >>> 5;)
2.11.10.Answer: shift operator and int value
2.11.11.What is the printed value (int x = -1; x = x >>> 32;)
2.11.12.Answer: shift and int value
2.11.13.What is the printed out value (byte x = -1; x = x >>> 5;)
2.11.14.Answer: shift and negative value
2.11.15.What is the output value (int x = -1; x = x >> 5;)?
2.11.16.Answer: negative value and shifting
2.11.17.Is it legal: String x = "Hello"; int y = 9; x += y;
2.11.18.Answer operation between int and string