Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Integer.valueOf().intValue()" supposed to do?

Tags:

java

Here is a java line of code that i have failed to understand.

 String line = "Some data";//I understand this line
 int size;//I understand this line too

 size = Integer.valueOf(line,16).intValue();//Don't understand this one

What i know is Integer.ValueOf(line) is the same as Integer.parseInt(line) , is not so? Correct me if i am wrong; Thanks.

like image 200
Xris Avatar asked Nov 08 '11 06:11

Xris


2 Answers

Integer.ValueOf(line,16) converts string value line into an Integer object. In this case radix is 16.

intValue() gets the int value from the Integer object created above.

Furthermore, above two steps are equivalent to Integer.parseInt(line,16).

In order to get more INFO please refer Java API Documentation of Integer class.

like image 92
Upul Bandara Avatar answered Sep 25 '22 23:09

Upul Bandara


Yes, this is equivalent to:

size = Integer.parseInt(line, 16);

Indeed, looking at the implementation, the existing code is actually implemented as effectively:

size = Integer.valueOf(Integer.parseInt(line, 16)).intValue();

which is clearly pointless.

The assignment to -1 in the previous line is pointless, by the way. It would only be relevant if you could still read the value if an exception were thrown by Integer.parseInt, but as the scope of size is the same block as the call to Integer.valueof, it won't be in scope after an exception anyway.

like image 26
Jon Skeet Avatar answered Sep 25 '22 23:09

Jon Skeet