In Java, data types define what kind of data a variable can hold. This is crucial because Java is a statically-typed language โ the compiler needs to know the type of every variable before it runs.
int age = 25;
โ int
tells Java that age
stores integers.let age = 25; // No type specified
Java enforces type safety, which reduces runtime errors.
These are predefined by Java, lightweight, and fast. Java has 8 primitive data types:
Data Type | Size | Example Values | Use For |
---|---|---|---|
byte | 1 byte | -128 to 127 | Small numbers, memory-saving |
short | 2 bytes | -32,768 to 32,767 | Medium numbers |
int | 4 bytes | ~ -2B to 2B | Default for whole numbers |
long | 8 bytes | Huge numbers | Big integers |
float | 4 bytes | 3.14f | Decimal numbers (less precise) |
double | 8 bytes | 3.14159265359 | Decimal numbers (more precise) |
char | 2 bytes | 'A', '%' | Single characters |
boolean | 1 bit | true, false | Logical values (conditions) |
โก๏ธ Important: All primitive data types start with lowercase letters in Java.
These are objects, created by the programmer or Java itself. Examples:
Weโll focus on primitive types first, as in the lecture.
Size: 8 bits (1 byte)
Range: -128 to 127 (256 total values)
Use Case: For memory-efficient storage in large
arrays.
byte myByte = 8;
System.out.println(myByte); // prints 8
Size: 2 bytes (16 bits)
Range: -32,768 to 32,767
Less common in real-world Java โ int is preferred.
Size: 4 bytes
Range: ~ -2 billion to +2 billion
Most used data type for whole numbers
int age = 25;
System.out.println(age);
Size: 8 bytes
Range: Way bigger than int
Use Case: Timestamps, large IDs
long population = 8000000000L;
Size: 4 bytes
Precision: Up to 7 decimal places
Decimal numbers (use f
suffix)
float pencilPrice = 10.5f;
Size: 8 bytes
Precision: Up to 15 decimal places
Default for decimal values
double pi = 3.14159265359;
Size: 2 bytes
Stores: Single character
Use single quotes 'A'
, not double quotes
char grade = 'A';
Size: 1 bit
Values: true or false
Use Case: Conditional logic
boolean isJavaFun = true;
Java allocates memory based on data type.
A byte = 8 bits โ Can store 2โธ = 256 values
This explains:
Formula:
Range = โ(2โฟโปยน) to (2โฟโปยน โ 1), where n is the number of bits
Task | Data Type |
---|---|
User's age | int |
Temperature in decimal | float |
Amount in a bank account | double |
Gender as a character | char |
Is student passed (yes/no)? | boolean |
Unique product ID (very big) | long |
f
, long with L
if (isActive) {...}
Thing You Might Forget | Memory Trick or Reminder |
---|---|
float needs an f | "Float needs an Flag" |
char uses single quotes 'A' | "Char = Character = One letter, One quote" |
boolean values are lowercase | "true/false โ all lowercase, never True" |
Size of each data type | Use mnemonic: "B S I L F D C B" |
Memory size importance | Think: โLess memory = less rangeโ |
int is default for whole numbers | Unless told otherwise, Java picks int |
Java is statically typed because every variable must be declared with a data type at compile time. This allows the Java compiler to:
int age = 25; // age must hold an int
age = "twenty"; // Error! Can't assign String to int
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Small integers |
short | 2 bytes | Medium integers |
int | 4 bytes | Default for whole numbers |
long | 8 bytes | Large integers |
float | 4 bytes | Decimal numbers (less precision) |
double | 8 bytes | Decimal numbers (high precision) |
char | 2 bytes | Single Unicode character |
boolean | 1 bit | true/false only |
float
uses 4 bytes, double
uses 8 bytesfloat
is less precise (up to 7 digits), double
is more precise (up to 15 digits)
float price = 10.5f;
double pi = 3.14159265359;
It causes a compilation error or unexpected behavior.
byte b = 130; // Error: 130 is outside byte range (-128 to 127)
Java won't allow overflow silently (unlike C in some cases).
byte
and short
are useful in large arrays or embedded systems
long
is used when int
isnโt big enough