Reputation: 534
I am relatively new to Java. I came across this statement while going through an example
((Integer)arg).intValue()
Can anybody explain this expression?
Upvotes: 1
Views: 73
Reputation: 4017
(Integer) arg
basically casts integer over the argument, meaning it attempts to return the arg value in an integer format.
intValue()
returns the value of the specified number as an int.
Upvotes: 1
Reputation: 18682
arg
is being typecasted into an Integer
(Java's wrapper class for int
). The intValue
method is then called on the Integer
, returning the primitive int
value.
Upvotes: 1
Reputation: 28687
Assuming arg
is an Object
, it is being cast to an Integer
so that the primitive int
value can be obtained using the intValue()
method available on Integer
.
In Java 5 and above, autoboxing could be utilized to simplify this to:
int x = (Integer)arg;
Upvotes: 3