Archangel Macsika Sikademy Image
Archangel Macsika

Learn about Literals and Identifiers in Java Programming

Tutorial 7 of 9 | 10 minutes read
Learn about Literals and Identifiers in Java Programming - Sikademy

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:

  1. 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.

  2. Reserved words such as the names of datatype int or String cannot be used as a name for an identifier. See our tutorial on keywords to get the list of all reserved keywords in Java.

  3. Identifiers are case sensitive e.g greetingmessage and greetingMessage are different variables.

  4. 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.

  5. 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 LiteralMeaning
""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.

Save, compile, and execute the program.



 
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 NotationsMeaning
\nNew line
\tTab
"\"\""A string containing a double quote ("").
"This is another string"A string containing 22 characters.
\bBackspace
\rCarriage return
\fFormfeed
\\Backslash
\'Single quotation mark
\"Double quotation mark
\dOctal
\xdHexadecimal
\udUnicode 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

Save, compile, and execute the program.

A
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.

Save, compile, and execute the program.

true
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:

Save, compile, and execute the program.

100
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.

Save, compile, and execute the program.

5600000
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.

Save, compile, and execute the program.

3
15

Octal Literals

Octal literals are numeric numbers represented by the octal (base 8) numbering system. Octal literals begin with a 0 (zero) prefix.

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.

Save, compile, and execute the program.

23
-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.

Save, compile, and execute the program.

4
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 digitsDefault Value
float432+/- 3.4 * 1038 (3.4e−038 to 3.4e+038)6 to 70.0f
double864+/- 1.8 * 10308 (1.7e−308 to 1.7e+038)150.0d

Save, compile, and execute the program.

0.01
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.

Save, compile, and execute the program.

null

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.

Tutorial Exercise

Represent the following values in Java using the appropriate literals (variable and datatype), and print the results.

""

" / "

"\nIt's a" + "\nNew day!"

0.314159e1

2.34

3E19

.0003413

1.50d

.025f

1.333D

12F

i

.

gPP

P

;p

\u0022

\u003b

0x04

0x0A


Enjoy this Tutorial? Please don't forget to share.