java 去掉分隔符_java7新特性-关于数字的下划线分隔符

本文介绍Java7中新增的数字下划线分隔符特性,该特性允许开发者在数字中加入下划线以提高代码可读性。文章通过多个示例展示了如何正确使用这一特性,并列举了不可使用的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java7新特性之数字的下划线分隔符使用方法实例教程,数字类型上可以加下划线”_”的作用是什么?java7下划线分隔符的使用。

/**

* author:sustbeckham

* java7新特性,数字类型上可以加下划线”_”, 为了增强可读性。

* 不适用的地方:

* 1. 在数字的开头

* 2. 在小数点的左边或者右边

* 3. 在Long或者Float型的后缀符F或者L的左边或者右边

* 4. 不能拆散二进制八进制十六进制数字 比如原本的0X被拆成0_X

*

* 总的来说,个人觉得,这个语法的增强…有那么一丝鸡肋的意味。

**/

public class Underscore

{

public static void main(String args[]){

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;

// 这是一个比较好的增强可读性的例子

long bank_card = 4367_4298_4082_7152_765L;

// 但是…

//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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值