Java Data Types
As discussed earlier, data type specifies the type of data which we are storing in a variable.
In Java, we can divide data types in two groups:
- Primitive data types
- Reference data types
Primitive Data Types
| Data Type | Size | Description |
|---|---|---|
| byte | 8 bits | Represents a signed 8-bit integer value. Range: -128 to 127. |
| short | 16 bits | Represents a signed 16-bit integer value. Range: -32,768 to 32,767. |
| int | 32 bits | Represents a signed 32-bit integer value. Range: -2^31 to 2^31-1. |
| long | 64 bits | Represents a signed 64-bit integer value. Range: -2^63 to 2^63-1. |
| float | 32 bits | Represents a single-precision floating-point number. It's used for decimal values. Example: 3.14159f. |
| double | 64 bits | Represents a double-precision floating-point number. It's used for decimal values. Example: 3.14159. |
| char | 16 bits | Represents a single Unicode character. Example: 'A', '1'. |
| boolean | 1 bit | Represents a true or false value. |
Reference Data Types
| Data Type | Description |
|---|---|
| class | Represents user-defined types. Classes define objects and their behavior in Java. |
| interface | Represents a blueprint for classes. Interfaces define a set of methods that classes must implement. |
| enum | Represents a special type used to define a set of constants. Enums are often used for defining a limited set of values. |
| array | Represents a collection of elements of the same data type. Arrays can be of primitive or reference types. |
We will not focus on the details of reference data types here.
String
Although it's not a primitive data type, String is widely used to represent text in Java. It's a class that provides methods for manipulating strings. We will discuss more about strings in strings section.
Variables with different Data Types
Scientific Numbers in Java
A floating point number can be used to store a scientific number with "e" corresponding to the power of 10.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
Sign in to track your progress and earn XP!
Exercise 1 of 2Easy
Which data type is used to store decimal numbers?
10 XP~2 min
Exercise 2 of 2Medium
What is the typical size of an int data type?
15 XP~3 min