• 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

What is Type Casting

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me what is typeCasting with example ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are telling the compiler and JVM that an object is a particular typeThe List interfce does not have an ensureCapacity method, but the java.util.ArrayList class does. So you can invoke the method on an ArrayList, not a LinkedList. So you tell the compiler that you have got an ArrayList here, and then you require space for 1000000 elements.

Note the instanceof operator ensures you have the correct type and won't suffer a ClassCastException; if you have a LinkedList, that invocation is skipped. One of the few places I like the use of instanceof.
 
Rohnit Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are telling the compiler and JVM that an object is a particular typeThe List interfce does not have an ensureCapacity method, but the java.util.ArrayList class does. So you can invoke the method on an ArrayList, not a LinkedList. So you tell the compiler that you have got an ArrayList here, and then you require space for 1000000 elements.

Note the instanceof operator ensures you have the correct type and won't suffer a ClassCastException; if you have a LinkedList, that invocation is skipped. One of the few places I like the use of instanceof.




Its not easy example to understand, it will not become clear to undestand.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohnit Jain wrote:Its not easy example to understand, it will not become clear to undestand.



int i = (int)16.4; what about this?

P.S.Campbell's example is clear .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try searching for "java casting" with Google? Here are some useful links:

Sun tutorials: Inheritance (scroll down to "Casting Objects")
Java Object Typecasting
Casting and Converting Objects and Primitive Types
Java Tutorial: Casting
Java - Arithmetic Operation, Conversion and Casts in java
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic