aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Object confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Object confusion" Watch "Object confusion" New topic
Author

Object confusion

George Panadis
Greenhorn

Joined: Jun 25, 2012
Posts: 14
The following code (from one of the Ethnuware practice exam questions) will not run:


The compiler complaints that A is not compatible with AA.

Now, if we uncomment the a = null; statement, it compiles.
Why is this?
I am confused...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

I couldn't understand that either, until I tried to compile the code (both ways) and found that it compiled perfectly well (both ways).

However when I tried to run the code, it runs perfectly well because the compile was successful. In the one case the runtime (not the compiler) outputs that error message you posted, and in the other case it didn't output anything.

So you're confusing compiling code with running code. Hope that clears up the confusion.
George Panadis
Greenhorn

Joined: Jun 25, 2012
Posts: 14
Okay, I am using RAD so I guess I couldn't distinguish compile error vs runtime error.
Still, why doesnt it produce a runtime error when a = null ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Because there's nothing wrong with assigning null to a variable of type AA.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

And, in RAD compiler errors appear as red-squiggle underlines under the incorrect code elements.
George Panadis
Greenhorn

Joined: Jun 25, 2012
Posts: 14
I see. Thank you.
George Panadis
Greenhorn

Joined: Jun 25, 2012
Posts: 14
Actually there is one more thing that I could use clarification on.
Given the following code:


The previous code will not compile if I remove the AA cast from the last line.
Why does 'a' need to be cast to AA. It is already of type AA.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

George Panadis wrote:Why does 'a' need to be cast to AA. It is already of type AA.


But the compiler doesn't know that. All the compiler knows about is the reference type, and that is A because of how you declare the a variable on line 4.
George Panadis
Greenhorn

Joined: Jun 25, 2012
Posts: 14
Matthew Brown wrote:
George Panadis wrote:Why does 'a' need to be cast to AA. It is already of type AA.


But the compiler doesn't know that. All the compiler knows about is the reference type, and that is A because of how you declare the a variable on line 4.


I see. Its starting to make sense. I should look more into what the compiler sees versus what is seen at runtime.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Yes, that's right. And remember that the compiler doesn't make any attempt to analyze the code (as you did there). All it officially knows about the variable "a" is that it was declared as type "A". It doesn't look at anything other than that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object confusion