Data Type – Below is the example of usage of data types in Java
import java.util.ArrayList;
public class Program2 {
public static void main(String[] args) {
int number = 10; // Integer (whole number)
float floatNum = 6.99f; // Floating point number
char letter = 'F'; // Character
boolean flag = false; // Boolean
String str = "Hello"; // String
}
}
Explanation –
This program demonstrates basic data types in Java:
int
– Stores whole numbers (e.g.,10
)float
– Stores decimal numbers (e.g.,6.99f
).f
is required to denote float.char
– Stores a single character (e.g.,'F'
)boolean
– Stores true or false values (e.g.,false
)String
– Stores a sequence of characters (e.g.,"Hello"
)
Below is the various data type size in java –
Data Type | Size |
---|---|
int | 4 bytes |
long | 8 bytes |
float | 4 bytes |
double | 8 bytes |
boolean | 1 bit |
char | 2 bytes |