This article provides lot of examples about initializing primitive data types using different literals like decimal literals, binary literals, Hex literal, Octal Literals, Unicode literals etc. along with negative and two’s complements values.
Java Primitive Literals
Few Facts
- All non fractional literals are by default int unless it has specific suffix like L ( long ) or D ( double ).
- All java primitive data types can be created from int as long as it is within their min & max value range. Ex: byte b = 123 , 123 in itself will be int, but it can be assigned to byte variable since it is within byte max range which is 127 inclusive.
- All fractional floating point literals are by default double unless it has suffix F ( float ).
Literal Number Systems
- Decimal or fractional literals
- Decimal literals do not need any prefix like binary or hex etc.
- Decimals might need suffix like L or D for certain data types. ex: 123L or 123.456D
- Binary literals
- Binary literals are prefixed with 0b i.e. 0b<binary> ex: 0b0111_1011
- Binary literals can have negative sign or you can set leftmost bit to 1 with two’s complement to make it negative
- Hexadecimal literals
- Hex literals are prefixed with 0x i.e. 0x<hex> ex: 0x7B
- Hex literals can have negative sign.
- Octal literals
- Octal literals are prefixed with 0 i.e. 0<octal> ex: 0173
- Octal literals can have negative sign.
- Unicode
- char can have direct character symbol or glyph as literal like 'A' .
- char can also have unicode as literals Ex: '\u0041'
Byte Primitive ( byte )
Byte from decimal literals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Initialize byte from decimal literal byte b = 123; System.out.println("byte from Decimal 123 --> " + b); // Initialize byte from positive signed decimal literal byte b_positive = +123; System.out.println("byte from Decimal +123 --> " + b_positive); // Initialize byte from negative signed decimal literal byte b_minus = -123; System.out.println("byte from negative Decimal -123 --> " + b_minus); /* * Initialize byte from fractional float literal. Needs explicit type casting. * Value after decimal point is ignored. */ byte b_fraction = (byte) 123.456; System.out.println("byte from fraction 123.456 --> " + b_fraction); |
1 2 3 4 |
byte from Decimal 123 --> 123 byte from Decimal +123 --> 123 byte from negative Decimal -123 --> -123 byte from fraction 123.456 --> 123 |
Byte from binary literal
1 2 3 4 5 6 7 8 9 10 11 |
// Initialize byte from binary literal byte b_binary = 0b0111_1011; System.out.println("byte from Binary 0b0111_1011 --> " + b_binary); // Initialize byte from negative sign binary literal byte b_minus_binary = -0b0111_1011; System.out.println("byte from negative Binary -0b0111_1011 --> " + b_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 byte b_minus_binary_2compl = (byte) 0b10000101; System.out.println("byte from Two's compliment Binary with sign bit 0b10000101 --> " + b_minus_binary_2compl); |
1 2 3 |
byte from Binary 0b0111_1011 --> 123 byte from negative Binary -0b0111_1011 --> -123 byte from Two's compliment Binary with sign bit 0b10000101 --> -123 |
Byte from Hex literal
1 2 3 4 5 6 7 |
// Initialize byte from hexadecimal hex literal byte b_hex = 0x7B; System.out.println("byte from Hex 0x7B --> " + b_hex); // Initialize byte from negative hexadecimal hex literal byte b_minus_hex = -0x7B; System.out.println("byte from negative Hex 0x7B --> " + b_minus_hex); |
1 2 |
byte from Hex 0x7B --> 123 byte from negative Hex 0x7B --> -123 |
Byte from Octal literal
1 2 3 4 5 6 7 |
// Initialize byte from octal literal byte b_octal = 0173; System.out.println("byte from Octal 0173 --> " + b_octal); // Initialize byte from negative octal literal byte b_minus_octal = -0173; System.out.println("byte from negative Octal 0173 --> " + b_minus_octal); |
1 2 |
byte from Octal 0173 --> 123 byte from negative Octal 0173 --> -123 |
Short Primitive ( short )
Short from decimal literal
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Initialize short from decimal literal short s = 123; System.out.println("short from Decimal 123 --> " + s); // Initialize short from signed decimal literal short s_positive = +123; System.out.println("short from Decimal +123 --> " + s_positive); short s_minus = -123; System.out.println("short from negative Decimal -123 --> " + s_minus); // Initialize short from fractional float literal. Value after decimal point is // ignored. short s_fraction = (short) 123.456; System.out.println("short from fraction 123.456 --> " + s_fraction); |
1 2 3 4 |
short from Decimal 123 --> 123 short from Decimal +123 --> 123 short from negative Decimal -123 --> -123 short from fraction 123.456 --> 123 |
Short from binary literal
1 2 3 4 5 6 7 8 9 |
// Initialize short from binary literal short s_binary = 0b0111_1011; System.out.println("short from Binary 0b0111_1011 --> " + s_binary); // Initialize byte from negative sign binary literal short s_minus_binary = -0b0111_1011; System.out.println("short from negative Binary -0b0111_1011 --> " + s_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 short s_minus_binary_2compl = (byte) 0b10000101; System.out.println("short from Two's compliment Binary with sign bit 0b10000101 --> " + s_minus_binary_2compl); |
1 2 3 |
short from Binary 0b0111_1011 --> 123 short from negative Binary -0b0111_1011 --> -123 short from Two's compliment Binary with sign bit 0b10000101 --> -123 |
Short from Hex literal
1 2 3 4 5 |
// Initialize short from hexadecimal hex literal short s_hex = 0x7B; System.out.println("short from Hex 0x7B --> " + s_hex); short s_minus_hex = -0x7B; System.out.println("short from negative Hex 0x7B --> " + s_minus_hex); |
1 2 |
short from Hex 0x7B --> 123 short from negative Hex 0x7B --> -123 |
Short from octal literal
1 2 3 4 5 |
// Initialize short from octal literal short s_octal = 0173; System.out.println("short from Octal 0173 --> " + s_octal); short s_minus_octal = -0173; System.out.println("short from negative Octal 0173 --> " + s_minus_octal); |
1 2 |
short from Octal 0173 --> 123 short from negative Octal 0173 --> -123 |
Integer Primitive ( int )
Int from decimal literal
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Initialize int from decimal literal int i = 123; System.out.println("int from Decimcal 123 --> " + i); // Initialize int from signed decimal literal int i_positive = +123; System.out.println("int from Decimcal +123 --> " + i_positive); int i_minus = -123; System.out.println("int from negative Decimal -123 --> " + i_minus); // Initialize int from fractional float literal. Value after decimal point is // ignored. int i_fraction = (int) 123.456; System.out.println("int from fraction 123.456 --> " + i_fraction); |
1 2 3 4 |
int from Decimcal 123 --> 123 int from Decimcal +123 --> 123 int from negative Decimal -123 --> -123 int from fraction 123.456 --> 123 |
Int from binary literal
1 2 3 4 5 6 7 8 9 |
// Initialize int from binary literal int i_binary = 0b0111_1011; System.out.println("int from Binary 0b0111_1011 --> " + i_binary); // Initialize byte from negative sign binary literal int i_minus_binary = -0b0111_1011; System.out.println("int from negative Binary -0b0111_1011 --> " + i_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 int i_minus_binary_2compl = (byte) 0b10000101; System.out.println("int from Two's compliment Binary with sign bit 0b10000101 --> " + i_minus_binary_2compl); |
1 2 3 |
int from Binary 0b0111_1011 --> 123 int from negative Binary -0b0111_1011 --> -123 int from Two's compliment Binary with sign bit 0b10000101 --> -123 |
Int from Hex literal
1 2 3 4 5 |
// Initialize int from hexadecimal hex literal int i_hex = 0x7B; System.out.println("int from Hex 0x7B --> " + i_hex); int i_minus_hex = -0x7B; System.out.println("int from negative Hex 0x7B --> " + i_minus_hex); |
1 2 |
int from Hex 0x7B --> 123 int from negative Hex 0x7B --> -123 |
Int from Octal literal
1 2 3 4 5 |
// Initialize int from octal literal int i_octal = 0173; System.out.println("int from Octal 0173 --> " + i_octal); int i_minus_octal = -0173; System.out.println("int from negative Octal 0173 --> " + i_minus_octal); |
1 2 |
int from Octal 0173 --> 123 int from negative Octal 0173 --> -123 |
Long primitive ( long )
For long primitive, you can use suffix ‘L’ or ‘l’ after number. Till the max range on int (2147483647), suffix is not needed because by default it will be created as int & then assigned to long variable. So long l = 2147483647; compiles fine. But beyond range of int i.e. long l = 2147483648; , you will get compilation error The literal 2147483648 of type int is out of rangehence you MUST use suffix ‘L’ or ‘l’ long l = 2147483648L;
Quote from Oracle
It is recommended that you use the upper case letter ‘L’ because the lower case letter ‘l’ is hard to distinguish from the digit 1.
Long from decimal literal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Initialize long from decimal literal long l = 123; System.out.println("long from Decimcal 123 --> " + l); // Initialize long from positive signed decimal literal long l_positive = +123; System.out.println("long from Decimcal +123 --> " + l_positive); // Initialize long from negative signed decimal literal long l_minus = -123; System.out.println("long from negative Decimal -123 --> " + l_minus); // Initialize long from decimal literal with L or l suffix long l_suffix = 2147483648l; System.out.println("long from Decimcal 2147483648l --> " + l_suffix); long l_minus_suffix = -2147483648L; System.out.println("long from negative Decimal -2147483648L --> " + l_minus_suffix); // Initialize long from fractional float literal. Value after decimal point is // ignored. long l_fraction = (long) 123.456; System.out.println("long from fraction 123.456 --> " + l_fraction); |
1 2 3 4 5 6 |
long from Decimcal 123 --> 123 long from Decimcal +123 --> 123 long from negative Decimal -123 --> -123 long from Decimcal 2147483648l --> 2147483648 long from negative Decimal -2147483648L --> -2147483648 long from fraction 123.456 --> 123 |
Long from Binary literal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Initialize long from binary literal long l_binary = 0b0111_1011; System.out.println("long from Binary 0b0111_1011 --> " + l_binary); // Initialize long from binary literal with suffix long l_binary_suffix = 0b0111_1011L; System.out.println("long from Binary 0b0111_1011L --> " + l_binary_suffix); // Initialize byte from negative sign binary literal long l_minus_binary = -0b0111_1011; System.out.println("long from negative Binary -0b0111_1011 --> " + l_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 long l_minus_binary_2compl = (byte) 0b10000101; System.out.println("long from Two's compliment Binary with sign bit 0b10000101 --> " + l_minus_binary_2compl); |
1 2 3 4 |
long from Binary 0b0111_1011 --> 123 long from Binary 0b0111_1011L --> 123 long from negative Binary -0b0111_1011 --> -123 long from Two's compliment Binary with sign bit 0b10000101 --> -123 |
Long from Hex literal
1 2 3 4 5 6 7 8 |
// Initialize long from hexadecimal hex literal long l_hex = 0x7B; System.out.println("long from Hex 0x7B --> " + l_hex); long l_minus_hex = -0x7B; System.out.println("long from negative Hex 0x7B --> " + l_minus_hex); // Initialize long from hexadecimal hex literal with suffix L long l_hex_suffix = 0x7BL; System.out.println("long from Hex 0x7BL --> " + l_hex_suffix); |
1 2 3 |
long from Hex 0x7B --> 123 long from negative Hex 0x7B --> -123 long from Hex 0x7BL --> 123 |
Long from Octal literal
1 2 3 4 5 6 7 8 |
// Initialize long from octal literal long l_octal = 0173; System.out.println("long from Octal 0173 --> " + l_octal); long l_minus_octal = -0173; System.out.println("long from negative Octal 0173 --> " + l_minus_octal); // Initialize long from octal literal with suffix L long l_octal_suffix = 0173L; System.out.println("long from Octal 0173 --> " + l_octal_suffix); |
1 2 3 |
long from Octal 0173 --> 123 long from negative Octal 0173 --> -123 long from Octal 0173 --> 123 |
Float primitive ( float )
Float from decimal literal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Initialize float from decimal literal float f = 123; System.out.println("float from Decimcal 123 --> " + f); // Initialize float from positive signed decimal literal float f_positive = +123; System.out.println("float from Decimcal +123 --> " + f_positive); // Initialize float from negative signed decimal literal float f_minus = -123; System.out.println("float from negative Decimal -123 --> " + f_minus); // Initialize float from decimal literal with L or l suffix float f_suffix = 2147483648f; System.out.println("float from Decimcal 2147483648f --> " + f_suffix); float f_minus_suffix = -2147483648F; System.out.println("float from negative Decimal -2147483648F --> " + f_minus_suffix); // Initialize float from fractional literal. float f_fraction = 123.456F; System.out.println("float from fraction 123.456 --> " + f_fraction); // Initialize float from negative fractional literal. float f_minus_fraction = -123.456f; System.out.println("float from fraction -123.456 --> " + f_minus_fraction); |
1 2 3 4 5 6 7 |
float from Decimcal 123 --> 123.0 float from Decimcal +123 --> 123.0 float from negative Decimal -123 --> -123.0 float from Decimcal 2147483648f --> 2.14748365E9 float from negative Decimal -2147483648F --> -2.14748365E9 float from fraction 123.456 --> 123.456 float from fraction -123.456 --> -123.456 |
Float from scientific notation
1 2 3 4 5 |
/* * Float from scientific notation */ float f_sc = 1.234e2f; System.out.println("float from scientific notation --> " + f_sc); |
1 |
float from scientific notation --> 123.4 |
Float from binary literal
1 2 3 4 5 6 7 8 9 10 11 |
// Initialize float from binary literal float f_binary = 0b0111_1011; System.out.println("float from Binary 0b0111_1011 --> " + f_binary); // Initialize byte from negative sign binary literal float f_minus_binary = -0b0111_1011; System.out.println("float from negative Binary -0b0111_1011 --> " + f_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 float f_minus_binary_2compl = (byte) 0b10000101; System.out.println("float from Two's compliment Binary with sign bit 0b10000101 --> " + f_minus_binary_2compl); |
1 2 3 |
float from Binary 0b0111_1011 --> 123.0 float from negative Binary -0b0111_1011 --> -123.0 float from Two's compliment Binary with sign bit 0b10000101 --> -123.0 |
Float from Hex literal
Do not add ‘F’ suffix to hex in float. ‘F’ is a hexadecimal digit so changes the value i.e. float f_hex_suffix = 0x7BF will be 1983.0 & not 123.
1 2 3 4 5 |
// Initialize float from hexadecimal hex literal float f_hex = 0x7B; System.out.println("float from Hex 0x7B --> " + f_hex); float f_minus_hex = -0x7B; System.out.println("float from negative Hex 0x7B --> " + f_minus_hex); |
1 2 |
float from Hex 0x7B --> 123.0 float from negative Hex 0x7B --> -123.0 |
Float from Octal literal
Do not add ‘F’ suffix to octal literal. It will be treated as decimal float & not octal i.e float f_octaf_suffix = 0173F; will generate output of 173.0 & not 123.0.
1 2 3 4 5 |
// Initialize float from octal literal float f_octal = 0173; System.out.println("float from Octal 0173 --> " + f_octal); float f_minus_octal = -0173; System.out.println("float from negative Octal 0173 --> " + f_minus_octal); |
1 2 |
float from Octal 0173 --> 123.0 float from negative Octal 0173 --> -123.0 |
Double primitive ( double )
Double from decimal literal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// Initialize double from decimal literal double d = 123; System.out.println("double from Decimcal 123 --> " + d); // Initialize double from positive signed decimal literal double d_positive = +123; System.out.println("double from Decimcal +123 --> " + d_positive); // Initialize double from negative signed decimal literal double d_minus = -123; System.out.println("double from negative Decimal -123 --> " + d_minus); // Initialize double from decimal literal with D or d suffix double d_suffix = 2147483648d; System.out.println("double from Decimcal 2147483648f --> " + d_suffix); double d_minus_suffix = -2147483648D; System.out.println("double from negative Decimal -2147483648F --> " + d_minus_suffix); // Initialize double from fractional literal. double d_fraction = 123.456; System.out.println("double from fraction 123.456 --> " + d_fraction); // Initialize double from negative fractional literal. double d_minus_fraction = -123.456; System.out.println("double from fraction -123.456 --> " + d_minus_fraction); // Initialize double from fractional literal with suffix. double d_fraction_suffix = 123.456d; System.out.println("double from fraction with suffix 123.456d --> " + d_fraction_suffix); // Initialize double from negative fractional literal with suffix. double d_minus_fraction_suffix = -123.456D; System.out.println("double from fraction with suffix -123.456D --> " + d_minus_fraction_suffix); |
1 2 3 4 5 6 7 8 9 |
double from Decimcal 123 --> 123.0 double from Decimcal +123 --> 123.0 double from negative Decimal -123 --> -123.0 double from Decimcal 2147483648d --> 2.147483648E9 double from negative Decimal -2147483648D --> -2.147483648E9 double from fraction 123.456 --> 123.456 double from fraction -123.456 --> -123.456 double from fraction with suffix 123.456d --> 123.456 double from fraction with suffix -123.456D --> -123.456 |
Double from scientific notation
1 2 3 4 5 |
/* * Double from scientific notation */ double d_sc = 1.234e2; System.out.println("double from scientific notation --> " + d_sc); |
1 |
double from scientific notation --> 123.4000015258789 |
Double from binary literal
1 2 3 4 5 6 7 8 9 10 11 |
// Initialize double from binary literal double d_binary = 0b0111_1011; System.out.println("double from Binary 0b0111_1011 --> " + d_binary); // Initialize byte from negative sign binary literal double d_minus_binary = -0b0111_1011; System.out.println("double from negative Binary -0b0111_1011 --> " + d_minus_binary); // Initialize byte from two's complement binary literal with sign bit as 1 double d_minus_binary_2compl = (byte) 0b10000101; System.out.println("double from Two's compliment Binary with sign bit 0b10000101 --> " + d_minus_binary_2compl); |
1 2 3 |
double from Binary 0b0111_1011 --> 123.0 double from negative Binary -0b0111_1011 --> -123.0 double from Two's compliment Binary with sign bit 0b10000101 --> -123.0 |
Double from Hex literal
Do not add ‘D’ suffix to hex in double. ‘D’ is a hexadecimal digit so changes the value i.e. double d_hex_suffix = 0x7BD; will be 1981.0 & not 123.
1 2 3 4 5 |
// Initialize double from hexadecimal hex literal double d_hex = 0x7B; System.out.println("double from Hex 0x7B --> " + d_hex); double d_minus_hex = -0x7B; System.out.println("double from negative Hex 0x7B --> " + d_minus_hex); |
1 2 |
double from Hex 0x7B --> 123.0 double from negative Hex 0x7B --> -123.0 |
Double from octal literal
Do not add ‘D’ suffix to octal literal. It will be treated as decimal double & not octal i.e double d_octal_suffix = 0173D; will generate output of 173.0 & not 123.0.
1 2 3 4 5 |
// Initialize double from octal literal double d_octal = 0173; System.out.println("double from Octal 0173 --> " + d_octal); double d_minus_octal = -0173; System.out.println("double from negative Octal 0173 --> " + d_minus_octal); |
1 2 |
double from Octal 0173 --> 123.0 double from negative Octal 0173 --> -123.0 |
Character primitive ( char )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* * Initialization of char primitive from literals. */ // Initialize char directly using character literal char a = 'A'; System.out.println("char from literal A --> " + a); // Initialize char from binary literal char a_binary = 0b0100_0001; System.out.println("char from Binary 0b0100_0001 --> " + a_binary); // Initialize char from unicode literal char a_unicode = '\u0041'; System.out.println("char from Unicode \\u0041 --> " + a_unicode); // Initialize char from decimal int literal char a_int = 65; System.out.println("char from Decimal 65 --> " + a_int); // Initialize char from hexadecimal hex literal char a_hex = 0x41; System.out.println("char from Hex 0x41 --> " + a_hex); // Initialize char from Octal literal char a_octal = 0101; System.out.println("char from Octal 0101 --> " + a_octal); |
1 2 3 4 5 6 |
char from literal A --> A char from Binary 0b0100_0001 --> A char from Unicode \u0041 --> A char from Decimal 65 --> A char from Hex 0x41 --> A char from Octal 0101 --> A |
Boolean primitive ( boolean )
1 2 3 4 5 6 7 |
/* * Initialization of double primitive from literals. */ boolean bool_true = true; System.out.println("boolean true --> " + bool_true); boolean bool_false = false; System.out.println("boolean false --> " + bool_false); |
1 2 |
boolean true --> true boolean false --> false |