Java & JVM
Java language, JVM behavior, dependencies and application diagnostics.
75 troubleshooting guides · Page 1 / 4
Why does assigning a nullable Integer to int throw before the next statement?
The assignment can contain an implicit method call even though the source shows no dot operator. A primitive cannot represent a missing quantity.
Why does Integer identity comparison work for small fixtures but fail for larger values?
Two wrapper variables are object references. A test that happens to receive reused wrapper objects can conceal an identity comparison.
Why does a command read from a file fail a Java string comparison?
A literal and decoded input can contain the same characters without being the same object. First distinguish reference identity from content and then investigate the input bytes.
Why does a BigDecimal created from 0.1 contain unexpected digits?
The decimal constructor cannot recover a value that was already approximated in a binary floating-point variable. Choose the representation at the input boundary.
How should an exact BigDecimal division handle a repeating decimal?
An exact quotient may need infinitely many decimal places. That is different from a zero divisor or corrupted input.
Why do BigDecimal values 2.0 and 2.00 behave differently in a HashSet?
Your domain may mean numeric equality while the object also retains a representation scale. Decide which distinction the collection must preserve.
Why does assigning an int multiplication to long still overflow?
The destination type does not necessarily change how the expression is evaluated. Two int operands can overflow before the result is widened.
Why is a negative Java remainder unsuitable for a rotating bucket index?
A remainder and a nonnegative wraparound index are different operations when the input can be negative.
How do I replace a failing downcast without hiding an unexpected payload type?
A cast checks an existing object; it does not transform that object into another class. Follow the producer as well as the failing consumer.
Why cannot a List<Integer> be passed to a method accepting List<Number>?
Allowing that substitution would let the method insert a Double into a list promised to contain only Integer values. The rejection protects the caller’s collection.
Why can I read a List<? extends Number> but not add an Integer to it?
The unknown element type might be a more specific subtype than Integer. Knowing the upper bound is enough to read, but not to manufacture a valid element.
Why does an Object[] variable throw ArrayStoreException when it receives a number?
An array retains its runtime component type even when a wider reference points to it. The reference declaration does not rebuild the array.
Why does removing the value 1 from List<Integer> remove the second row instead?
Overloaded methods can interpret the same-looking number as an index or an object. Check the selected signature before investigating list ordering.
Why can Arrays.asList change an element but cannot append a new one?
The returned list is a view with a fixed size. Element replacement and structural growth are different permissions.
Why does List.of fail before my loop can remove null elements?
The collection factory enforces its input contract during construction. A cleanup loop cannot run against an object that was never created.
Why does a list returned by Stream.toList reject add after a migration?
A terminal collection operation does not automatically promise a mutable result. Review the contract when replacing one stream terminal operation with another.
Why does a HashMap key become unfindable after one of its fields changes?
A key’s lookup identity must remain stable while it is stored. Changing fields used by equality or hashing can leave it in a location selected by its previous state.
Why do equal custom Java keys still create duplicate HashSet entries?
A hash-based collection needs equality and hashing to describe the same identity. A custom equals implementation alone is not enough.
Why does deleting rows inside an enhanced for loop trigger ConcurrentModificationException?
The iterator and the collection can disagree about structural changes even when only one thread is involved. The exception name does not prove a race between threads.
Why does keeping a subList produce surprising changes after editing its parent?
A slice can be a view of another container instead of an independent snapshot. Decide whether your consumer should observe future parent edits.
What should a Java toMap collector do when two records share a key?
A duplicate key is a data-model decision, not just a collector exception to suppress. Decide whether duplicates represent an error, multiple values or a mergeable quantity.
How can I count and export the same Java stream without reusing a consumed pipeline?
A stream describes one traversal. Holding its reference does not preserve a reusable copy of the elements.
Why can a Java stream count skip the work placed in peek?
An observation hook is not a reliable place for required processing. A terminal operation may determine its answer without evaluating every intermediate action.
Why does parallelStream lose results when each worker adds to one ArrayList?
The shared accumulator has become a concurrent mutable resource. A sequential test can hide that ownership error.
Ask your own question · Sign in to post questions and answers.