ExamTray App is now Available on Google Play EXAMTRAY android app link

 

Last Minute Java Programming Data Literals or Constants Tutorial

java literals or constants explained

Like any other programming language, Java too supports Data constants or Literals. A literal is simply a constant of data we assign to a variable of a particular data type.

Java Data Literals or Constants

There are 5 types of Literals or constants in Java language.

  1. Integer Literals
  2. Floating Point Literals
  3. Boolean Literals
  4. Character Literals
  5. String Literals

JDK 7 introduced two new features namely Binary Literals and Underscores in number literals.

Underscore in number constants or literals for Readability

You can embed underscore symbols ( _ ) as part of a number constant to increase readability. You should not add Underscore at the beginning or end. You can add more than one underscore symbols consecutively. Underscore symbols are removed before processing. Examples are as below.

int a = 123_456_7;
int b = 23_456_56789_1;
long c = 24___56__789_12L;
int d = 0xef_a_b; //Hexadecimal constant
int e = 0b1011_0111; //binary constant
int f = 01234_5678; //Octal constant
float pp = 34__456.12___23_4; //floating point constant
double qq = 455_456.126_234;  //double constant

 

1. Integer Literals

When initializing byte, short, int and long data type variables, we use integer literals. For long data, you can use lower case 'l' or upper case 'L' at the end if you want.

Integer literals or constants of 4 types.

  1. Decimal Literal
  2. Octal Literal
  3. Hexadecimal Literal
  4. Binary Literals

A. Decimal Literal

Decimal literals can have digits from 0 to 9. There is no need to prefix the number to specify explicitly.

Example:

int a = 0987654321;
int b = 987_654_321_0;
short c = 3456;
byte bt = 123;

B. Octal Literal

Octal literals can have digits from 0 to 7. You should Prefix the number with a ZERO '0'.

Example:

int a = 0345;
int b = 0456_789;
short c = 0802; //Error. 8 is out of range of Octal.
byte bt = 110;

C. Hexadecimal Literal

Hexadecimal literals can have digits from 0 to 9, a, b, c, d, e, f, A, B, C, D, E, F. You should Prefix the number with a '0x' or '0X'.

Example:

int a = 0x345;
int b = 0X456_7__89;
long ab = 0xffeeL;

D. Binary Literal

Binary literals can have digits from 0 or 1. You should Prefix the number with a '0b' or '0B'. JDK 7 supports this literal.

Example:

int a = 0b1011_1010;
int b = 0B10__10__11_01;
long ab = 0b1101_1100_0101_1111L;

2. Floating Point Literals

Floating point data types namely float and double variables can hold floating point literals. For floats 'f' or 'F' may be postfixed at the end. For double, you may append 'd' or 'D' at the end of a number. By default, all real numbers are of 'double' data type.

To specify exponents 'e' or 'E' is used. Exponent represents 10 power some number say 10 power 2 ( = 100). So E is called Power of Ten Exponent.

To specify Two power of some number, JDK 7 introduced an exponent which represents 2 instead of 10. The symbol used is 'p' or 'P'. So, P is called Power of Two Exponent. This P format can be used only with Hexadecimal floating point numbers.

Example:

float a = 123_456.789_0987F;
float b = 123456.78678f;
float c = 12e23;
double d = 345E567d; // E notation
double e = 345E-567;
double f = 345E+567;
float g = 0x23.4567P4; //Hexadecimal float
double h = 0X5678p-10; //P notation

3. Boolean Literals

Boolean literal can have only two possible values true or false. TRUE or FALSE are illegal to use. Capitalization is not allowed. Also, 1 is not equal to true and 0 is not equal to false.

Example:

boolean a = false;
boolean b = true;
boolean c = 0; //error
boolean d = -123; //error
if( a ) { } // Works
if( 1 ) { } // Error.
if( TRUE ) { } // Error.
if( false ) { } //Works.

4. Character Literals

As Java language supports 16 bit type characters, the number of characters that can be represented has been increased to 65536. You can specify a single character as a character literal to a character variable. To specify a character literal, you need to surround the character with Single Quotes (' ').

Example:

char a='a';
char b = 'A';
char c = 'ab'; //Error

You can also specify Octal character literal in \ddd notation. 'd' is the digit from 0 to 7.

Example:

char a = '\123';
char b = '\567';
char c = '345'; //Error

You can also specify Hexadecimal character literal in \udddd notation. 'd' value can be from 0 to 9 and a to f or A to F. These are also called Unicode literals in Java.

Example:

char a = '\uaef0';
char b = '\ua123';
char c = "\u1234"; //Error. Double Quote

5. String Literals

String literal is a group of characters, numbers and symbols surrounded by double quotes. In C language, a string is a character array. In Java language, String is an Object. It has methods to calculate length, reverse string, replace characters and more.

Example:

String a = "Hello Java";
String b = "Hello\nJava"; //you can use new line
String c = "Hello" + " Java"; //Concatenation

Having an understanding of literals improves your coding. You can write better java programs after practising these basics.