------- android培训、java培训、期待与您交流! ----------
二进制直接量
在Java
// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.
int anInt4 = 0b00000000000000000000000000000101;
anInt2==anInt3 //true
anInt2==anInt4 //true
// A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
二进制直接量让数据之间的关系更明显比起十六进制或八进制。例如,下面数组中后一个数是前一个数的左移一位:
public static final int[] phases = {
0b00110001,
0b01100010,
0b11000100,
0b10001001,
0b00010011,
0b00100110,
0b01001100,
0b10011000}
在十六进制表示中数值之间的关系不能很明显的看出来:
public static final int[] phases = { 0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98};
可以使用二进制直接量让一个位图更具可读性:
public static final short[] HAPPY_FACE = {
(short)0b0000011111100000,
(short)0b0000100000010000,
(short)0b0001000000001000,
(short)0b0010000000000100,
(short)0b0100000000000010,
(short)0b1000011001100001,
(short)0b1000011001100001,
(short)0b1000000000000001,
(short)0b1000000000000001,
(short)0b1001000000001001,
(short)0b1000100000010001,
(short)0b0100011111100010,
(short)0b0010000000000100,
(short)0b0001000000001000,
(short)0b0000100000010000,
(short)0b0000011111100000,}
数字直接量使用下划线
从Java
比如如果你的代码包含的数字有许多数字位,你可以使用下划线字符把数字分成三个一组,类似于你使用标点符号像逗号,空格作为分隔符一样。
下面的例子演示了其他的方式你可以在数字直接量使用下划线:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
下划线只能被使用在数字之间;下划线不能被使用在下列位置:
- 一个数字开始或结束的位置
- 临近浮点直接量的小数点
- F或L后缀的前面
- 当数字用字符串表示的时候
下面的例子演示了正确的和不正确的下划线放置位置:
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point long socialSecurityNumber1 = 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix int x1 = _52; // This is an identifier, not a numeric literal int x2 = 5_2; // OK (decimal literal) int x3 = 52_; // Invalid; cannot put underscores at the end of a literal int x4 = 5_______2; // OK (decimal literal) int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number int x7 = 0x5_2; // OK (hexadecimal literal) int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number int x9 = 0_52; // OK (octal literal) int x10 = 05_2; // OK (octal literal) int x11 = 052_; // Invalid; cannot put underscores at the end of a number
switch语句可以使用字符串了
在jdk7里可以使用字符串对象作为switch语句的表达式,如下面的例子:
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay = "Start of work week"; break; case "Tuesday": case "Wednesday": case "Thursday": typeOfDay = "Midweek"; break; case "Friday": typeOfDay = "End of work week"; break; case "Saturday": case "Sunday": typeOfDay = "Weekend"; break; default: throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); } return typeOfDay; }
switch语句比较它后面的表达式String对象和每一个case标签后的表达式string对象就好像它使用String的equals方法,结果就是在Switch语句中比较String对象是大小写敏感的。一般情况下,比起使用链式的if-then-else 语句在switch语句中使用String对象Java编译器能产生更有效的字节码。