For which values does Integer.valueOf always return cached objects?
So == between boxed Integers is unreliable outside that range.
Object Class and Wrapper Classes · Exercise 1 of 4
Twenty multiple-choice questions on java.lang.Object and the wrapper classes: toString, equals, hashCode, clone, autoboxing, unboxing, the Integer cache, parsing and BigDecimal. Pick an option to check your answer instantly.
For which values does Integer.valueOf always return cached objects?
So == between boxed Integers is unreliable outside that range.
What is the correct way to check whether two Integer variables hold the same number?
== compares references.
What happens at int n = map.get("missing"); when the key is absent (map is a Map<String, Integer>)?
Use getOrDefault("missing", 0).
What is the difference between Integer.parseInt(s) and Integer.valueOf(s)?
Both throw NumberFormatException for invalid text.
What does Integer.parseInt("12.5") do?
Use Double.parseDouble for decimals.
What does count++ do when count is an Integer?
Wrappers are immutable.
What does Long.valueOf(5).equals(5) return?
Write equals(5L).
Which creates exactly 0.1?
new BigDecimal(0.1) captures the binary approximation 0.1000000000000000055511…
Why does new BigDecimal("2.0").equals(new BigDecimal("2.00")) return false?
compareTo returns 0 for them.
What is Objects.hash(a, b, c) typically used for?
It is null-safe and combines the fields' hash codes.
Timed online tests with instant scores and explanations.