Data Types in Java
Java data types are divided into Primitive Data Types and Non-Primitive Data Types.
1. Primitive Data Types
Primitive data types are predefined by the language and stored directly in memory. They are
lightweight and simple.
int is commonly used for integers unless memory optimization is required.
Use float for decimal numbers when memory is a concern, but double is more precise.
char holds a single character enclosed in single quotes (e.g., 'A').
boolean is used for true/false values in conditional statements.
2. Non-Primitive Data Types
Non-primitive (or reference) data types are created by the programmer and include objects,
arrays, and strings.
String: Represents a sequence of characters.
Ex: String name = "Hello, World!";
Array: Used to store multiple values of the same data type.
Ex: int[] numbers = {1, 2, 3, 4, 5};
Classes and Objects: Defined by the programmer to model real-world entities
class Student {
String name;
int age;
}