int age; String name = "Sam";
Here, we are defining a variable age of type Integer and name of type String.
You can optionally initialize a variable with a value in its declaration.
Variable names are:
- Case sensitive
- Can contain letters, digits,
- Cannot start with a digit
- Cannot be any of the reserved keywords
// Primative Data Types boolean booleanVar = true; byte byteVar = 12; short shortVar = 12; int intVar = 12; long longVar = 12L; float floatVar = 1.618F; double doubleVar = 1.618; char charVar = 'c'; // Non-Primative Data Types String stringVar = "Hello"; int arrayVar = new int;
Data Types in Java are divided into two groups:
Primitive Data Types:
These are the most basic data types, which are:
Non-Primitive Data Types:
These are types that are using Primitive Types to create new Types:
Array, Class object types, etc.
int status = 0; status = "success"; // Error
Java is a Type-Safe programming language, meaning that variables can only accept values of their type.
int x, y=7, z=12;
You can define multiple variables in one statement.
Here, we are creating variables x, y, and z of type integer, and initialize y to 7 and z to 12.
int age; age = 21;
Here, we are defining a variable age of type integer first and assign a value 21 to it.