0

I am wondering if someone could explain something about a class cast for me.

I am playing around with Android and I have a subclass of Application named ExApp.

I want to call a method of ExApp from one of my activities, so I do:

ExApp ex = ((ExApp)getapplication());

What I don't understand is why I need a double set of parentheses? Why can't I just:

ExApp ex = (ExApp)getApplication();  

?

Thanks.

Bogdan Vasilescu
  • 407
  • 4
  • 22
Tom
  • 1
  • 1
  • 1

1 Answers1

8

You can. The two statements are exactly the same.

Where you'd see a difference is if you were calling a method on the result, e.g.

(ExApp) getApplication().foo();

is different to:

((ExApp) getApplication()).foo();

In the first case, it's the result of foo() which is cast to ExApp; in the second, it's the result of ExApp, and the overall expression is the return type of foo().

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194