[英]What is “Integer.valueOf().intValue()” supposed to do?
Here is a java line of code that i have failed to understand. 这是我无法理解的java代码行。
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? 我所知道的是Integer.ValueOf(行 )与Integer.parseInt(行)相同,是不是这样? Correct me if i am wrong; 如果我错了,请纠正我; Thanks. 谢谢。
Integer.ValueOf(line,16)
converts string value line
into an Integer
object. Integer.ValueOf(line,16)
将字符串值line
转换为Integer
对象。 In this case radix is 16. 在这种情况下,基数为16。
intValue()
gets the int
value from the Integer
object
created above. intValue()
从上面创建的Integer
object
获取int
值。
Furthermore, above two steps are equivalent to Integer.parseInt(line,16)
. 此外,上述两个步骤相当于Integer.parseInt(line,16)
。
In order to get more INFO please refer Java API Documentation of Integer class. 要获得更多信息,请参阅Integer类的Java API文档。
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. 顺便说一下,前一行中-1的赋值是没有意义的。 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. 只有在Integer.parseInt
抛出异常时仍然可以读取该值才有意义,但由于size
的范围与调用Integer.valueof
块相同,因此在异常后它不会在范围内无论如何。
Please look at the data type of the variables on the left hand side. 请查看左侧变量的数据类型。
public class Test {
public static void main(String[] args) {
String s = "CAFE";
Integer m = Integer.valueOf(s, 16);
int n = m.intValue();
System.out.println(n);
}
}
Integer
is a reference type that wraps int
, which is a primitive type. Integer
是一个包装int
的引用类型,它是一种基本类型。
" = Integer.valueOf().intValue()"
and Example: 和示例:
String myNumber = "54";
int c = Integer.valueOf(myNumber).intValue(); // convert strings to numbers
result: 结果:
54 // like int (and before was a String)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.