Preskočiť na hlavný obsah

JAVA 7: Underscores in Numeric Literals

In Java 7 an underscore characters (_) can be put anywhere between digits in a numeric literals.  If application code contains numbers with so many digits, you can use an underscore character to organize digits to the groups of three, four or as much as you want. This absolutly improves code readability.

Examples:


long myPayment   = 1_234_567L;
long phoneNumber = 0421_555_1234_5678L;
long niceColor   = 0x68_FE_A0;
long someFlags   = 0b01101001_10010010;

Rules for this feature 

You can place underscore characters only between digits. You cannot place underscores in the following places:
  • at the beginning or end of a number
  • next to a decimal point in a floating point literal
  • before an F or L suffix
  • in positions where a string of digits is expected
int n = _42;      // WARNING! an identifier, not a literal!
int n = 42_;      // ERROR! underscores at the end of a literal
float e  = 2_.72; // ERROR! underscore before a decimal point
long n = 123_L;   // ERROR! underscores at invalid position
int a = 0_x0D;    // ERROR! underscore in the 0x prefix

Obľúbené príspevky z tohto blogu

mysql 5.0 upgrade to 5.1

The 5.1 series of MySQLwas unmasked for the gentoo portage. When upgrading from an older major version (including 5.0), you will be required to rebuild everything linked to the libmysqlclient.so.15 and libmysqlclient_r.so.15. You can do this by installing app-portage/gentoolkit and running: # revdep-rebuild --library libmysqlclient.so.15 # revdep-rebuild --library libmysqlclient_r.so.15 If you use the Portage 2.2 series, you may also use: # emerge @preserved-rebuild The official upgrade documentation is available here: http://dev.mysql.com/doc/refman/5.1/en/upgrading.html Note that existing databases may need converting as well, again including those upgrading from 5.0 to 5.1.