Home Java Tutorial Java Variables and Literals

Java Variables and Literals

by Anup Maurya
18 minutes read

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.

Java Variables

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

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>;

For example:

int age = 25;
double salary = 50000.00;
char gender = 'M';
boolean isEmployed = true;

In the above example, we have declared four primitive variables: age, salary, gender, and isEmployed. We have assigned integer, double, character, and boolean values to these variables, respectively.

Reference Variables

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>();

For example:

String name = new String("John");
Date today = new Date();

In the above example, we have declared two reference variables: name and 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.

Java Literals

A literal is a value that is written directly into the code. Java supports several types of literals, including:

Numeric Literals

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 float or double data types to represent floating-point literals.
  • Scientific notation: represented by a number followed by the letter e or E and 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

String literals are used to represent a sequence of characters. A string literal is enclosed in double quotes, such as “Hello, world!”.

Boolean Literals

Boolean literals are used to represent true or false values. The two boolean literals in Java are true and false.

Conclusion

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.

related posts

Leave a Comment

Enable Notifications OK No thanks