黑马程序员---Java SE 7引入的三个新特性

 ------- android培训java培训、期待与您交流! ----------

二进制直接量

Java SE 7中,整型类型(byte,short,int,long)也可以使用二进制数字系统表示。为了指定二进制直接量在二进制值前面加上0b或者0B。下面的例子演示了二进制直接量

// 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 SE 7往后,在数字直接量的任意数字之间可以出现任意数量的下划线。这个特性使你能够,例如,分开一组数字直接量这样可以提高代码的可阅读性。

比如如果你的代码包含的数字有许多数字位,你可以使用下划线字符把数字分成三个一组,类似于你使用标点符号像逗号,空格作为分隔符一样。

下面的例子演示了其他的方式你可以在数字直接量使用下划线:

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;


 
 

下划线只能被使用在数字之间;下划线不能被使用在下列位置:

 

  • 一个数字开始或结束的位置
  • 临近浮点直接量的小数点
  • FL后缀的前面
  • 当数字用字符串表示的时候

下面的例子演示了正确的和不正确的下划线放置位置:

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对象就好像它使用Stringequals方法,结果就是在Switch语句中比较String对象是大小写敏感的。一般情况下,比起使用链式的if-then-else 语句在switch语句中使用String对象Java编译器能产生更有效的字节码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值