Java Data Types
Java could be a statically-typed programming language, meaning each variable must have a declared type. Java data types are classified into Primitive Data Types and Non-Primitive (Reference) Data Types.
Data types
- Data type is used in a declaring a variable.
- Data type will decide which data need to be stored inside the variable.
- We can say data type will assign restriction to the variable.
- Data type will decide capacity or memory size of an variable.
We have a following two types of java data types

- Primitive data type
- Nonprimate data type
1. Primitive Data Types
These are the most basic data types, predefined by Java, and are directly stored in memory.
a) Numeric Data Types
Used for storing numbers.
Type | Size | Default Value | Description | Example Values |
---|---|---|---|---|
byte | 1 byte | 0 | Stores small integers from -128 to 127. | byte a = 100; |
short | 2 bytes | 0 | Stores integers from -32,768 to 32,767. | short b = 20000; |
int | 4 bytes | 0 | Stores integers from -2,147,483,648 to 2,147,483,647. | int c = 500000; |
long | 8 bytes | 0L | Stores larger integers from -2^63 to 2^63-1. | long d = 10000000000L; |
float | 4 bytes | 0.0f | Stores fractional numbers with 6-7 decimal digits of precision. | float e = 5.75f; |
double | 8 bytes | 0.0d | Stores fractional numbers with up to 15 decimal digits of precision. | double f = 19.99; |
b) Character Data Type
Type | Size | Default Value | Description | Example Values |
---|---|---|---|---|
char | 2 bytes | ‘\u0000’ | Stores a single character (uses Unicode). | char g = 'A'; |
c) Boolean Data Type
Type | Size | Default Value | Description | Example Values |
---|---|---|---|---|
boolean | 1 bit | false | Stores a value of either true or false . | boolean h = true |
2. Non-Primitive Data Types
These include objects and arrays. Unlike primitives, non-primitive data types are references to objects in memory.
a) Strings
A String
is a sequence of characters.
- Example:
b) Arrays
Used to store multiple values of the same data type.
- Example:
c) Classes and Objects
Used to define user-defined data types.
- Example:
Key Differences Between Primitive and Non-Primitive Data Types
Feature | Primitive Data Types | Non-Primitive Data Types |
---|---|---|
Definition | Basic, predefined by Java. | User-defined or built-in (e.g., String). |
Storage | Stores actual values. | Stores references to objects. |
Default Values | Have predefined default values. | Null for all non-primitive types. |
Examples | int , char , boolean . | String , Array , Class . |
Choosing the Right Data Type
- Performance: Use
int
orlong
for numeric calculations andfloat
/double
for decimals. - Storage Constraints: Use
byte
orshort
for memory-critical applications. - Use Case: Use non-primitive types for more complex objects like lists or user-defined data types.
Java Data Types Example
Here’s a Java program demonstrating different data types:
public class DataTypesExample {
public static void main(String[] args) {
// Primitive Data Types
byte byteValue = 127; // Byte range: -128 to 127
short shortValue = 32000; // Short range: -32,768 to 32,767
int intValue = 100000; // Int range: -2^31 to 2^31-1
long longValue = 10000000000L; // Long range: -2^63 to 2^63-1
float floatValue = 5.75f; // Float for fractional numbers
double doubleValue = 19.99; // Double for high precision
char charValue = ‘J’; // Single character
boolean booleanValue = true; // True/False
// Non-Primitive Data Types
String stringValue = “Hello, Java!”; // Sequence of characters
int[] arrayValue = {1, 2, 3, 4, 5}; // Array of integers
// Displaying Values
System.out.println(“Byte Value: ” + byteValue);
System.out.println(“Short Value: ” + shortValue);
System.out.println(“Integer Value: ” + intValue);
System.out.println(“Long Value: ” + longValue);
System.out.println(“Float Value: ” + floatValue);
System.out.println(“Double Value: ” + doubleValue);
System.out.println(“Character Value: ” + charValue);
System.out.println(“Boolean Value: ” + booleanValue);
System.out.println(“String Value: ” + stringValue);
System.out.println(“Array Values: “);
for (int i : arrayValue) {
System.out.print(i + ” “);
}
}
}
Explanation of the Program:
- Primitive Data Types:
- The program initializes each primitive type with a valid value.
- Examples include
byte
,int
,float
,char
, andboolean
.
- Non-Primitive Data Types:
- A
String
object is used for textual data. - An integer array demonstrates how multiple values of the same type can be stored.
- A
- Output:
- The program uses
System.out.println()
to display primitive values. - For the array, a
for-each
loop iterates through its elements.
- The program uses
Expected Output:
Byte Value: 127
Short Value: 32000
Integer Value: 100000
Long Value: 10000000000
Float Value: 5.75
Double Value: 19.99
Character Value: J
Boolean Value: true
String Value: Hello, Java!
Array Values:
1 2 3 4 5