• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding the SCJP exam?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am doing some exercise on some book: if the question say that chose those which can compile? but the answer given to me throw an exception..should it still consider true?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not quite sure what you're asking, but I'll take a shot anyway.

The question might be trying to illustrate the concept that compilation does not imply correct operation. Compilation is a product of correct syntax and operation is a product of correct design. Invalid syntax will lead to compiler errors while poor design will lead to runtime errors.

More directly, the following is a valid java program:



(nb. A RuntimeException does not need to be declared as being thrown. Think of what you would have to add to this program if you were throwing an Exception)

Hope that helps.
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mayb i should i give an example

This is wat i mean
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, the program compiles without errors.
Second when run there is a ClassCastException on this line:


The variable a can refer to a Database which could be (but is not necessarily) an Oracle (because Oracle is a subclass of Database).

That's what you are saying with the cast (Oracle).

But, a doesn't not refer to an Oracle when the program is run, because of the line:


So at runtime the JVM complains giving the ClassCastException.

The cast is like a promise, the exception is because you have not kept to the promise.
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I get it.

If the question is "will that compile" the answer is yes.

why?

Oracle "is a" Database

This means at any point, a Database object *might* be an Oracle object (but it *might* not be, the point is that it *could* be). The same way that all objects have the Object class as a supertype.

a simpler example:



At 1, we can assign String to Object because String "is a" Object (just like Oracke "is a" Database in your example). At 2, all the compiler knows is that we are casting an Object to an Integer. Integer "is a" Object, so this case *might* work (we know in this case that it won't work, but that's beside the point). The cast is the programmer telling the compiler "I know the type really is Integer, even though you think it's just Object". The compilter looks at Integer and sees that it has Object as a supertype and thus an Integer can be stored in an Object reference.

Here's an example where it won't work:



This time, I've set the time of something to String (1). When the compiler looks at 2, it knows that an Integer could never be stored in a String (Integer is *not* a String because it does not have String as a supertype).

A good programmer will guard their casts (and possibly try/catch exceptions) as follows:



At execution, this program will output "something is not an Integer" because the if statement at 1 will validate the cast *before* it is executed. The instanceof operator will evaluate to true if the object on the left has the object on the right as a type or supertype. In this case, the String instance does not have Integer as a sypertype and so the if statement will be false (and run the else block).

This facility (of being able to cast down an object hierarchy) is a necessary* feature and you, as a programmer, have to know that (1) it can compile even if it is obviously wrong and that (2) there are ways to prevent and guard against bad casts (instanceof cast guarding is common).

* necessary, for example, because collections (like List, Vector, etc) used to only return Objects from get methods (with generics, they can return a specific type). You would, at some point, have to cast down from the Object to make the result useful - but you chould guard your casts.

Hope that helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic