Code:
package c1.s2; /** * @author Mikalai Zaikin */ public class BinaryLiterals { // An 8-bit 'byte' value: byte aByte = 0b00100001; // A 16-bit 'short' value: short aShort = 0b0010001010001010; // Some 32-bit 'int' values: int anInt1 = 0b10100001010001011010000101000101; int anInt2 = 0b101; int anInt3 = 0B101; // The B can be upper or lower case. // A 64-bit 'long' value. Note the "L" suffix: long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L; double d1 = 0b0001; double d2 = 0b0001d; // COMPILATION ERROR: can only be of integer type ! float f1 = 0b0001; float f2 = 0b0001f; // COMPILATION ERROR: can only be of integer type ! }
Code:
package c1.s2; /** * @author Mikalai Zaikin */ public class Underscores { 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; // VALID int x2 = 5_2; // OK (decimal literal) int x4 = 5_______2; // OK (decimal literal) int x7 = 0x5_2; // OK (hexadecimal literal) int x10 = 0B0_0_0; // OK (binary literal) // INVALID 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 ssn = 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix int x3 = 52_; // Invalid; cannot put underscores at the end of a 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 x8 = 0x52_; // Invalid; cannot put underscores at the end of a number int x1 = _52; // This is an identifier, not a numeric literal }
![]() ![]() ![]() |