Intern
What data types exist in Java?
sobes.tech AI
Answer from AI
In Java, there are two main data types:
-
Primitive Data Types: Store simple values. Their size and range are predefined by the JLS (Java Language Specification).
- Logical:
boolean(storestrueorfalse). - Integer:
byte(1 byte, from -128 to 127)short(2 bytes, from -32,768 to 32,767)int(4 bytes, from -2^31 to 2^31-1)long(8 bytes, from -2^63 to 2^63-1)char(2 bytes, stores a single Unicode character)
- Floating point:
float(4 bytes, single precision)double(8 bytes, double precision)
- Logical:
-
Reference Data Types: Store references (addresses) to objects in memory. Include predefined Java classes, arrays, and user-defined classes, interfaces, and enums.
- Classes (
class) - Interfaces (
interface) - Enums (
enum) - Arrays (
array)
- Classes (
Examples:
// Primitive types
int count = 10;
boolean isValid = true;
char initial = 'J';
double price = 99.99;
// Reference types
String name = "Java"; // String is a class
int[] numbers = {1, 2, 3}; // Array
Object obj = new Object(); // Object class
Differences:
| Attribute | Primitive Types | Reference Types |
|---|---|---|
| Storage | Actual value | Reference (address) to object in memory |
| Default value | Depends on type (0, false, '\u0000') | null |
| Size | Fixed | Depends on object and JVM platform |
| Memory handling | Placed on stack (usually) | Objects are placed on heap, references on stack |