user907629
user907629

Reputation: 534

Purpose of this expression

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

Answers (3)

Dennis
Dennis

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

fivedigit
fivedigit

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

ziesemer
ziesemer

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

Related Questions