Java Literals - The Coding Shala

Home >> Learn Java >> Java Literals

Java Literals

Java literals are constant values that we assigned to the variables called literals or constant. For example - 

int var = 10;
//here 10 is integer literal.

Java Literals - The Coding Shala

Integer Literals

Any Whole number value is an integer literal. For example 1,2, 3, 4, 32, etc all are integer literals. In java decimal values(base 10), octal(base 8) and hexadecimal(base 16) can be used as integer literals.

octal literals

In Java, Octal values are denoted by a leading zero and range is 0 to (base 8). For example 05, 06, 045, etc. are octal literals and 09  will give an error it's not octal(9 is not in range) or decimal.  

decimal literals

In Java Decimal literals cannot have a leading zero and range is 0 to 9(base 10). For example 10, 25, 78 etc.

hexadecimal literals

In Java, Hexadecimal literals start with leading zero-x (0x or 0X) and the range of a hexadecimal digit is 0 to 15(base 16). For 10 to 15 we use A to F(or a to f) like 10 is represented by A, 11 is B and 15 is F. Examples of hexadecimal literals are 0x023A, 0X09C, etc.

Binary Literals

If literals start with 0b or 0B then it is binary literals and allowed digits are 0 and 1. For examples 0b1111,0B1001 etc.

In Java, integer literals create an int value. Whenever a literal value is assigned to a byte or short no error will generate if a literal value is within the range of the type. To assign integer literal to a long variable we have to append 'L' or 'l' to the literal. For example, 2327462373286l, 0x2248ADL, etc. we can also assign an integer literal to char within its range like char a = 67;

Floating-point literals

Floating-point numbers(real numbers) have decimal values with a fractional component. In Java, floating-point literals to double precision. To specify a float literal we have to append 'F' or 'f' to the literal. We can also specify a double literal by appending 'D' or 'd' to the literals. For examples- 

flolat ft = 34.233f;
double = 343.33432;

Boolean Literals

Boolean literals can have two values true and false. In Java, the value true is not equal to 1 and value false is not equal to the 0. Boolean literals can only be assigned to the variables declared as a boolean data type. For example - 

boolean a = true;
boolean b = false;
boolean aa = 1; //will give error

Character Literals

In Java, characters are represented by Unicode. A literal character is represented inside a pair of a single quote like char a = 'A' or can be converted into integers and we can use integer operators on that, such as addition and subtraction operators.  For char data type we can specify literals in the following ways - 

Single Quote 

We can specify literals to char as a single character inside a pair of a single quote. 

char ch = 'X';
char ch1 = 'a';

Integral literal 

we can specify char literal as integral literal within its range(0 to 65535). These integral literals can be specified in decimal, octal, and hexadecimal forms. 

char ch = 97;
char ch1 = 062;

Unicode Representation

We can specify char literals in Unicode representation. To represent in Unicode we use '\uxxxx'. Here xxxx represents 4 hexadecimal numbers. 

char ch = '\u0061';  // here /u0061 represent a.

Escape Sequence

The escape character can be specified as char literals. The following is Character Escape Sequence - 
Java Escape Sequence - The Coding Shala

String Literals

Any sequence of characters within double quotes is treated as String literals. Examples of string literals are - 

String str = "Hello World";

The following Java Program will explain literals - 

public class DataTypeExample{
    public static void main(String args[]){
    int it = 45; //in decimal
    int it1 = 0100; //octal 
    int it2 = 0xA;  //Hexadecimal
    int it3 = 0b1111;  //boolean
    System.out.println("This is an integer literal in decimal: " + it);
    System.out.println("This is an integer literal in octal: " + it1);
    System.out.println("This is an integer literal in hexadecimal: " + it2);
    System.out.println("This is an integer literal in boolen: " + it3);
    long distance = 27452937356723l; //to declare long we have to append 'l' or 'L'
    System.out.println("This is long type distance : " + distance);
    float ft = 3.14f;
    //in Java floating point default to double precision
    //to specify float we have to append 'f' or 'F'.
    System.out.println("This is float type ft: " + ft);
    double dt = 3.14;
    System.out.println("This is double type dt: " + dt);
    char ch1 = 'X';
    char ch2 = 66;
    char ch3 = '\u0061';
    System.out.println("This is char ch1 : " + ch1);
    System.out.println("This is char ch2 : " + ch2);
    System.out.println("This is char ch3 : " + ch3);
    boolean b1 = true;
        System.out.println("This is boolean b1 with having value " + b1);
    String name = "Akshay Saini";
    String hello = "Hello \n There";

This is an integer literal in decimal: 45
This is an integer literal in octal: 64
This is an integer literal in hexadecimal: 10
This is an integer literal in boolen: 15
This is long type distance : 27452937356723
This is float type ft: 3.14
This is double type dt: 3.14
This is char ch1 : X
This is char ch2 : B
This is char ch3 : a
This is boolean b1 with having value true
Akshay Saini

Point to Remember: We can use underScore(_) in numeric Literals. If our number contains many digits, we can use an underscore character to separate digits.
public class UnderScore{
    public static void main(String[] args){
        int num = 2_00_00;
        float ff = 3.00_555_333f;
        System.out.println("number is "+num);
        System.out.println("float is  "+ff);

number is 20000
float is  3.0055532

Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.


Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala