Question:
How isparseInt()
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?
Best Answer:
Well, the API forInteger.valueOf(String)
does indeed say that the String
is interpreted exactly as if it were given to Integer.parseInt(String)
. However, valueOf(String)
returns a new
Integer()
object whereas parseInt(String)
returns a primitive int
. If you want to enjoy the potential caching benefits of
Integer.valueOf(int)
, you could also use this eyesore:valueOf(String)
may be more attractive than making a new object out of parseInt(String)
because the former is consistently present across Integer
, Long
, Double
, etc.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review