Literals in Java

Literals in Java

Last updated on 21st Sep 2020, Artciles, Blog

About author

Kiran (Sr Project Manager - Java )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 13567 Ratings 493

Java literals are the fixed values assigned to a variable. A literal doesn’t require any computation and it is possible to assign a literal to a variable of a primitive type and also to a String even though String is not a primitive type in Java.

Types of Literals

Literals in Java can be classified into four types–

  • Integer Literals
  • Floating-point Literals
  • Character and String Literals
  • Boolean literals
Subscribe For Free Demo

Error: Contact form not found.

Literal typeDescriptionData type of the literal
IntegerDecimal values with no decimal pointDecimal values ended in: l, LHexadecimal values (0x prefix)Binary values (0b prefix)long if it ends with L or l: 123Lint otherwise: 123, 0x1A, 0b110
Floating-pointDecimal values with a decimal point or using scientific notationDecimal values ended in: f, F, d, D float if it ends with F or f: 1.3f, 2e3fdouble if it ends with D or d: 1.3d, 2e3ddouble otherwise: 1.3, 2e3
CharacterValues between single quotes (may contain Unicode characters)char: ‘A’, ‘\u0041’
StringValues between double quotes (may contain Unicode characters)String: “ABC”, “\u0041BC”
ClassAny type name + .classIt refers to the object that represents the type itselfThe object’s class, e.g.: String.class

Integer Literals in Java

  • Literal assigned to a type byte, short, int or long is called an integer literal in Java. Make sure when you assign a literal value to a byte or short it is within the range of that particular type.
  • An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lowercase letter l is hard to distinguish from the digit 1.
  • You will generally use a base 10 number i.e. decimal as an integer literal. But integer literals can be expressed by Binary (base two) and hexadecimal (base 16) number system also. 

Integer literals can be expressed by these number systems:

  • Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day.
  • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F.
  • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later).

Example

  • int dec=56;
  • int octal=0164;
  • int hex=0x56;

Floating-point Literals in Java

  • Literal assigned to a type float or double is called floating-point literal in Java. A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double in that case it can end with the letter D or d but that is optional.
  • Floating-point literal can also be expressed using E or e (for scientific notation).

Example

  • double d = 153.6;
  • double temp = 1.534e2; //equals to 153.6
  • float num = 23.4f;

Character and String Literals in Java

  • Literals of types char and String may contain any Unicode (UTF-16) characters.
  • Always use ‘single quotes’ for char literals and “double quotes” for String literals.
  • The Java programming language also supports a few special escape sequences for char and String literals: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \” (double quote), \’ (single quote), and \\ (backslash).

Example:(char Literals)

  • char c=’a’;

    char ch=’$’;

Example:(string Literals)

  • String s = “hi”;

    String s1 = “Hello!”;

Java Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Example:(Escape Sequence)

Escape sequenceMeaning
\nNew line
\tTab
\bBackspace
\rCarriage return
\fForm Feed
\\Backslash
\’Single quotation mark
\”Double quotation mark
\dOctal
\xdHexadecimal
\udUnicode character

Boolean literal in Java

Boolean literal can have only one of two values true and false. A Boolean literal can only be assigned to a variable of type boolean. It can also be used with conditions which evaluate to a boolean value.

Example:

  • boolean flag = true;

    boolean set = false;

Are you looking training with Right Jobs?

Contact Us

Popular Courses