Java Variables

What Are Variables?

In Java a variable serves as a named storage location in memory where information can be kept and manipulated. It acts as a placeholder for a value that can be changed during program execution. Variables play a role in programming by enabling efficient data storage and retrieval.

Each variable is linked to a data type that dictates its size, structure, range of values and allowable operations. In Java data types fall into two categories; primitive types and reference types.

  • Primitive types encompass integers (byte, int long) floating point numbers (float, double) characters (char) and booleans (boolean). These types have fixed sizes. Represent fundamental data values.
  • Reference types are constructed from classes. Facilitate the creation of intricate objects. They comprise strings, arrays and user defined classes. Reference types have sizes and can store collections of data.

The selection of a data type for a hinges on the nature of the data it will contain and the operations to be carried out on it. Opting for the data type ensures efficient memory usage and guards, against unexpected outcomes.

Types of Variables in Java

In Java, variables play a crucial role in storing and manipulating data within a program. Understanding the different types of variables is essential for efficient data management. Java has three main types of variables: primitive variables, reference variables, and class variables.

Primitive Data Types

Java has eight primitive data types, which are the most basic building blocks for storing data. Each type has a specific size and role:

  • Byte: 1 byte, ranges from -128 to 127.
  • Short: 2 bytes, ranges from -32,768 to 32,767.
  • Int: 4 bytes, ranges from -2,147,483,648 to 2,147,483,647.
  • Long: 8 bytes, ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • Float: 4 bytes, stores decimal numbers with a range of approximately ±3.40282347 x 10^38.
  • Double: 8 bytes, provides higher precision with a range of approximately ±1.79769313486231570 x 10^308.
  • Char: 2 bytes, holds a single character using Unicode encoding.
  • Boolean: Size is JVM-dependent, represents true or false.

Integers

Integer literals in Java represent whole numbers without fractional or decimal values. Java supports several types of integer literals:

  • Binary: Prefixed with 0b or 0B, e.g., int binaryNumber = 0b1010;.
  • Decimal: No prefix needed, e.g., int decimalNumber = 15;.
  • Octal: Prefixed with 0, e.g., int octalNumber = 034;.
  • Hexadecimal: Prefixed with 0x or 0X, e.g., int hexadecimalNumber = 0xFF;.

You can also convert strings to integers using Integer.valueOf().

Floating-Point Numbers

Floating-point numbers represent real numbers with fractional and exponential forms. They are used for high-precision calculations, such as scientific or financial data. Java has two types of floating-point variables:

  • Float: Stores single-precision floating-point numbers, e.g., float pi = 3.14;.
  • Double: Stores double-precision floating-point numbers, e.g., double myNumber = 4.56e-7;.

Characters

In Java, characters are represented by the char keyword. Character literals are single characters enclosed in single quotes, such as 'a' or '5'. Java also supports escape sequences like '\n' for a new line and '\t' for a tab. Characters can also be represented by their Unicode values.

Booleans

Booleans represent the values true or false and are used to evaluate conditions and control the flow of a program. Booleans are essential in branching statements and loops, making them a crucial aspect of programming logic.

Reference Data Types

Reference data types in Java store references or memory addresses to objects. There are two main types:

  • Object references: Used to refer to objects created from a class. The reference variable holds the memory address of the object.
  • Arrays: Store multiple values of the same data type and are accessed using an index.

Reference data types have default values of null and methods associated with them for performing operations on the referenced objects.

Variable Declaration in Java

Variable declaration in Java involves specifying the data type and assigning a value. The syntax follows a specific order:

int num;
num = 10;

Or in one line:

int num = 10;

Naming Conventions

Adhering to naming conventions is vital for code readability and maintainability. Key points include:

  • Case sensitivity: Java is case-sensitive, so count and Count are different.
  • Starting with a letter: Variable names must begin with a letter, underscore, or dollar sign.
  • Avoiding reserved words: Do not use Java's reserved keywords as variable names.
  • CamelCase: For multiword names, use CamelCase, e.g., numberOfStudents.

Following these conventions helps in creating clear and maintainable code.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate