Java Data Types
1. Primitive Data Types
Data Type | Size | Default Value | Description |
---|---|---|---|
byte | 1 byte | 0 | Stores whole numbers from -128 to 127. |
short | 2 bytes | 0 | Stores whole numbers from -32,768 to 32,767. |
int | 4 bytes | 0 | Stores whole numbers from -2,147,483,648 to 2,147,483,647. |
long | 8 bytes | 0L | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Add L or l suffix for long literals (e.g., 1234L ). |
float | 4 bytes | 0.0f | Stores fractional numbers. Precision: Up to 7 decimal digits. Add f or F suffix for float literals (e.g., 3.14f ). |
double | 8 bytes | 0.0d | Stores fractional numbers. Precision: Up to 15 decimal digits. Add d or D suffix for double literals (e.g., 3.14159d ). |
char | 2 bytes | '\u0000' | Stores a single 16-bit Unicode character. Example: 'A' or '\u0041' . |
boolean | 1 bit (not precisely defined) | false | Stores true or false. Size depends on JVM implementation (typically 1 bit or 1 byte). |
2. Non-Primitive Data Types
Type | Size | Default Value | Description |
---|---|---|---|
String | Depends on content | null | Stores a sequence of characters. Immutable (cannot be changed after creation). |
Arrays | Depends on length & type | null | Stores a collection of elements of the same type. |
Class | Depends on object fields | null | Represents user-defined types. |
Interface | N/A | null | Represents a contract that classes can implement. |
Key Points
- Wrapper Classes: Each primitive type has a corresponding wrapper class (
Byte
,Short
,Integer
,Long
,Float
,Double
,Character
,Boolean
) to use primitives as objects. - Default Values: Non-primitive data types default to
null
. - Ranges of Integer Types: Derived from
-2^(n-1)
to2^(n-1)-1
, wheren
is the number of bits. - Floating-Point Representation: Based on IEEE 754 standard.
- Type Conversion: Implicit conversions (widening) occur in compatible types (e.g.,
int
tolong
), but explicit casting is required for narrowing (e.g.,double
tofloat
).