这篇简单些
关键字: java 面试题 自增 自减 位运算符
int i = 0;int j = i++;int k = --i;
int i = 0;int j = i++ + ++i;int k = --i + i--;
int i=0;System.out.println(i++);
float f=0.1F;f++;double d=0.1D;d++;char c='a';c++;
public class Test { public static void main(String[] args) { // 整型 byte b = 0; b++; // 整型 long l = 0; l++; // 浮点型 double d = 0.0; d++; // 字符串 char c = 'a'; c++; // 基本类型包装器类 Integer i = new Integer(0); i++; }}
public class BitOperatorTest { public static void main(String[] args) { // 整型 byte b1 = 10, b2 = 20; System.out.println("(byte)10 & (byte)20 = " + (b1 & b2)); // 字符串型 char c1 = 'a', c2 = 'A'; System.out.println("(char)a | (char)A = " + (c1 | c2)); // 基本类型的包装器类 Long l1 = new Long(555), l2 = new Long(666); System.out.println("(Long)555 ^ (Long)666 = " + (l1 ^ l2)); // 浮点型 float f1 = 0.8F, f2 = 0.5F; // 编译报错,按位运算符不能用于浮点数类型 // System.out.println("(float)0.8 & (float)0.5 = " + (f1 & f2)); }}
public class OperatorTest { public boolean leftCondition() { System.out.println("执行-返回值:false;方法:leftCondition()"); return false; } public boolean rightCondition() { System.out.println("执行-返回值:true;方法:rightCondition()"); return true; } public int leftNumber() { System.out.println("执行-返回值:0;方法:leftNumber()"); return 0; } public int rightNumber() { System.out.println("执行-返回值:1;方法:rightNumber()"); return 1; } public static void main(String[] args) { OperatorTest ot = new OperatorTest(); if (ot.leftCondition() && ot.rightCondition()) { // do something } System.out.println(); int i = ot.leftNumber() & ot.rightNumber(); }}
public abstract class Test { public static void main(String[] args) { System.out.println("1 <<3 = " + (1 <<3)); System.out.println("(byte) 1 <<35 = " + ((byte) 1 <<(32 + 3))); System.out.println("(short) 1 <<35 = " + ((short) 1 <<(32 + 3))); System.out.println("(char) 1 <<35 = " + ((char) 1 <<(32 + 3))); System.out.println("1 <<35 = " + (1 <<(32 + 3))); System.out.println("1L <<67 = " + (1L <<(64 + 3))); // 此处需要Java5.0及以上版本支持 System.out.println("new Integer(1) <<3 = " + (new Integer(1) <<3)); System.out.println("10000 >> 3 = " + (10000 >> 3)); System.out.println("10000 >> 35 = " + (10000 >> (32 + 3))); System.out.println("10000L >>> 67 = " + (10000L >>> (64 + 3))); }}