/** Demo of && vs & */ public class AndTypes { public static void main (String [] args) { boolean t = true; boolean f = false; // Printing out booleans--invokes toString as needed System.out.println("t="+t); System.out.println("f="+f); // The operators &&, &, || and | are all legal for boolean System.out.println("t && f = " + (t&&f)); System.out.println("t || f = " + (t||f)); System.out.println("t & f = " + (t&f)); System.out.println("t | f = " + (t|f)); // We can use bitwise & and | on integers int x = 0xABCD; // 1010 1011 1100 1101 hex int y = 0xDCBA; // 1101 1100 1011 1010 hex int z = x & y; // 1000 1000 1000 1000 bitwise and int w = x | y; // 1111 1111 1111 1111 bitwise or System.out.printf("x=%x\n",x); // yes, printf in Java! System.out.printf("y=%x\n",y); // the %x prints in hex System.out.printf("z=%x\n",z); System.out.printf("w=%x\n",w); // But we CANNOT use && and || (logical ops) on ints // Unlike in C/C++, Java does not treat ints as boolean. // int za = x && y; // <== syntax error // int wa = x || y; // <== syntax error } }