Java Data Types and Variables

Java is a strongly-typed language, which means that all variables must have a declared data type. Variables are used to store values in memory. In Java, you declare a variable by specifying its data type and name.

Primitive vs Non-Primitive

Java has two categories of data types: primitive types and reference types.

Primitive

Primitive types are built-in types that represent simple values, such as int, float, double, bool and char.

Non-Primitive or Reference

Reference types are more complex types that represent Objects, such as Strings, arrays and user-defined classes.

Initialization

Variables can be initialized with a value when they are declared, or they can be assigned a value later in the program.

int num1 = 10; // declare and initialize an integer variable
double num2; // declare a double variable
num2 = 3.14; // assign a value to the double variable
String name = "John"; // declare and initialize a string variable