Java is a widely-used programming language that is known for its robustness, flexibility, and object-oriented approach. Java variables and literals are two essential concepts in Java programming. In this tutorial, we will cover Java variables and literals in detail.
A variable is a named memory location that can store data. In Java, variables are used to hold values that can be accessed and modified throughout the program. Java variables can be classified into two categories: primitive variables and reference variables.
Primitive variables are variables that hold primitive data types such as int, double, float, char, boolean, byte, short, and long. These variables hold the actual value of the data type, such as a number or a character.
To declare a primitive variable, we use the following syntax:
<datatype> <variable_name> = <value>;
int age = 25; double salary = 50000.00; char gender = 'M'; boolean isEmployed = true;
In the above example, we have declared four primitive variables:
isEmployed. We have assigned integer, double, character, and boolean values to these variables, respectively.
Reference variables are variables that hold the memory address of an object. In Java, all non-primitive data types are objects, and we use reference variables to hold their memory addresses.
To declare a reference variable, we use the following syntax:
<datatype> <variable_name> = new <class_name>();
String name = new String("John"); Date today = new Date();
In the above example, we have declared two reference variables:
today. We have assigned the memory addresses of two objects to these variables. The
name variable holds the memory address of a
String object that contains the name “John”. The
today variable holds the memory address of a
Date object that represents the current date and time.
A literal is a value that is written directly into the code. Java supports several types of literals, including:
Numeric literals are used to represent numbers. Java supports four types of numeric literals:
- Integer literals: represented by an integer value, such as 10 or -5. We can use decimal, octal, or hexadecimal formats to represent integer literals.
- Floating-point literals: represented by a decimal value with a fractional part, such as 3.14 or -0.5. We can use the
doubledata types to represent floating-point literals.
- Scientific notation: represented by a number followed by the letter
Eand a power of 10, such as 1.23e-4 or 2.5E6.
- Character literals: represented by a single character enclosed in single quotes, such as ‘A’ or ‘\n’.
String literals are used to represent a sequence of characters. A string literal is enclosed in double quotes, such as “Hello, world!”.
Boolean literals are used to represent true or false values. The two boolean literals in Java are
In this tutorial, we explored Java variables and literals. Variables are named memory locations that can store values, and literals are values that are written directly into the code. Java supports two types of variables: primitive and reference variables. Java also supports several types of literals, including numeric literals, string literals, and boolean literals. By understanding these concepts, you can write more efficient and effective Java programs.