How is parseInt() different from valueOf() ?
They appear to do exactly the same thing to me (also goes for parseFloat(), parseDouble(), parseLong() etc, how are they different from Long.valueOf(string) ?
Also, which one of these is preferable and used more often by convention?
Well, the API for
Integer.valueOf(String)does indeed say that theStringis interpreted exactly as if it were given toInteger.parseInt(String). However,valueOf(String)returns anewInteger()object whereasparseInt(String)returns a primitiveint.If you want to enjoy the potential caching benefits of
Integer.valueOf(int), you could also use this eyesore:Now, if what you want is the object and not the primitive, then using
valueOf(String)may be more attractive than making a new object out ofparseInt(String)because the former is consistently present acrossInteger,Long,Double, etc.