Primitive data types in Java - Time & Space Complexity
We want to see how fast operations with primitive data types run in Java.
How does the time needed change when we work with these simple values?
Analyze the time complexity of the following code snippet.
int a = 5;
int b = 10;
int c = a + b;
boolean isEqual = (a == b);
char letter = 'A';
float pi = 3.14f;
This code performs simple operations using primitive data types like int, boolean, char, and float.
There are no loops or repeated steps here.
- Primary operation: Single arithmetic and assignment operations.
- How many times: Each operation runs once.
Since there are no loops or repeated steps, the time stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 6 |
| 100 | 6 |
| 1000 | 6 |
Pattern observation: The number of operations does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to run these operations stays the same no matter how big the input is.
[X] Wrong: "Primitive operations take longer as numbers get bigger."
[OK] Correct: Primitive operations like addition or comparison take the same time regardless of the number size within their limits.
Understanding that primitive operations run in constant time helps you explain why simple calculations are fast and efficient in real programs.
"What if we replaced primitive types with objects like Integer or Float? How would the time complexity change?"
Practice
Solution
Step 1: Recall Java primitive types
Java primitive types include int, boolean, char, byte, short, long, float, and double.Step 2: Identify non-primitive type
String is a class in Java, not a primitive type.Final Answer:
String -> Option DQuick Check:
Primitive types exclude classes like String [OK]
- Confusing String as primitive
- Thinking boolean is not primitive
- Mixing wrapper classes with primitives
byte in Java?Solution
Step 1: Understand byte range
byte stores values from -128 to 127 inclusive.Step 2: Check each option
byte b = 128; uses 128 which is out of range; byte b = 127; uses 127 which is valid; byte b = 'a'; assigns a char which is not directly compatible; byte b = 128L; uses long literal 128L which is invalid for byte.Final Answer:
byte b = 127; -> Option AQuick Check:
byte max value = 127 [OK]
- Assigning values outside byte range
- Using char or long literals directly
- Not knowing byte limits
int x = 5; float y = 2.0f; System.out.println(x + y);
Solution
Step 1: Understand type promotion in expression
int x is promoted to float when added to float y.Step 2: Calculate the sum and output type
5 + 2.0f = 7.0 (float), but println prints float as decimal number without suffix.Final Answer:
7.0 -> Option AQuick Check:
int + float = float output 7.0 [OK]
- Expecting integer output
- Confusing float suffix in output
- Ignoring type promotion rules
char c = 'AB';
Solution
Step 1: Check char assignment rules
char stores a single 16-bit Unicode character, so it must be assigned a single character in single quotes.Step 2: Analyze the given code
'AB' is two characters, which is invalid for char type.Final Answer:
char can only hold a single character -> Option CQuick Check:
char = single character only [OK]
- Assigning multiple characters to char
- Confusing char with String
- Forgetting single quotes for char
int can hold. Which primitive type should you use in Java?Solution
Step 1: Recall int and long ranges
int holds up to about 2 billion (2^31-1), long holds much larger values (2^63-1).Step 2: Choose type for large whole number
long is the correct primitive type for large integers beyond int range; float and double are for decimals, short is smaller than int.Final Answer:
long -> Option BQuick Check:
Use long for large integers beyond int [OK]
- Using float/double for whole numbers
- Choosing short which is smaller
- Not knowing long size
Ready to prove it?
Pass the test and this pattern moves unknown
