Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Java 7: Binary literal type

From Java 7 Binary type also joins the category of literal with *

byte, short, int and long

.* Which means a binary value can be assigned  by using 0b or 0B  prefix to a  primitive type.

What is a Literal :  A literal is source code representation of source code. Which can be directly assigned to any primitive type.

Try following in your code with JDK1.7+

int intType = 0b100; 

char charType = 0B1100001;

Why Binary Literals:

  • No need to translate binary value to integer to use them.
  • Benefit to programs where bit per bit operation is required.

Other literal type are following

char capitalC = 'C'; /*Character type or you can use unicode 'u0043' */
byte b = 100;   /* byte limit is -128 to 127*/
short s = 10000; /* short limit is -32768 to 32767*/
int i = 100000;  /*int limit is -2^31 to 2^31 - 1*/

One more feature introduced related to primitive type.

Underscore in integer type: To improve readability of code in integer delimiters.

int newInt = 100_00_00;
float newfloat = 3.12_34_67f;

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy