1

I have a string representing a numerical value, how to decide whether to use Long.valueOf() or is sufficient Integer.valueOf()? The question may be extended to consider the whole range of types: byte, short, int and long.

4
  • I would convert to long, then test the value to see what it could fit into. Watch out for floating point when using this scheme. Commented Dec 15, 2022 at 14:37
  • 2
    The Integer.MAX_VALUE is 2147483647. So while it is not precise you could get a rough estimate by looking at the length of the String you are trying to parse. Everything 9 or less digits fits an Integer, anything with 11 or more doesn't and most numbers with 10 digits also don't fit. So length <= 9 Integer else Long would give results that I personally would consider "good enough" Commented Dec 15, 2022 at 14:44
  • 2
    I'm with @markspace. The costs of a single long vs an int,short or byte are only relevant if you're working with very limited resources. Just convert to long, then compare against Byte.MAX_VALUE, Short.MAX_VALUE or Integer.MAX_VALUE. Commented Dec 15, 2022 at 14:51
  • 2
    Also note that you should only use valueOf if you want the result to be a wrapper. If you want a primitive, always use Long.parseLong / Integer.parseInt / Short.parseShort ' / Byte.parseByte. Commented Dec 15, 2022 at 14:52

1 Answer 1

1

You asked:

how to decide whether to use Long.valueOf() or is sufficient Integer.valueOf()

Well, you have to know the kind of data expected to be used in your app.

Each numeric integer type has a limit on its range.

  • If you know for certain the domain of values valid in your context, choose the most limited type to appropriately represent that fact.
  • If you do not know the domain of possible values, then go with the biggest possible range: long/Long.

A long/Long is a 64-bit value with a range of Long.MIN_VALUE to Long.MAX_VALUE, from 2^63-1 through -2^63. The int/Integer type is a 32-bit value, with a range of a bit more than +/- two billion.

To learn the minimum/maximum range of the various types, look at the Javadoc of their wrapper class for their MIN_VALUE & MAX_VALUE constants. That limit applies to both the respective primitive type and the object type.

Sign up to request clarification or add additional context in comments.

4 Comments

@AlexanderIvanchenko I considered your point when drafting the Answer. The Question does not really focus on the issue of conserving memory. If we put that issue aside, then using short can be useful in expressing the fact that the programmer has determined the domain’s range of values fit within the range of that type.
Ok, fair point.
As an opinionated remark: I would treat a whole number as int even they can fit into byte (if there's no requirement to plug byte value somewhere).
@AlexanderIvanchenko And, thanks for your feedback and suggestions. And also thank you for your recent edit to fix a different Answer of mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.