Literals in Java: String, Boolean, Char, Int and Float Literals

Java Literals

Java literals are fixed or constant values in a program's source code. You can use any primitive type value as a literal e.g. boolean int, char and float. However, string is not a primitive type in Java yet we can use strings as literals. Later in this article string literals are discussed.

Java Integer Literals

Integer literals in Java can be used in three flavors in a Java program those are decimal, octal (base 8), and hexadecimal (base 16).

Octal literals are specified in Java program by a leading zero e.g., 07 or 06 are valid octal values, while 09 or 08 are not because octal range lies between 0-7. If you try 08 as an octal literal for an experiment it will result into the error "The literal 08 of type int is out of range".

A hexadecimal literal starts with a leading zero and x (0x) or zero and X (0X). For example, 0xff or 0XFF are valid hexadecimal literals.

Decimal integer literals neither start with a leading zero nor a 0x or 0X. Following piece of code shows all three variants of integer literals.

int octLit = 0400; //octal equivalent of decimal 256
int hexLit = 0x100; //hexadecimal equivalent of decimal 256
int decLit = 256; // decimal 256

Integer literals create an int value, which in Java is a 32-bit integer value. Since Java is strongly typed, you might be wondering how it is possible to assign an integer literal to one of Java's other integer types, such as byte or long, without causing a type mismatch error. Fortunately, such situations are easily handled. When a literal value is assigned to a byte or short variable, no error is generated if the literal value is within the range of the target type. Also, an integer literal can always be assigned to a long variable. However, to specify a long literal, you will need to explicitly tell the compiler that the literal value is of type long. You do this by appending an upper or lower-case letter L to the literal. For example, 0x7ffffffffffffffL or 9223372036854775807L is the largest long.

Java Floating-point Literals

Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant. You can also explicitly specify a double literal by appending a D or d. For example,

float f = 89.0; // Type mismatch: cannot convert from double to float
float ff = 89.0f; //OK
 
double dou = 89.0D; //OK
double doub = 89.0d; //OK
double doubl = 89.0; //OK, by default floating point literal is double 

Java Boolean Literals

Boolean literals have only two possible values those are true and false.

boolean boolFalse = false;
boolean boolTrue = true;

Words true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

Java Character Literals

Character literals are enclosed in single quotes when they are used for example, 'a' or 'A'. Whereas string literals are enclosed in double quotes for example, "Coffea arabica".

char charLit = 'a';
String strLit = "String Literal";

Java String Literals

A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences. A string literal is always of type String and a reference to an instance of class String. A string literal always refers to the same instance of class String. This is because string literals are interned so as to share unique instances, using the method String.intern. It is a compile-time error for a line terminator to appear after the opening " and before the closing matching ". A long string literal can always be broken up into shorter pieces and written as an expression using the string concatenation operator +.

Along with 8 built-in primitive data types; Java provides a rich set of readymade classes for general data structures and problems.

Last Word

In this tutorial we discussed Java literals (integer, floating-point, boolean, character and string literals). Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.