Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does parseInt warn of using valueOf

Tags:

java

When looking at the source code for Integer.parseInt(String s, int radix) (java 8, 1.8.0_131), I found the following comment block:

/* * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */ 

While I understand the first part about the IntegerCache, I don't understand why there's a warning about valueOf, and why in this context.

I see that valueOf() relies on parseInt(), but I still don't get why there's this warning.

Can someone explain what exactly the comment warns me about (and the context where valueOf shouldn't be used), and what could possibly go wrong.

Edit:

The code in Integer.valueOf(int i) seems to have changed since the other question from the comment below was asked, it is now

public static Integer valueOf(int i) {     if (i >= IntegerCache.low && i <= IntegerCache.high)         return IntegerCache.cache[i + (-IntegerCache.low)];     return new Integer(i); } 

and should be save from the assertion error before.

like image 791
Michael Avatar asked Jun 19 '17 17:06

Michael


People also ask

What is the difference between parseInt and valueOf?

valueOf() returns an Integer object while Integer. parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer. valueOf() whereas only a String can be passed as parameter to Integer.

What is the purpose of integer valueOf ()?

valueOf(int a) is an inbuilt method which is used to return an Integer instance representing the specified int value a. Parameters : The method accepts a single parameter a of integer type representing the parameter whose Integer instance is to be returned.

Is the function of valueOf () in Java?

Java - valueOf() Method The valueOf method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. This method is a static method. The method can take two arguments, where one is a String and the other is a radix.

What is the use of parseInt method in Java?

The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .


1 Answers

Can someone explain what exactly the comment warns me about (and the context where valueOf shouldn't be used), and what could possibly go wrong.

The Integer class creates and maintains a cache of Integer objects representing small integer values; by default, values in the range -128 to 127 are covered (more discussion here, here, and many other places). Integer.valueOf() will return an object from this cache when its argument represents a number in the range. The comment is warning that parseInt() must not rely on valueOf() because the former may be invoked before that cache is populated.

The misbehavior that could be expected in that case is not specified, and conceivably might vary between Java versions, but plausible possibilities are that null would be returned or an exception (NullPointerException, IndexOutOfBoundsException, ...) would be thrown.

In any case, this is an internal comment in the implementation, not a comment to users of class Integer. By the time any user code runs, the necessary cache initialization is complete, and Integer.valueOf() can be relied upon to behave fully as its API documentation describes.

like image 172
John Bollinger Avatar answered Nov 07 '22 03:11

John Bollinger