This question is a follow-up to Does java have a int.tryparse that doesn't throw an exception for bad data?, which is marked as a duplicate of Java: Good way to encapsulate Integer.parseInt().
Both questions seem to be more about how to catch the NumberFormatException
thrown by Integer.parseInt()
or provide a better API which does not use exceptions by encapsulating Integer.parseInt()
.
But neither is formulated to specifically address the performance aspect of the fact that Java's Integer.parseInt()
throws an exception if the input is not parseable as an int
. If most of your input consists of valid int
s, this won't matter. But if your input consists of a lot of data which may or may not be int
s and you need to parse it, Integer.parseInt()
will be inefficient.
So this specific question is about how to parse integers efficiently, given that the input can consists of lots of valid integers but also lots of invalid ones.