aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. casting from Khalid Mughal 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. casting from Khalid Mughal" Watch "Reg. casting from Khalid Mughal" New topic
Author

Reg. casting from Khalid Mughal

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
This for everyone referring to Khalid Mughal:
(Pg : 208 )
With reference to the example given i have some doubts:
Doubt 1:
See the last line on the page
System.out.println(" (10): " + (obj instanceof String) );// false
Why does it print false ?
What i understand :


Now the object "obj" actual class is StackImpl class
while the declared type is Object class.
While checking for the instanceof operator it
checks the reference type, which in this case the Object
class and the String class are related, so the code compiles
and at runtime becoz the object actual class is StackImpl
which is not a subclass of String it will give false.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Angela Narain:
This for everyone referring to Khalid Mughal:
(Pg : 208 )
With reference to the example given i have some doubts:
Doubt 1:
See the last line on the page
System.out.println(" (10): " + (obj instanceof String) );// false
Why does it print false ?
What i understand :


Now the object "obj" actual class is StackImpl class
while the declared type is Object class.
While checking for the instanceof operator it
checks the reference type, which in this case the Object
class and the String class are related, so the code compiles
and at runtime becoz the object actual class is StackImpl
which is not a subclass of String it will give false.

Yes. That's correct
instanceof basically helps your compiler to determine if a casting be carried out without a compile time error
But we have no way determining what will the JVM do when we run it until it hits Classcast exception
That's why i believe it is said that casting is very tricky and not recommended often

Ragu
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello ragu.
Because the following in JLS 15.20.2
"At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (�15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
"
I think that instanceof checks the runtime type not the compile type. I mean the compiler won't give any error for an instanceof comparing an object with a type for which the object is not hierarquically related to.


SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Jose Botella:
Hello ragu.
Because the following in JLS 15.20.2
"At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (�15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
"
I think that instanceof checks the runtime type not the compile type. I mean the compiler won't give any error for an instanceof comparing an object with a type for which the object is not hierarquically related to.

Yes. I stand corrected
Thankyou for your clarification
Ragu
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
No, i am still confused !
I am in the opinion that "instanceof" takes the declared
type during the compilation check.
Pls. refer to Pg 206 (third paragraph from below.)
As per the example reference type of Light1 and String
cannot be compared using instanceof as they are not related.
Pls. correct if wrong...
Anyway pls. explain what it should be ?
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Angela,
Your original understanding and the latest post are
perfectly correct. Trust Mughal's book!

What i understand :
Now the object "obj" actual class is StackImpl class
while the declared type is Object class.
While checking for the instanceof operator it
checks the reference type, which in this case the Object
class and the String class are related, so the code compiles
and at runtime becoz the object actual class is StackImpl
which is not a subclass of String it will give false.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I was wrong in my previous post.
instanceof makes a check for hierarquically related types at compile type. The same counts for ==
My try-out program was faulty. And I over looked the following in the JLS:
"
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error.
"
Thanks for correcting me.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Jose Botella:
Hello ragu.
Because the following in JLS 15.20.2
"At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (�15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
"
I think that instanceof checks the runtime type not the compile type. I mean the compiler won't give any error for an instanceof comparing an object with a type for which the object is not hierarquically related to.

I thought i corrected myself based on this JLS info
From this what i understood was
instanceof helps a programmer to determine that if the casting will work during the compile time without a classcast exception when it returns true
If this not correct , then i guess i am confused too
Please somebody help me to understand this clearly
Ragu
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

output: false
At compile time, cat could possibly be instanceof Dog since reference type of cat is Animal and Dog can be cast to Animal. However, at runtime we see that cat is not an instanceof Dog.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg. casting from Khalid Mughal