简体   繁体   中英

Android Java class casting

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.

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, eg

(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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM