This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parantheses in explicit casting

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic