File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is Type Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is Type Casting" Watch "What is Type Casting" New topic
Author

What is Type Casting

Rohnit Jain
Greenhorn

Joined: Apr 08, 2010
Posts: 3
Can anyone explain me what is typeCasting with example ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36555
    
  16
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

Joined: Apr 08, 2010
Posts: 3
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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 .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13883
    
  10

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is Type Casting
 
Similar Threads
Unregistered?
I Always Wanna Post Java Related Topics
Software Life
World Market
WA #2 ..... word association