Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Anonymous Cast (I think)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm studying for the OCA/OCP exams and casts cause me real headaches...

I've wrapped my head around the idea that a Reference variable can "point to" an object of a subclass. And, I think I've wrapped my head around the notion of up casts and down casts. But when I see exam questions using what look like anonymous casts I'm really lost.

For example, an exam question may have a prelude such as...



But will then have something like this without any assignment to a variable...



I know it's cast, but what is it? Is it a Hound object being cast into a Dog object? Or, is it a Hound object being assigned to an anonymous Dog reference variable?

And how do I decide which bark method will be called?

The lack of assignment is really confusing me.

Any assistance would be much appreciated.

Kind regards


 
Saloon Keeper
Posts: 9267
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same as
When in doubt try it out
 
Sheriff
Posts: 16926
284
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expression ((Dog) new Hound()) is an object reference expression, i.e., an expression that evaluates to an object reference. ((Dog) new Hound()).bark() is a method invocation expression.  A method invocation expression uses an explicit reference or an implied reference (this). What you have used so far prior to seeing this example is an object reference expression that uses a variable reference. That's a very common way to have an object reference but it's not only way, as you have seen.

It's the same thing as with math and algebraic expressions:

Let x = 7
(x + 5) = 12  (this is an algebraic equation that uses the variable "x")

((14 / 2) + 5) = 12 (this is a mathematical equation that is equivalent to the above)

In the second example, you just calculate the value in line, without using the variable x. Kind of the same thing that's happening in your code example.
 
Junilu Lacar
Sheriff
Posts: 16926
284
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, "Anonymous cast" is not A Thing™ in Java, as far as I know. In fact, now that I think about it, it's a contradiction in terms. If you're going to cast something, you have to know exactly what you're going to cast it to. So yeah, I'm pretty sure now that "anonymous cast" is not a thing in Java.
 
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Duffy wrote:. . . Is it a Hound object being cast into a Dog object? Or, is it a Hound object being assigned to an anonymous Dog reference variable? . . .

Probably, neither nor. But much nearer to the latter.

You can cast the type of a reference however you like. In some instances, however, the javac tool will recognise that the cast could never succeed and it will decline to compile it:-You can cast as much as you like, but you cannot change the actual type of an object If it is a Hound once, it is a Hound for ever, and that cannot be changed. That may be necessary when you have different options for instantiation at runtime. Nobody would write this sort of code in real life, would they?But it will compile. Any of those references can be cast to (Dog)..., but does that make any difference? Maybe only a Hound has a followScent() method and only a Sheepdog has guideSheep(). But you cannot call either of those methods on a Dog reference.That line 3 depends on d actually being a Sheepdog. What you are doing is reassuring the compiler that it is in fact a Sheepdog, and it will believe you. But any casts to a more specific type, officially called narrowing reference conversion are inherently error‑prone. If you cast your Hound object to (Dog)h, that means you are casting to a wider type, which always succeeds, and is never actually necessary. You are not creating an anonymous reference, but asserting to the javac tool that the object behind that reference will be that type.

You need to know about casts for the exam, but in real life, start suspecting that you have something wrong with your design if you start casting references types.
 
John Duffy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Thank you for taking the time to reply to my question. I think I'm closer to understanding this now.

I've worked my way through the whole K&B OCP Exam Guide and I have found "casts", and how to determine whether they will generate a compile time or run time error, to be the most difficult bit. Probably just me.

Thanks again.
 
Are we home yet? Wait, did we forget the tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic