Solved

instanceof  operator in java

Posted on 2016-08-17
26
72 Views
Last Modified: 2016-08-20
Hi,

Why we use instanceof  operator in java in down casting only not in upcasting. How ClassCastException  are generated?
please advise
0
Comment
Question by:gudii9
  • 13
  • 9
  • 3
  • +1
26 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 125 total points
ID: 41760441
Upcasting can be checked at compile time and therefore doesn't need to use a runtime check like the instanceof operator. You get a ClassCastException when you try to downcast to a type that's not in the type hierarchy of the object you're casting.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760444
use a runtime check like the instanceof operator
why instanceof is called runtime check?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41760459
The check happens while the code is running (i.e., not during compilation).
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760463
how to for which operators happens at compile time and for which operators happen at run time? any other operators apart from instanceof that happens at runtime? any examples for operators for which check happens at compile time?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41760473
It's not really an important distinction to make. When you think about it, all operators happen at runtime, right? But they're also checked for proper syntax at compile time. If you have a specific question about an operator, I can answer that. Beyond that, we're getting to concepts that probably aren't useful.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 41760523
Perhaps an example would help?

Integer x = new Integer(5) ;
Object y = x ;   // Upcast - compiler knows "x" is an Object so allows this
Integer z = y ; // Downcast - compiler doesn't know that y is actually an Integer so rejects this

Integer a = (Integer)y ; // Runtime cast - but may throw an exception (maybe y is not an Integer)?

if (y instanceof Integer) {  // Runtime check
   Integer a = (Integer)y ;   // Safe cast - we know y is an Integer here so this will work
}

Open in new window


Any clearer?

Doug
0
 
LVL 4

Assisted Solution

by:Elizabeth Anderson
Elizabeth Anderson earned 125 total points
ID: 41760595
class ClassCastExceptionDemo
{
    public static void main(String args[])
    {
    
    Object ob=new Integer(10);

    // ClassCastException occurs
    System.out.println("The value is "+(String)ob);

            
    }
}

Open in new window


Output
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer canno
t be cast to java.lang.String
        at ClassCastExceptionDemo.main(ClassCastExceptionDemo.java:9)

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41761439
Integer x = new Integer(5) ;
Object y = x ;   // Upcast - compiler knows "x" is an Object so allows this//WHY COMPILER KNOWS HERE
Integer z = y ; // Downcast - compiler doesn't know that y is actually an Integer so rejects this//WHY COMPILER DOES NOT KNOW HERE...WHY THIS IS CALLED DOWNCAST HERE WITHOUT KNOWING WHAT Y IS?
Integer a = (Integer)y ; // Runtime cast - but may throw an exception (maybe y is not an Integer)?//WHY COMPILER NOT SURE ON Y IF INTEGER OR NOT

if (y instanceof Integer) {  // Runtime check
   Integer a = (Integer)y ;   // Safe cast - we know y is an Integer here so this will work
}
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41761483
The compiler knows, at compile time, every superclass and interface in the hierarchy of any given type. Because it knows this, it can check upcasts at compile time. The compiler does not know everything that extends or implements a given type, so downcasts are always a runtime check. The JVM knows, at runtime, the exact type hierarchy of every object in memory (otherwise it wouldn't be able to instantiate them), so it's well-equipped to handle the runtime type checks.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 41761726
WHY COMPILER NOT SURE ON Y IF INTEGER OR NOT

Like CPColin said.

The compiler sees what you see:

public void check(Object y) {
   Integer a = y ;          // What type of object is y?
}

Open in new window

It could be an Integer, it could be a String.
You can't tell and neither can the compiler - so it rejects this.

At runtime, "y" will be a specific instance (like Integer(25) or String("Hello")) and then you can tell whether this is OK or not.  But only at runtime.

If we want the compiler to trust us that this will be OK we tell it:

public void check(Object y) {
   Integer a = (Integer)y ;          // Trust me, y will be an Integer when we get here
}

Open in new window

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41761857
The compiler sees what you see:

public void check(Object y) {
   Integer a = y ;          // What type of object is y?
}

Select all
 
Open in new window
It could be an Integer, it could be a String.
You can't tell and neither can the compiler - so it rejects this.

At runtime, "y" will be a specific instance (like Integer(25) or String("Hello")) and then you can tell whether this is OK or not.  But only at runtime.

If we want the compiler to trust us that this will be OK we tell it:

public void check(Object y) {
   Integer a = (Integer)y ;          // Trust me, y will be an Integer when we get here
}

''down cast is clear.


not clear on upcast yet


Object y = x ;   // Upcast - compiler knows "x" is an Object so allows this//How COMPILER KNOWS HERE??
0
 
LVL 26

Expert Comment

by:dpearson
ID: 41761910
''down cast is clear.
Excellent.


Integer x ;
Object y = x ;   // Upcast - compiler knows "x" is an Object so allows this//How COMPILER KNOWS HERE??

OK the key here is that the compiler knows the class hierarchy - it can see this part of the code:
public class Integer extends Number {
}
and then Number extends Object {
}

So we have:
 Integer extends Number extends Object

So:
Integer x ;
Object y = x ;  // OK
Number z = x ; // Also OK
String a = x ; // Not allowed (Integer does not extend String)

Open in new window

Make sense?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41761941
OK the key here is that the compiler knows the class hierarchy - it can see this part of the code:
public class Integer extends Number {
}
and then Number extends Object {
}


so compiler is designed and prebuilt such a way that it understands all the class hierarchy starting from Integer to number to Object??(only for built in classes like Interger right not for custom classes we write like Customer.Person??
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 14

Expert Comment

by:CPColin
ID: 41761955
The compiler, at compile time, knows the entire type hierarchy of every type, all the way up to Object, even for custom classes. Otherwise, it wouldn't be able to compile the code.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763064
Integer x ;
Object y = x ;  // OK
Number z = x ; // Also OK
String a = x ; // Not allowed (Integer does not extend String)

above is up cast right?

public void check(Object y) {
   Integer a = (Integer)y ;          // Trust me, y will be an Integer when we get here
}

above is down cast right?
i am confused with jargon up and down.

LHS(left hand side)=RHS(right hand side)
if we are assigning RHS Subclass to LHS  super class then up casting which checked at compile time code wont suddenly break at run time?

if we are assigning RHS Superclass to LHS  sub class then down casting? unless we use instaceof code may break at runtime as compiler cannot spot at compile time??


what is up wha is down hard remember in brain. just have to by heart?
by heart
Learned by rote; memorized word for word.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763069
we always assign to parent reference variable with child object which is common and compiler takes care of it right which is up cast...
other rare scenario where child reference assigned with parent object which is down cast ,,possible runtime classcast exception without instanceof operator check
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763071
package com.upcast;

public class ClassCAst {

      
                public static void main(String args[])
                {
               
                Object ob=new Integer(10);

                // ClassCastException occurs
                System.out.println("The value is "+(String)ob);

                       
                }
            }

above down cast failed as below

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
      at com.upcast.ClassCAst.main(ClassCAst.java:12)



how to fix this with instanceof?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41763082
If you wrap that line in a conditional like if (ob instanceof String), the downcast won't be attempted unless it will succeed.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763126
package com.upcast;

public class ClassCAst {

	
		    public static void main(String args[])
		    {
		    
		    Object ob=new Integer(10);

		    // ClassCastException occurs
		    if (ob instanceof String)
		    System.out.println("The value is "+(String)ob);
		    System.out.println("passed downcast line");

		            
		    }
		}

Open in new window


passed downcast line
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763128
package com.upcast;

public class ClassCAst {

	
		    public static void main(String args[])
		    {
		    
		    Object ob=new Integer(10);//upcast fine

		    // ClassCastException occurs
		    if (ob instanceof String)
		    System.out.println("The value is "+(String)ob);
		    System.out.println("passed downcast line");

		            
		    }
		}

Open in new window


Object ob=new Integer(10);//upcast fine

above is upcast right where as below is down cast?
  System.out.println("The value is "+(String)ob);
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41763133
Yes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763348
package com.upcast;

public class ClassCAst {

	
		    public static void main(String args[])
		    {
		    
		    Object ob=new Integer(10);//upcast fine

		    // ClassCastException occurs
		    if (ob instanceof String)
		    System.out.println("The value is "+(String)ob);
		    System.out.println("passed downcast line");

		            
		    }
		}

Open in new window

but String an Integer are not like super, sub classes right? they are more like peers so should we technically call it like peer casting rather than down casting?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41763686
You're casting an Object reference into a String reference, so it's a downcast. Just because the actual object is an instance of Integer doesn't change that fact. Basically, if it's not an upcast (which the compiler determines), it's a downcast.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763708
You're casting an Object reference into a String reference

is Object is big or String is big just to imagine and relate like physical size , cast, foundry  forgemanufactoring terminology ?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41763711
If you still had so many questions about this subject, why did you already assign points? I don't know that bringing manufacturing terminology into this discussion is going to help with the concepts at all.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763715
sure. cast means my mind goes that direction. I was under impression that sub class should be big compared to parent class. But it seems other way
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now