This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes multiple 'chaining' of dot operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple Watch "multiple New topic
Author

multiple 'chaining' of dot operators

Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
When I see a statement like:

File codeLoc = new File(protectionDomain.getCodeSource().getLocation().getFile());

is there a process by which I can break down each of the "dots"? i.e., should I always resolve the leftmost method protectionDomain.getCodeSource(), after I get its return value use it on the following method getLocation(), get its return value and use it on the next method getFile())...etc... ? I've never seen a decent explanation when the dots operators are chained together like this and would just like to confirm.

Thanks in advance to all those who reply.
Dan


Thanks in advance to all who reply,
Dan
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19747
    
  10

Yep, you got it. That's exactly how you should analyze that expression.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20154
    
  46

Dan Bromberg wrote:I've never seen a decent explanation when the dots operators are chained together like this and would just like to confirm.


As with any operator, it is dependent on precedence and the associativity. Since they are all the same operator (chain of dot operators), then only the assoc matters. And in the case of the dot operator, the assoc is left to right.

Agreed with Paul. What you described is correct.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Thanks for the affirmation, guys!
Hope to pay it forward someday...

Dan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple 'chaining' of dot operators
 
It's not a secret anymore!