wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes examlab - 1 [reference variable casting] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "examlab - 1 [reference variable casting]" Watch "examlab - 1 [reference variable casting]" New topic
Author

examlab - 1 [reference variable casting]

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198


This code compiler and runs! I have no idea how to start thinking about this question.

Can somebody explain the thought process for this question? [the six lines 'from here']
Thanks


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
nathan dennis
Greenhorn

Joined: Jun 23, 2009
Posts: 1
not that i have taken this exam, but coming from a math background this concept is used frequently in group and set theory. this is what is known as the Property of Transitivity.

if A and B extends A, then A is contained in B. if B and C extends B, then B is contained in C. therefore by transitivity, A is contained in C. hence in C, B implements Min. (def of class.... all in the same group,,, much like the Reals or some other set or subset you might be working with)


since B also implements Min, you can cast "m" to C because B is contained in C and B implements Min. (//from here)
m is Class Min which is implemented by B. etc. etc.

dont bother try to run this... it does nothing except demonstrate concept... you need to actually create and A B and C class that does something in order to really see it at work.


Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

instanceof test fails but no ClassCastException is thrown


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
Post your code, Lukas.
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
I still do not know how to think about these questions.

There are two parts to the problem:
a) whether it will compile
b) whether it will run

and in solving these type of problems I am always confused whether to look at the reference first or the object?

Please help me with this
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 780

Hi Aakash,

Up cast means casting from sub class to super class
Down cast means casting from super class to sub class

Up casting happen automatically or you can do it manually.
Down casting happen only manually. You have to do it.




See comment at end of each line

Correct me if I am wrong


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
aabir sanyal
Greenhorn

Joined: Jun 22, 2009
Posts: 21
Ninad Kulkarni wrote:Hi Aakash,

Up cast means casting from sub class to super class
Down cast means casting from super class to sub class

Up casting happen automatically or you can do it manually.
Down casting happen only manually. You have to do it.




See comment at end of each line

Correct me if I am wrong



This will throw runtime ClassCastExceptions right??
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
hi Ninad,

thanks for the excellent explanation [now atleast i will think of attempting such questions]

So, in this situation the casts compile and run.

It would be helpful if you can give a case where the cast compiles but not runs.

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
aabir sanyal wrote:
This will throw runtime ClassCastExceptions right??


No, they all run fine. --> No runtime exceptions.

It seems I am not the only one confused with this.

I am waiting for someone to give an example where they compile but fail at runtime.
jeetendra Choudhary
Ranch Hand

Joined: Jun 02, 2009
Posts: 33
No there is no ClassCastException. It Compiles and run fine. I have a confusion that Min is an interface we are supposed not to create object of interface.
so can any one explain me What this Line is doing here..??


@Aakash
i think This Link would be helpful.

Thanks & Regards
Jeetendra..!!


Preparing for SCJP...!!
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
jeetendra Choudhary wrote:No there is no ClassCastException. It Compiles and run fine. I have a confusion that Min is an interface we are supposed not to create object of interface.
so can any one explain me What this Line is doing here..??


@Aakash
i think This Link would be helpful.

Thanks & Regards
Jeetendra..!!


That line initializes the m reference to null. The only restriction is that you cannot use the new construct with an interface type.

Thanks for the link:

I think I now understand how this happens.

I am crossposting a reply from the link mentioned by Jeetendra:

avi sinha wrote:
what i know is:

1> you can cast object refernces only if they are in the same inheritance tree. you can ,means it will compile .it doesn't say that it will not give any runtime error.

2> if the original object & the referance are of the same type ,it will run fine too.

3>if the original object is an object of the subclass of the referance type ,it will run fine too.


avi sinha



----------------------------------------------------------------------------------------------------------------------------------------------

Related to this :

does anyone of the following have anything to do with the concepts of reference variable casting?

a) instanceof
b) ==
c) equals()

Now that we know how casting works, probably understanding these will be easier?
jeetendra Choudhary
Ranch Hand

Joined: Jun 02, 2009
Posts: 33
Well What i know is
instanceof() is used to check the ISA relation that is the object is an object of the subclass of the reference type or not.
==
is used for primitive comparison. it becomes a little tricky when it comes to wrapper class except that it returns true if the primitives having equal value.
equals()
is used for object comparison. means wheather both object reference refer to same object or not.

Any more explanation and rules are very welcome.

Thanks & Regards.
Jeetendra...!!
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 780

@ Aakash,

Object o = new Obejct();
String s = new String("String");
Integer i = new Integer(1);

o = s; It will compile and runs fine. Automatic upcast.

s = (String)o; It will compile and runs fine. Manual downcast needed.

s = (String)i; It will not compile. Compiler thinks that Integer and String are different objects, they can't refer to each other always so compiler will not allow at compile time.

o = s; Automatic Upcasting allowed.
i = (Integer)o; It will compile but throws ClassCastException here. Actually o refer to String object but compiler thinks that o may refer to Integer object so compiler will allow this code to compile.

Correct me if I am wrong
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Ninad Kulkarni wrote:@ Aakash,

Object o = new Obejct();
String s = new String("String");
Integer i = new Integer(1);
o = s; It will compile and runs fine. Automatic upcast.
s = (String)o; It will compile and runs fine. Manual downcast needed.


You are right, if you have assigned before the reference variable o the String object s, otherwise the execution will fail. It needs to be assigned to a String object. So the following code doesn't execute, but successfully compiles:


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
The last post is confusing me.

It was said:


but now there is a similar situation with casting Object to Integer
i = (Integer)o
and when casting Object to String since both String & Integer are subclasses of object then why doesnt the above formula for A and m work here??

I know I am wrong somewhere in understanding this, but can anybody tellme?

[AG Edit: corrected code tags]
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537



This will compile fine but execute only if m has the object of B or C.


This will compile fine but execute only if o has an Integer Object

Both cases, compilation is successful but there is a possibility of choking at runtime.



[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Nitish Bangera wrote:

This will compile fine but execute only if m has the object of B or C.


This will compile fine but execute only if o has an Integer Object

Both cases, compilation is successful but there is a possibility of choking at runtime.






this also compiles and runs fine though I commented m having a reference to b. Please explain what you mean.


SCJP 6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well basic point to note is that the compiler will check only the reference type and while casting it checks the the variable which is present for casting to the casted type is in the same inheritance hierarchy.


Well m is an interface and can have Objects of type B and C. So it can be casted to C and given to and reference type of B. This will only choke at runtime if the object present is of type B which cannot be casted to the type C. So this gives a runtime exception. But in your original code Min m = null means its not assigned to any object at runtime. So there is no exception at runtime as all the references are nulled which is valid.


Well this will compile and execute properly without any exception even if m referred to an object of Type B and C. But in your code m = null so a2 is assigned a null reference.

Same goes for


But in

If m hadn't been null then this particular would give a runtime exception if b2 referred to an object of type B which could not be casted to a type of C. But as m = null, m2 is assigned null.

On the same lines is





Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Something similar on this line has been discussed here too. And remember, null can be typecasted to any type...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
So compiler only cares about reference variable types ,and that they are the same hierarchy without looking at what the reference contains.
And whether this object is compatible with the cast or not is only determent at runtime. Did I get it right???
If so will I be expected to know when it throws exceptions?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

The JVM creates the object and not the compiler. So the objects are present only at runtime. Compiler's present only to check if the syntax typed by the programmer are correct or no and also if the particular things will cause an error at runtime as in with generics.

You will know when it will throw an exception if the reference type refers to an object which cannot be casted to the type given by the programmer.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Does it ask at the exam if it fails to run? Should I be expected to know that
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I guess its pretty basic to know that..... Well K&B chapter 5 pg 382 will help you. Know what all exceptions are there and when are they thrown. Well Compilation fails and Runtime exceptions are some of the answers in the SCJP6 exam so you need to know them.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Thanks, I guess I do have to read the book again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: examlab - 1 [reference variable casting]
 
Similar Threads
Assigning references problem
Casting Problem
ExamLab Question
Casting problem
can't understand examlab downcasting question..