Latest Code Tutorials

What are Literals in Java: The Complete Guide

Literals in Java are the fixed sequence of characters (digit, letters) in the form of constant values assigned to the variable. Literals can be numbers, letters, or anything represented in the form of a constant value.

Literals in Java

Literal in Java represents constant value to be stored in variables. Literals are syntactic representations of boolean, character, numeric, or string data. There are five types of literal:

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

Integer Literals in Java

There are four primitive data types present in integer literals they are byte, short, long, int, and we can represent these in 4 ways:

Decimal literals:

Any number from 0-9 is allowed here.

Ex: int a=100;

Octal literals:

Any number from 0-7 is allowed here, but the value should start from 0 only.

Ex: int b=035;

Hexa-decimal literals:

Here the digit from 0-9 and characters from a-f are allowed. The value can be a combination of characters and digit. We can use both uppercase and lower-case characters here; it is not case-sensitive. This value should start from 0X or 0x.

Ex: int c=0X238ce;

Binary literals:

It consists of only two-digit 0 and 1, but the value should start from 0b or 0B.

Ex: int d=0B1101;

See the following program.

public class Integer {
  public static void main(String args[]) {
    int a = 100; // decimal literals
    int b = 035; // octal literals
    int c = 0X238ce; // hexadecimal literals
    int d = 0B1101; // binary literals

See the following output.

Java Literals Example

Floating-point literals

Floating-point literals only contain decimal values with a fractional component. It consists of a double and floats data type.

Ex: double a=3.24343;

      Float b=45.34f;

See the following program.

public class Floating {
  public static void main(String[] args) {
    double a = 3.24343;
    float b = 45.34f;

See the following output.

Floating-point literals

By default, every floating-point literal is of double type, so we cannot assign directly to the float variable. But we can specify floating-point literal as float type by suffixed with f or F.

We can explicitly specify floating-point literal as double type by suffixed with d or D. Of course, and this convention is not required.

Character Literals

It is a 16-bit Unicode, enclosed in single quotes, whereas the string is enclosed in double-quotes.

Ex: Char ch=’a’;

      Char ch1=’#’;

      Char ch2=’4’;

For the char data types, we can define the literals in 4 ways:

Single quote

We can specify literal to a char data type as a single character within a single quote.

char ch = 'a';

Char literal as Integral literal

We can define the char literal as an integral literal representing the Unicode value of a character. Those integral literals can be defined either in the Decimal, Octal, and Hexadecimal forms.

But the final allowed range is 0 to 65535.

char ch = 062;

Unicode Representation

We can define the char literals in Unicode representation’ \uxxxx’. Here xxxx represents four hexadecimal numbers.

char ch = '\u0061';

Here /u0061 represent a.

Escape Sequence

Every escape character can be specified as char literals.

char ch = '\n';

See the following program.

public class Test {
  public static void main(String[] args) {
    char ch1 = 'a';
    char ch2 = '#';
    char ch3 = '4';

See the output.

Character Literals

String Literals

They are the sequence of characters enclosed in double-quotes, and it can be a sentence.

Any sequence of the characters within the double quotes is treated as String literals.

String str1=” how are you?”;
String str2=” I’m good thanks!”;

See the following program.

public class Test {
  public static void main(String[] args) {
    String str1 = "how are you?";
    String str2 = " I'm good thanks!";

See the output.

String Literals

String literals may not contain unescaped newline or linefeed characters. However, the Java compiler will evaluate compile-time expressions.

Boolean literals

Only two values can be represented in Boolean literals, i.e., true or false. These two values can be assigned to a variable. These true and false are case-sensitive in Java.

Ex: boolean flg1=” true”;

       boolean flg2=” false”;

See the program of Boolean literals.

public class Boolean {
  public static void main(String args[]) {
    boolean flg1 = true;
    boolean flg2 = false;

See the output.

Boolean literals

When performing concatenation operations, the values in brackets are concatenated first. Then the values are concatenated from left to right.

We should be careful when we are mixing character literals and integers in String concatenation operations, and this type of operation is known as Mixed Mode operation.

That’s it for this example.

Recommended Posts

Multithreading in Java

Object-Oriented Programming in Java

Inheritance in Java

Encapsulation in Java

Abstract Class in Java

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.