Integer Data Types in Java and Kotlin (Byte, Short, Int, Long) with Examples
Table of Contents
Introduction
Integer data types are used to store whole numbers without decimal values. Both Java and Kotlin provide four integer data types based on memory size and value range.
Integer Data Types Overview
| Type | Java | Kotlin | Size | Range |
|---|---|---|---|---|
| Byte | byte | Byte | 8 bits | -128 to 127 |
| Short | short | Short | 16 bits | -32,768 to 32,767 |
| Int | int | Int | 32 bits | -2³¹ to 2³¹ - 1 |
| Long | long | Long | 64 bits | -2⁶³ to 2⁶³ - 1 |
1. Byte Data Type
The Byte data type is the smallest integer type. It is mainly used to save memory in large arrays or streams.
- Memory Size: 8 bits
- Minimum Value: -128
- Maximum Value: 127
Kotlin Example
val minByte: Byte = -128
val maxByte: Byte = 127
println(minByte)
println(maxByte)
Java Example
byte minByte = -128;
byte maxByte = 127;
System.out.println(minByte);
System.out.println(maxByte);
2. Short Data Type
The Short data type stores small integer values larger than Byte but smaller than Int. It is rarely used today except in memory-critical systems.
- Memory Size: 16 bits
- Range: -32,768 to 32,767
Kotlin Example
val shortValue: Short = 1000
println(shortValue)
Java Example
short shortValue = 1000;
System.out.println(shortValue);
3. Int Data Type
Int is the default integer data type in both Java and Kotlin. It is the most commonly used type for calculations.
- Memory Size: 32 bits
- Range: -2,147,483,648 to 2,147,483,647
Kotlin Example
val number: Int = 10000
println(number)
Java Example
int number = 10000;
System.out.println(number);
4. Long Data Type
The Long data type is used to store
very large integer values.
An L suffix is required while assigning values.
- Memory Size: 64 bits
- Used for timestamps, IDs, large calculations
Kotlin Example
val population: Long = 9_223_372_036_854_775_807L
println(population)
Java Example
long population = 9223372036854775807L;
System.out.println(population);
Java vs Kotlin – Integer Data Type Differences
| Feature | Java | Kotlin |
|---|---|---|
| Primitive Types | Yes | No (handled internally) |
| Null Safety | No | Yes |
| Implicit Conversion | Allowed | Not Allowed |
| Type Safety | Medium | High |
Example Difference
// Kotlin (Not Allowed)
val x: Int = 10
val y: Long = x
// Java (Allowed with casting)
int x = 10;
long y = (long) x;
Interview Questions & Answers
Q1. What is the default integer data type in Java and Kotlin?
Answer: int in Java and Int in Kotlin.
Q2. Why does Kotlin not allow implicit type conversion?
Answer: To avoid data loss and make code safer.
Q3. When should Long be used instead of Int?
Answer: When values exceed Int range or for timestamps and large IDs.
Q4. Does Kotlin use primitive data types?
Answer: No. Kotlin uses classes optimized by JVM.
Conclusion
Integer data types are fundamental for numerical programming. Kotlin enhances Java integers by adding strict type safety and null safety while maintaining JVM performance.
Comments
Post a Comment