Skip to main content

Variables and Data Types

Data Types​

Java is a strongly-typed language, meaning every variable must be declared with a data type.

Primitive Data Types​

  • int: Integer type.
  • double: Double-precision floating point.
  • char: Character type.
  • boolean: Boolean type (true or false).

Example:

int number = 10;
double price = 9.99;
char letter = 'A';
boolean isJavaFun = true;

Reference Data Types​

Reference types refer to objects and include arrays, classes, interfaces, etc.

Example:

String message = "Hello, World!";
int[] numbers = {1, 2, 3, 4, 5};

Variables​

Variables store data values. They must be declared before use.

Variable Declaration​

int age;
String name;

Variable Initialization​

age = 25;
name = "Alice";

Combined Declaration and Initialization​

int age = 25;
String name = "Alice";