What are literals in java

What are Literals in Java? With examples. | Types of Literals in Java | Get a complete overview of the best books and courses to learn Java.

Spread this useful information with your friends if you liked.

What are Literals in Java?

Literals is a very interesting concept to learn in java. In this article will understanding “what are literals in java” with its types and examples for easier understanding.

What are Literals in Java?

In Java, A literal is a representation of a constant value in a source code. These literals are used directly in the code without any computations.

Here is an example of a literal:

char a = ‘A’;
int  b = 24;
float c = 1.5;

In the above example the variables a, b, c are assigned to the fixed values A, 24, 1.5. Any primitive type variable can be assigned as a literal.  

Types of Literals

1. Integer literals:

An Integral literal can be specified using 4 ways:

  • Decimal Integer:

Decimal Integers are set of numbers comprising digits0 from 0-9. The numbers can be positive (e.g. 54) or negative (e.g. -65).

The base of an Octal Integer is 8.

int deci_value = 54;
  • Octal Integer:

An Octal integer consists of any digits from 0-7, also the octal literal must start with 0.

The base of an Octal Integer is 8.

int octal_value = 0655;
  • Hexa-decimal Integer:

A Hexa-decimal Integer consists of digits from 0-9 and characters from a-f. The characters used in a Hexa-decimal can be in uppercase or lowercase. The characters A to f (or a – f) represent numbers from 10 to 15. In Java, the sequence of digits and characters preceded by 0X or 0x are considered as a Hexa-decimal integer.

The base of a Hexa-decimal integer is 16.

int hexa_value = 0x1AB;
  • Binary Integer:

A binary Integer consist of digits 0 & 1. The base of a binary number is 2. In java a binary integer is represented using 0b or 0B prefix.

int binary_value = 0b1001;

2. Floating -Point literal:

A Floating-point literal is a numeric literal containing decimal point. In Java, the primitive data types float and double falls in the category of Floating-point literal.

float a = 12.93F;
double b = 12.93;

3. Character Literals:

For char primitive data type we can represent in 4 ways.

  • Single quote:

We can specify a char primitive data type literal as an Alphabet within a single quote (‘’).

char alphabet = ‘A’
  • Char literal in the form of Integer literal:

We can use an Integer literal as a char literal. The integer value represents Unicode value of a character. The integer literal can be specified in Decimal, Octal or Hexadecimal form.

char int_character = 064;
  • Char literal in Unicode representation:

The char literal can be specified using Unicode representation. The format of Unicode representation is ‘\uxxxx’, here the xxxx represents 4 hexadecimals.

char unicode_ch = ‘\u0061’;
//here \u0061 represents a.
  • Char literal as Escape sequence:

In Java, The escape sequence can be used as Char literal.

char escape_ch = ‘\n’;

3. Boolean literals:

The Boolean literal is the value represent in the form of true or false. It may also be represented in 0 and 1, 0 mean false and 1 mean true.

boolean a = true;
boolean b = 0;

4. String literals:

String is data type that comprises of sequence of characters. Strings are represented in doubles quotes (“”).

String name = “John”;

5. Null literal:

Null literal is a representation to indicate that reference type object is unavailable. The null value can be assigned to any variable, except variables of primitive data types.

String hello = null;

Why do we use literal?

Literals are often used to initialize a variable. In Java, literals are syntactic representations of Boolean, numeric, integer or string primitive data types.

In Java, a literal can be identified with the prefix of ‘=’ followed by a value.

Example of Literals

package variables;

public class ExampleLiterals {
    public static void main(String[] args) {
        int example_no = 6;
        char example_ch = 'A';
        float example_deci = 5.44F;
        String hello = "Hello World"
        String example_null = null;
        
        System.out.println("The integer literal is " + example_no);
        System.out.println("The Char literal is " + example_ch);
        System.out.println("The Floating-Point literal is " + example_deci);
        System.out.println("The String literal is " + hello);
        
    }
}

Books to learn Java

Books to learn java in India:

1. Effective Java

Link: https://amzn.to/3yeDasr

 

2. Head First Java, a Brain-Friendly Guide

Link: https://amzn.to/3sJML9P

 

3.Java: The Complete Reference

Link: https://amzn.to/3sNnWK3

Books to learn java in USA:

1. Effective Java

Link: https://amzn.to/3DfWSIe

 

2. Java: The Complete Reference

Link: https://amzn.to/3sPT63l

 

3. Java in a Nutshell: A Desktop Quick Reference

Link: https://amzn.to/2WoydAC

Courses to learn Java

Free Java Courses:

  1. Java Tutorial for Beginners [2020]

 

  1. Learn Java 8 – Full Tutorial for Beginners

Conclusion

Java is a great programming language and is very important programming language for software development. In Java, literals are initialized values of the variables. Literals are of various types eg integer literals, string literal etc.


Spread this useful information with your friends if you liked.

Leave a Comment

Your email address will not be published. Required fields are marked *