This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes parantheses in explicit casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parantheses in explicit casting" Watch "parantheses in explicit casting" New topic
Author

parantheses in explicit casting

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84


when i use explicit casting without outer parantheses (Dog) animal.eat(); instead of ((Dog) animal).eat();
i am getting compilation error...what these parantheses tell to the compiler..how the compiler understand
this explicit casting with and without outer parantheses

"I Love Java Ranch"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

If you write (Dog) animal.eat(), it means you are trying to typecast the value returned by the eat method to Dog type. But the eat method has void return type thus it doesn't return anything so you can't typecast it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

It's to do with the precedence. The "." operator has higher precedence than the cast, so it is performed first unless you put an extra set of brackets in. That's why you get the behaviour Ankit describes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parantheses in explicit casting