Java Primitive Data Types Ultimate Reference | Many ways to initialize literals (decimal, binary, hex, octal, unicode etc.)

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

Byte from binary literal

Byte from Hex literal

Byte from Octal literal



Short Primitive ( short )

Short from decimal literal

Short from binary literal

Short from Hex literal

Short from octal literal



Integer Primitive ( int )

Int from decimal literal

Int from binary literal

Int from Hex literal

Int from Octal literal



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

Long from Binary literal



Long from Hex literal

Long from Octal literal



Float primitive ( float )

Float from decimal literal

Float from scientific notation



Float from binary literal

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.

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.



Double primitive ( double )

Double from decimal literal

Double from scientific notation



Double from binary literal

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.

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.



Character primitive ( char )



Boolean primitive ( boolean )



 

 

Leave a Reply

Your email address will not be published.