简体   繁体   中英

How is this get evaluated? (Crime)(getListAdapter()).getItem(position)

My Java is quite a bit rusty and I am currently learning Android. I encountered this particular line from the book I am reading:

(Crime)(getListAdapter()).getItem(position).

From what I understand is that this will convert the returned item ( Object type) from getItem() to Crime . The question here is why is (getListAdapter()) enclosed in parenthesis? Is there a meaning on that expression? Is this the same as (Crime) getListAdapter().getItem(position)? I knew that is not just Java but I am perplexed when I encountered this.

At this point I am confuse on the semantics of this kind of expression z = (x)(y).

Thank you!

It's being used to allow the cast to complete without starting a new expression. That allows the subsequent expression of getItem(position) to be on a Crime . It is equivalent to something like

Crime c = (Crime) getListAdapter(); // <-- parenthesis not needed
c.getItem(position); // <--  on the Crime

(Crime)(getListAdapter()).getItem(position) is the same thing as (Crime) getListAdapter().getItem(position)

But if getItem method is defined in the Crime class and the type returned by getListAdapter() is a java.lang.Object , this code won't compile. In fact, the dot following the getListAdapter() will give access to all methods available in java.lang.Object only.

Maybe this line is wrong and the author wanted to write: ((Crime)getListAdapter()).getItem(position)

In that case any dot following ((Crime)getListAdapter()) will give you access to all methods available in Crime class.

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