Java se 7 新特性研究(三)

下划线在数字表达中的应用.

在 Java se 7中可以使用下划线(_)来分组表示数字.

int x1=2323_234_21;

double x3=232_2.324_12F;

其在以下位置不可以使用:

1.数字字面的开头与结尾;

2.小数点前和后;

3.表示类型的L或F前或后.

4.表示数进制的前和中间;

如下:

 

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

 

泛型声明的改进

在JAVA Se 7及以后的版本,声明泛型时泛型类可以使用(<>)来声明,编译器会自动根据上下文件取适合的类型.如下例:

Java se 7以前:

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Java se 7及以后:

Map<String, List<String>> myMap = new HashMap<>();

但是下面的的方式还是会出没有检查的转换警告;

Map<String, List<String>> myMap = new HashMap();

 

下面使用是不允许的:

List<String> list = new ArrayList<>();
list.add("A");

  // The following statement should fail since addAll expects
  // Collection<? extends String>

list.addAll(new ArrayList<>());

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuoShunkui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值