Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Java Literals

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



Literals in Java

A constant value which can be assigned to the variables is called Literal

Example: int var = 20;

Here int is the data type, var is a variable name and 20 is a literal or constant value.

Types of Literals in Java


Integral Literals:

For the integral data types(byte, short, int, long) the following are various ways to specify Literal value.

  1. Decimal Literals: The allowed digits are 0 to 9.

  2. Example: int x=10;

  3. Octal Literals:

  4. The allowed digits are 0 to 7.

    Literal value should be prefixed with 0 (zero).

    Example: int x= 010;

  5. Hexadecimal Literals:

The allowed digits are 0 to 9 , a to f or A to F.

For the extra digits we can use both uppercase and lower case.

This is one of few places where the Java language is not case sensitive.

Literal value should be prefixed with 0x or 0X

Example: int x= 0x10;

Or

int x=0X10;

These are the only possible ways to specify integral literal.

Example:


class Test
{
 // main() method
public static void main(String[] args)
{
int x=10;
int y=010;
int z=0x10;
System.out.println("X= "+x+", Y= "+y+", Z= "+z);
//  o/p – X=10 Y= 8 Z= 16
}

}

Which of the following are valid declarations

  1. int x=10; //valid

  2. int x = 066; //valid

  3. int x= 0786; // compile time exception, integer number too large. 8 not allowed.

  4. int x= 0XFACE; // valid

  5. int x = 0XBEER;// compile time exception. R is not valid

  6. int x= 0xBea; //valid

Note: By default every literal is of int type but we can specify explicitly as long type by suffixing with l or L.

Example:

Int i=10;

int i=10l; // compile time exception. Possible Loss of Precision. Found: long. Required: int.

long l= 10l;

long l= 10;

Note: There is no way to specify that the integral literal is of byte or short type explicitly. If we are assigning integral literal to the byte variable and that integral literal is within the range of byte then it treats as byte literal automatically, similarly short literal also.

Example:

byte b= 10;

byte b= 130; // compile time exception. Possible Loss of Precision.


Floating Point Literals

Every floating point literals are by default double type and hence we cannot assign directly to float type.

But we can specify explicitly floating point literal is of float type by suffixing with f or F.

We can specify floating point literal explicitly as double type by suffixing with d or D.

Example:


float f= 123.456; // Compile time exception. Possible Loss of Precision.
float f= 123.456f;
double d= 123.456;
double d= 123.456d;

Note:-We can specify floating point literal only in decimal form and we cannot specify in octal and hexadecimal form.


Example:


double d= 123.456;// valid
double d= 0x123.456;// Compile time exception

We can assign integral literal directly to the floating point data types and that integral literal can be specified either in decimal form or octal form or hexadecimal form. But we cannot assign floating point literals directly to the integral types.

Example:


int i= 123.456;// Compile time exception
double d= 1.2e3;// o/p = 1200

We can specify floating point literal even in scientific form also.

Example:


double d= 1.2e3;
float f= 1.2e3;// Compile time exception
float f= 1.2e3;

Boolean Literals

The only possible values for the boolean data types are true and false.

Which of the following are valid boolean declarations?


boolean b= 0;  // Compile time exception. Incompatible types
boolean b= True;  // // Compile time exception. Cannot find symbol.
boolean b= "true";  // Compile time exception. Incompatible types
boolean b= true;  // valid
boolean b= false;  // valid

Char Literals

A char literal can be represented as single character within single quotes.

Example:


char c= 'a';
char c= a;  // invalid
char c= 'ab';  // invalid

A char literal can be represented as integral literal which represents unicode of that character.

We can specify integral literal either in decimal form or octal form or hexadecimal form but allowed range 0 to 65535.

Example:


char c= 97;
System.out.println(c); // o/p = a
char c= 65535; 
char c= 65536;// Compile time exception. 
char c= 0xface;
char c= 0642;

A char literal can be represented in Unicode representation which is nothing but \uxxxx 4-digit hexadecimal number.

Example:


char c= '\u0061';
System.out.println(c); // o/p = a


String Literals

Any sequence of characters within " " ( double quotes) is called string literal.

Example:


String name= "Topper Skills";

Note: To work with string literals you need to use String or StringBuffer or StringBuilder class object in Java.

Share the article to help your friends