• 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

Compile-time Type Checking or Run-time Type Checking?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could any one of u plz tell me.
Whenever we call a method, sometimes the compiler resolve the type of its parameters at compile-time but in some other cases it couldn't & it resolves them at run-time.
What do u think is Compile-time type checking is better or run-time type checking?
e.g.
Class A{}
Class B extends A{}
Class C extends A{}
If I write:
A a = new A();
B b = (B)a;
It'll not be checked at compile time but there'll be an exception at runtime.
If I write:
B b = new B();
C c = (C)b;
Then it'll be checked it at compile-time.
Any Suggestions?
 
author
Posts: 3252
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type checking is always done at compile time as far as possible.
When you say "(B)a", it is determined at compile time that this may be legal. After all, B is a subclass of A, so "a" may very well refer to an instance of B. If the compiler were intelligent enough (i.e. with data-flow analysis) it could have seen that "a" does not, in fact, contain a B, but this is not required.
However, because "a" may very well not refer to an instance of B, the compiler requires you to show that you know what you're doing using the cast operator (B). The conversion is a so-called narrowing conversion. The other way around, in "a = (A)b", you may omit the cast; this is a widening conversion that can never lead to a ClassCastException. Any instance of B is also a valid instance of A.
On the other hand, when you say "(C)b" there is no way that "b" can refer to an instance of C. That's why it is flagged up as a compile-time error.
In summary, there are three possible cases:
  • Widening conversions which are always valid; they are performed implicitly when required.
  • Narrowing conversions, the validity of which cannot be determined at compile time; they require an explicit cast and may throw a run-time ClassCastException.
  • Conversions between incompatible types. These are flagged up as compile-time errors.
  • HTH
    - Peter

    [This message has been edited by Peter den Haan (edited October 27, 2001).]
 
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic