Skip to content

Java Variables and Data Types

Variables are the fundamental building blocks of any program. In Java, a variable is a container that holds data which can be changed during the execution of the program.

Declaring and Initializing Variables

In Java, every variable must be declared with a specific data type. This tells the compiler how much memory to allocate and what kind of data can be stored.

Syntax:

dataType variableName = value;

Example:

int age = 25;
String name = "Saurabh";
double salary = 50000.50;

Data Types in Java

Java is a statically-typed language, meaning all variables must be declared before they can be used. Data types are divided into two groups:

1. Primitive Data Types

These are predefined by the language and named by a reserved keyword. There are 8 primitive data types:

TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers for very large ranges (suffix with L)
float4 bytesStores fractional numbers (suffix with f)
double8 bytesStores fractional numbers (more precision than float)
boolean1 bitStores true or false
char2 bytesStores a single character/ASCII value (enclosed in ' ')

2. Non-Primitive (Reference) Data Types

These are created by the programmer and are not predefined by Java (except for String). Examples include:

  • Strings: A sequence of characters. String greeting = "Hello";
  • Arrays: Collections of data of the same type.
  • Classes & Interfaces.

Type Casting

Type casting is when you assign a value of one primitive data type to another type.

Widening Casting (Implicit)

Converting a smaller type to a larger type size. This happens automatically. byte -> short -> char -> int -> long -> float -> double

int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

Narrowing Casting (Explicit)

Converting a larger type to a smaller size type. This must be done manually. double -> float -> long -> int -> char -> short -> byte

double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int

Java Operators

Operators are used to perform operations on variables and values.

1. Arithmetic Operators

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y (Returns division remainder)
++Increment++x
--Decrement--x

2. Relational (Comparison) Operators

Used to compare two values, returning a boolean.

  • == (Equal to)
  • != (Not equal)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal)
  • <= (Less than or equal)

3. Logical Operators

Used to determine the logic between variables or values.

  • && (Logical and)
  • || (Logical or)
  • ! (Logical not)

Next Steps

Now that you understand variables and data types, the next topic will cover Control Flow: If-Else, Switch, and Loops.