πŸ’©

programierds

Back to course
Lessons 3 / 12

Variables and Data Types

Variables and Data Types

Variables

Variables are memory spaces where we temporarily store data. Imagine them as β€œcontainers” labeled with a name and a data type.

int age = 25;
age = 30; // We can change its value at any time

Primitive Data Types

Java has 8 built-in basic data types:

1. Integers (Numbers without decimals)

TypeSizeRange
byte8 bits-128 to 127
short16 bits-32,768 to 32,767
int32 bits~-2 billion to ~2 billion
long64 bitsHuge range (use β€˜L’ at the end: 100L)

2. Floating-point numbers (Numbers with decimals)

TypeSizeUse
float32 bitsSingle precision (use β€˜f’ at the end: 3.14f)
double64 bitsDouble precision (standard for decimals)

3. Characters

  • char: Stores a single Unicode character (use single quotes: 'a').

4. Booleans

  • boolean: Can only be true or false.

Summary table

TypeGroupSizeRange
booleanLogical1 bittrue or false
byteInteger8 bits-128 to 127
charCharacter16 bits’\u0000’ to β€˜\uffff’
intInteger32 bits-2,147,483,648 to 2,147,483,647
doubleReal64 bits~1.7e308