Literal in Java

This tutorial will explain about Literals in Java.


Literal in Java

In this section you will learn about literals in Java. "literal" is a constant value. A literal is a constant value that can be written directly to java program. Literals is a representation of fixed value in your source code. Literals is used to initializes the variable in the program. Literals are used directly in your source code without any computation. Literals are declared as follows:

int a=10;
char c='A';

For example :

Integer literals : 10 211 -90
Character literals : 'A'
String literals : "Ab"
Boolean literals : true false
floating point literals : .9 3.14 9.88  

Integer Literals :

An int is represented using integer literals. An integer literal are primary literal used in programming language. They are in different forms like decimal , hexadecimal and octal. The decimal literal are represented with base 10, hexadecimal represented with base 16 and octal represented with base 8. These format are correspond to the base of the number system used by the literal. Decimal (base 10) literal appear as ordinary number with no special character. Hexadecimal number represented with a leading ox or 0x. Octal number are represented with a leading o in front of the digit. For example, an integer literal of decimal 12 is represented as 12 and 0xc in hexadecimal, 014 in octal.

Character Literals :

A char is represented using character literals. We can specify the char literals by pair of single quotes. Character in java are represented by Unicode 16-bit . Special character are represented by backslash(\) followed by character. A good example of special character is \n, Which forces the output to new line when printed. The table below show the special character.

Description Representation
New Line\n
Carriage Return\r
Single quotation mark\'

Floating point Literals:

Floating point represent the decimal number in fractional part like 3.14. Default data type of floating type is double. Unlike integer literals, floating-point literals default to the double type, which is a 64-bit value. You have need or require you can use smaller 32-bit float type as well. You do this by appending an f or F to the end of the number, as in 5.638 . Floating-point numbers are like real numbers in mathematics, for example, 2.13232, -0.7877. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double, but you can designate it explicitly by appending the D (or d) suffix. However, the suffix F (or f) is appended to designate the data type of a floating-point literal as float. We can also specify a floating-point literal in scientific notation using Exponent (short E ore)

String Literals :

The String literals are represented with double quotes. String literals are represented in java by String class. When Java encounter a String, then it sets character appearing within double quotes. String literal are represented by multiple characters: empty string is represented by "",  "\"" represent a string contain, "hello" represent a string with 5 characters.


Share on Google+Share on Google+

Literal in Java

Posted on: July 3, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Literal in Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image