Learn about Literals and Identifiers in Java Programming

In this tutorial, we will talk about the meaning of identifiers and literals in Java. Both terms are usually confused by programmers especially the newbies in programming.
Java Identifiers
All Java components such as classes, variables, and methods must be identified with unique names called identifiers. In this tutorial, we will be facing variables.
Rules for Writing Good Identifiers (variables) in Java
When writing names of identifiers, there are some rules to keep in mind:
The name of an identifier should always start with a letter (A to Z or a to z), currency character ($), or an underscore (_). After the first character, any combination of characters (letters, digits, underscores, and dollar signs) can be used.
Reserved words such as the names of datatype
int
orString
cannot be used as a name for an identifier. See our tutorial on keywords to get the list of all reserved keywords in Java.Identifiers are case sensitive e.g greetingmessage and greetingMessage are different variables.
Identifiers can be short names like _, $, x, dob, and pi or more descriptive names like dateOfBirth, totalSum, and accountBalance, and can contain letters, digits, underscores, and dollar signs.
Identifiers cannot contain white space.
Examples of Acceptable and Unacceptable Identifiers for Java Variables
Examples of Acceptable Java Variables
Some examples of acceptable identifiers for Java variables are x, age, dob, $salary, _value, __value, __1_value, a$___, dateOfBirth, name1 etc.
Please take note that even though the examples of identifiers above are permitted, we are merely displaying them for illustration purposes. You should make it a goal to write clear, readable, and well-formatted codes when working on real applications whether small-scale or large-scale. As a result, we recommend you write reasonable names for identifiers to enhance code readability. For instance, dob should be written in full as dateOfBirth.
Examples of Unacceptable Java Variables
Some examples of unacceptable identifiers for Java variables are 123abc, -salary., first name, @dateOfBirth, first.name, etc.
Java Literals
Literals in Java are a sequence of characters that represent the values to be stored in identifiers like variables. Literals are values in everyday human-readable form and can be any number (e.g 90), text (e.g "Hello World"), or other information that represents a value. Whenever we initialize a data type variable, it is assigned a literal value.
Literals are specified in this syntax:
(data type) (variableName) (assignment operator =) (literal) (semicolon ;)
Some examples of literals are given below:
int a = 58;
char ch = 'Z';
String s = "Quick brown fox";
Types of Literals in Java Programming Language
String literals
Character literals
Boolean literals
Numeric literals
Floating literals
Null literal
String Literals in Java
A group of characters forming words, sentences, or phrases is represented as String literals in Java and they are usually enclosed within double quotes ("").
In Java programming, there are methods for manipulating Strings such as combining words and sentences, counting the number of characters, and comparing the values of two strings. You will learn more about Strings in upcoming tutorials in this series.
String Literal | Meaning |
---|---|
"" | An empty string. |
" " | A string containing a single white-space. It counts as a character. |
"\"\"" | A string containing a double quote (""). |
"This is another string" | A string containing 22 characters (Note that spaces also count as a character). |
"This is" + " one sentence, but" + "with three strings" | This is a full sentence formed by combining three different strings |
"The name's, \"Archangel Macsika\"" | Strings in double in between another double quote. |
- Java Code
Examples of string literals are specified in the code block below:
public class TutorialClass {
public static void main(String[] args) {
String emptyString = "";
String spaceInString = " ";
String doubleQuotes = "\"\"";
String stringWith22Characters = "This is another string";
String combinedStrings = "This is" + " one sentence, but" + "with three strings";
String stringWithDoubleQuotes = "The name's, \"Archangel Macsika\".";
System.out.println(stringWith22Characters);
System.out.println(emptyString);
System.out.println(combinedStrings);
System.out.println(spaceInString);
System.out.println(doubleQuotes);
System.out.println(stringWithDoubleQuotes);
}
}
Save, compile, and execute the program.
- Output
This is another string
This is one sentence, butwith three strings
""
The name's, "Archangel Macsika".
Character Literals in Java
A character literal can be specified as a single character within a pair of single quote characters such as 'c', '5', and '$'. The available set of characters for use is determined using the ASCII character set. The ASCII character set is made up of 128 characters including letters, numerals, punctuations, etc.
There are few character literals which cannot be accessed using a computer keyboard. The table below shows the codes that can represent these special characters. The letter d such as in the octal, hex, etc represents a number.
Escape Notations | Meaning |
---|---|
\n | New line |
\t | Tab |
"\"\"" | A string containing a double quote (""). |
"This is another string" | A string containing 22 characters. |
\b | Backspace |
\r | Carriage return |
\f | Formfeed |
\\ | Backslash |
\' | Single quotation mark |
\" | Double quotation mark |
\d | Octal |
\xd | Hexadecimal |
\ud | Unicode character |
It is also possible to represent a single quote, backslash, or any non-printable character as a character literal by making use of an escape notation. An escape notation makes use of special syntax to specify a character. The syntax begins with a single backslash character.
The table below shows how character literals make use of escape notation to represent both printable and non-printable characters.
'u0041' | Capital letter (A) |
'\u0030' | Digit (0) |
'\u0022' | Double quote (") |
'\u003b' | Semicolon (;) |
'\u0020' | Space |
'\u0009' | Horizontal Tab |
- Java Code
public class TutorialClass {
public static void main(String[] args) {
char a = '\u0041';
char zero = '\u0030';
char doubleQuote = '\u0022';
char semiColon = '\u003b';
char space = '\u0020';
char horizontalTab = '\u0009';
System.out.println(a);
System.out.println(zero);
System.out.println(space);
System.out.println(doubleQuote);
System.out.println("" + horizontalTab + semiColon); //The result shows a tab of semicolon. The ("") at the beginning aids in printing an nothing else.
}
}
Save, compile, and execute the program.
- Output
0
"
;
Boolean Literals
The values true
and false
are also treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values.
Unlike in C Programming Language, we cannot assume that the value of "1" is equivalent to true
, and "0" is equivalent to false
in Java. We have to use the values true
and false
to represent a Boolean value.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
boolean isProgrammingFun = true;
boolean isFightingFun = false;
System.out.println(isProgrammingFun);
System.out.println(isFightingFun);
}
}
Save, compile, and execute the program.
- Output
false
Note: The literal true
and false
should not be written between double quotes if the data type is a boolean
. The Java compiler will treat it as an error if placed between quotation marks except the data type is String
. Take the example below:
boolean isFightingFun = "false";
The code above will give an error. But,
String isFightingFun = "false";
will print "false".
Numeric Literals in Java
Numeric data types consist of the following primitive data types: int
, long
, byte
, and short
.
These datatypes can be expressed in binary (base 2), octal(base 8), decimal(base 10), or hexadecimal(base 16) number systems as well. The prefix "0" is used to represent the octal base and the prefix "0x" represents a hexadecimal base when using these number systems for literals.
For example, the numbers below will result in the same value "100" when printed in Java:
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
System.out.println(decimal);
System.out.println(octal);
System.out.println(hexa);
}
}
Save, compile, and execute the program.
- Output
100
100
From Java version 7 and upwards, you can insert an underscore character(s) in a numeric literal but it's mainly for readability.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int a = 5600000; // no underscores
int b = 5_600_000; // with underscores
System.out.println(a);
System.out.println(b);
}
}
Save, compile, and execute the program.
- Output
5600000
Binary Literals
Binary literals are numeric numbers represented by the binary (base 2) numbering system. Binary literals begin with a 0B
or 0b
prefix.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int a = 0B00000011;
int b = 0b00001111;
System.out.println(a);
System.out.println(b);
}
}
Save, compile, and execute the program.
- Output
15
Octal Literals
Octal literals are numeric numbers represented by the octal (base 8) numbering system. Octal literals begin with a 0
(zero) prefix.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int seven = 07;
int eight = 010;
int nine = 011;
System.out.println(seven);
System.out.println(eight);
System.out.println(nine);
}
}
Save, compile, and execute the program.
Decimal Literals
Decimal literals are whole numbers of the base 10 numbering system. It is important not to confuse decimal literals with floating-point literals. Contrary to their name, decimal literals cannot contain a decimal point.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int a = 23;
int b = -156;
System.out.println(a);
System.out.println(b);
}
}
Save, compile, and execute the program.
- Output
-156
Hexadecimal Literals
Hexadecimal literals are numeric numbers represented by the hexadecimal (base 16) numbering system. Hexadecimal literals begin with a 0X
or 0x
prefix.
The numbers 0 to 9 and the letters A to F (uppercase or lowercase) are used to represent hexadecimal numbers i.e 0123456789ABCDEF.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
int four = 0X04;
int ten = 0x0A;
int twenty = 0x14;
System.out.println(four);
System.out.println(ten);
System.out.println(twenty);
}
}
Save, compile, and execute the program.
- Output
10
20
Floating-point literals
Floating-point numbers are like decimal numbers in mathematics, for example, 4.13179, -0.000001. There are two kinds of floating-point numbers in Java programming language: float
and double
. The most used generally is the double
floating-point literal.
A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E or e), and as an integer. We also add a suffix to the floating-point literal as "D" or "d" for double
floating-point literal and "F" or "f" for float
floating-point literal.
Difference Between Float and Double in Java
Type(Name) | Size (Bytes) | Size (Bits) | Range (Approximate) | No. of decimal digits | Default Value |
---|---|---|---|---|---|
float | 4 | 32 | +/- 3.4 * 1038 (3.4e−038 to 3.4e+038) | 6 to 7 | 0.0f |
double | 8 | 64 | +/- 1.8 * 10308 (1.7e−308 to 1.7e+038) | 15 | 0.0d |
- Java Code
public class TutorialClass {
public static void main(String[] args) {
float floatVar = .01f;
double doubleVar = 7d;
System.out.println(floatVar);
System.out.println(doubleVar);
}
}
Save, compile, and execute the program.
- Output
7.0
Null Literals
The final literal that we can use in Java programming is a Null literal. We represent the Null literal in the source code as null
. The Null literal is used to reduce the number of references to an object. As a result, they are usually assigned to object reference variables.
The data type of a Null literal is an Object
while the value of a Null literal is always null
.
- Java Code
public class TutorialClass {
public static void main(String[] args) {
Object moneyInTheBank = null;
System.out.println(moneyInTheBank);
}
}
Save, compile, and execute the program.
- Output
Wrap Off
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below the tutorial exercise.