Java
Java Data Types: Complete Reference with Memory Size and Examples
By Utility Zone · 2025-11-06T09:34:37.583267
Java is a statically typed language, meaning every variable must be declared with a data type before use. Java provides a comprehensive set of data types organized into two main categories: primitive data types and reference data types.123
Primitive Data Types (8 Types)
Primitive data types store actual values directly in memory and have fixed sizes defined by the Java language. They are stored on the stack and provide fast access.2456
Integer Types (Signed Whole Numbers)
1. byte
The smallest integer type, useful for memory-efficient storage in large arrays.712
byte age = 25;
byte temperature = -50;
2. short
A 16-bit integer type used when memory conservation in large arrays is important but byte range is insufficient.81
short temp = -200;
short year = 2025;
3. int
The default choice for integer values. Most operations in Java use int by default.8712
int population = 2000000;
int count = 100;
4. long
Used for very large integer values that exceed int range.781
- Size: 8 bytes (64 bits)52
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80735
- Default Value: 0L52
- Example:2
long worldPopulation = 7800000000L; // Note the 'L' suffix
long distance = 999999999999L;
Important: When assigning a value to a long variable, append the letter L to indicate it's a long literal.2
Floating-Point Types (Decimal Numbers)
5. float
A single-precision 32-bit floating-point type used to save memory in large arrays of decimal numbers.731
- Size: 4 bytes (32 bits)52
- Precision: ~6-7 significant decimal digits352
- Default Value: 0.0f52
- Example:2
float pi = 3.14f; // Note the 'f' suffix
float distance = 12.5f;
Important: Always append f to float literals. Never use float for precise values like currency.73
6. double
A double-precision 64-bit floating-point type providing better accuracy than float. This is the default choice for decimal numbers.1372
- Size: 8 bytes (64 bits)52
- Precision: ~15-16 significant decimal digits325
- Default Value: 0.0d25
- Example:2
double precisePi = 3.141592653589793;
double temperature = 98.6;
Character Type
7. char
Stores a single Unicode character using 16-bit encoding.132
- Size: 2 bytes (16 bits)52
- Range: 0 to 65,535 (Unicode characters)12
- Default Value: '\u0000'52
- Example:2
char grade = 'A';
char symbol = '@';
char unicode = '\u0041'; // Represents 'A'
Boolean Type
8. boolean
Represents logical values used in conditional statements and control flow.812
- Size: 1 byte (JVM-dependent, typically 1 byte)52
- Values:
trueorfalse32 - Default Value:
false25 - Example:2
boolean isJavaFun = true;
boolean flag = false;
Reference Data Types
Reference data types store references (memory addresses) that point to objects stored in memory (the heap), rather than storing the values directly. All reference types default to null if not initialized.495
1. String
The most commonly used reference type, representing sequences of characters.9
String language = "Java";
String name = new String("John");
String message = "Hello, World!";
Key Point: Strings are immutable in Java, meaning once created, they cannot be modified.10
2. Arrays
Collections of elements of the same type stored sequentially.1112
- Size: Variable (stored on heap)5
- Default Value: null5
- Element Access: Uses 0-based indexing11
- Example:12
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[^10];
double[] scores = {95.5, 87.3, 92.1};
// Accessing elements
int first = numbers[^0]; // Returns 1
Important: When arrays are passed to methods, the method receives a copy of the reference. Changes to array elements persist after method execution.12
3. Classes (Objects)
Custom data types created by programmers that bundle data and methods together.135
public class Student {
String name;
int age;
}
Student student = new Student();
4. Interfaces
Abstract reference types that define a contract for implementing classes.13
interface Animal {
void speak();
}
class Dog implements Animal {
public void speak() {
System.out.println("Woof!");
}
}
Animal animal = new Dog();
Wrapper Classes
Java provides wrapper classes for each primitive type, allowing you to treat primitives as objects. These classes are useful for:1415
- Using primitives in collections (which require objects)
- Accessing special methods and constants
- Converting between types
| Primitive | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
// Autoboxing (primitive to wrapper)
Integer num = 100;
Double value = 3.14;
// Unboxing (wrapper to primitive)
int primitive = num; // Converts back to int
// Using wrapper class methods
int maxInt = Integer.MAX_VALUE; // 2,147,483,647
int minInt = Integer.MIN_VALUE; // -2,147,483,648
double maxDouble = Double.MAX_VALUE;
Key Differences: Primitive vs Reference Types
| Aspect | Primitive | Reference |
|---|---|---|
| Storage Location | Stack (direct values) | Heap (memory addresses) |
| Memory | Fixed size | Variable size |
| Default Value | Type-specific (0, false, etc.) | null |
| Speed | Faster | Slower |
| Copy Behavior | Copy by value | Copy by reference |
| Examples | int, double, boolean | String, Arrays, Objects |
Practical Example: Using All Data Types
public class DataTypesDemo {
public static void main(String[] args) {
// Primitive Types
byte myByte = 100;
short myShort = 5000;
int myInt = 1000000;
long myLong = 15000000000L;
float myFloat = 3.14f;
double myDouble = 3.14159265359;
char myChar = 'J';
boolean myBoolean = true;
// Reference Types
String myString = "Java Programming";
int[] myArray = {1, 2, 3, 4, 5};
// Output
System.out.println("byte: " + myByte);
System.out.println("int: " + myInt);
System.out.println("long: " + myLong);
System.out.println("double: " + myDouble);
System.out.println("String: " + myString);
System.out.println("Array: " + java.util.Arrays.toString(myArray));
}
}
Output:
byte: 100
int: 1000000
long: 15000000000
double: 3.14159265359
String: Java Programming
Array: [1, 2, 3, 4, 5]
Selection Guide
- Use
byteorshort: When memory is critical and values fit within their range - Use
int: For most integer values (default choice) - Use
long: For very large numbers (population, distances) - Use
float: Rarely, when memory is extremely limited - Use
double: For all decimal calculations (default choice) - Use
char: For single characters - Use
boolean: For true/false conditions - Use
String: For text data - Use arrays: For collections of same-type data
- Use custom classes: For complex data structures combining multiple types
Understanding these data types and their memory requirements is essential for writing efficient, reliable Java applications.312 <span style="display:none">161718</span>
<div align="center">⁂</div>
Footnotes
-
https://www.datacamp.com/doc/java/java-data-types ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13
-
https://www.inexture.com/java-data-types-size-range-usage-guide/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 ↩25 ↩26 ↩27 ↩28 ↩29 ↩30 ↩31 ↩32 ↩33 ↩34 ↩35 ↩36 ↩37 ↩38 ↩39 ↩40 ↩41 ↩42
-
https://www.geeksforgeeks.org/java/java-data-types/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11
-
https://www.freecodecamp.org/news/java-data-types-and-variables/ ↩ ↩2
-
https://devstacktips.com/web-development/2025/08/14/java-data-types-for-high-performance-coding-a-complete-reference/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 ↩25 ↩26 ↩27 ↩28 ↩29 ↩30
-
https://www.shiksha.com/online-courses/articles/primitive-data-types-in-java-blogId-164457 ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
https://www.geekster.in/articles/java-data-types/ ↩ ↩2 ↩3 ↩4
-
https://hyperskill.org/university/java/java-data-types ↩ ↩2 ↩3
-
https://www.sololearn.com/en/Discuss/1488976/are-strings-reference-type ↩
-
https://www.tutorialspoint.com/what-are-reference-data-types-in-java ↩ ↩2
-
https://runestone.academy/ns/books/published/csawesome/Unit2-Using-Objects/topic-2-8-IntegerDouble.html ↩ ↩2
-
https://runestone.academy/ns/books/published/csjava/Unit2-Using-Objects/topic-2-8-IntegerDouble.html ↩ ↩2
-
https://stackoverflow.com/questions/15183387/java-arrays-as-reference-types-in-methods ↩