
Literals in Java
Last updated on 21st Sep 2020, Artciles, Blog
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 type | Description | Data type of the literal |
Integer | Decimal 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-point | Decimal 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 |
Character | Values between single quotes (may contain Unicode characters) | char: ‘A’, ‘\u0041’ |
String | Values between double quotes (may contain Unicode characters) | String: “ABC”, “\u0041BC” |
Class | Any type name + .classIt refers to the object that represents the type itself | The 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!”;
Example:(Escape Sequence)
Escape sequence | Meaning |
---|---|
\n | New line |
\t | Tab |
\b | Backspace |
\r | Carriage return |
\f | Form Feed |
\\ | Backslash |
\’ | Single quotation mark |
\” | Double quotation mark |
\d | Octal |
\xd | Hexadecimal |
\ud | Unicode 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- The Most Popular Java Applications Used World-wide
- Resources To Help You Learn Java Programming
- Top Skills that Boosts Java Developer Salaries
- Java Interview Questions and Answers
- JAVA Tutorial
Related Articles
Popular Courses
- Python Online Training
11025 Learners
- C And C Plus Plus Online Training
12022 Learners
- C Sharp Online Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know