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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic type casting for Java Objects" Watch "Dynamic type casting for Java Objects" New topic
Author

Dynamic type casting for Java Objects

bhaswar goswami
Greenhorn

Joined: Jul 04, 2005
Posts: 14
Hi,
I have a situation where i need to cast java objects dynamically .
saying :

public void getObject( Object paramObject , String type)
{

ArryList list =( type )paramObject ;
// This type i wil know dynamically while calling the Method.


}

can i do it in Java.
If yes . How ??
NB: i don't want to put different if blocks for ddifferent Java types.


Regards,

Bhaswar Goswami
SCJP 5.0 , SCDJWS


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37877
    
  22
Yes, you can do it, but if you are really using anything from the Collections framework, which includes ArrayList, then you want to avoid casts like the plague. An incorrect cast will cause your entire application to crash.

You would need to set your ArrayList up as a generic List; see "Generics" in the Java tutorial, in the first instance.
magesh karnam
Greenhorn

Joined: Sep 08, 2003
Posts: 14
Use reflection and you can do this.

What is reflection


Thanks
Magesh
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
Also can try "instanceOf" operator prior to casting. Care should be taken not to use "instanceOf" operator in large loops etc since it can adversely affect performance.

If you use reflection remember to cache reflected values otherwise it can adversely affect performance.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by bhaswar goswami:
I have a situation where i need to cast java objects dynamically .


With all due respect, I seriously doubt that you are.


ArryList list =( type )paramObject ;
// This type i wil know dynamically while calling the Method.


You could simply cast to ArrayList. Why would you want to cast to something else?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I have a situation where i need to cast java objects dynamically .


I agree with Ilja that this is something that you do not want or need to do.

- You write the source code before you compile or run the program.
- You say that you don't know what you want to cast the variable to before runtime.

Those two are in conflict with eachother. At the moment you write the source code, you already know to what type of variable you are assigning the result of the cast expression to - you already know it at compile time, not at runtime.

So you must be confusing some things.

Can you explain why you think you need to do this?
[ July 18, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12755
    
    5
Care should be taken not to use "instanceOf" operator in large loops etc since it can adversely affect performance.

Whoa! where did that come from? The instanceOf operator is used (invisibly) every time a reference cast is done so it is very frequent in Java programs and has to be fast.
String thing = (String) object ; // there is an instanceOf check being done
- otherwise how would you get a class cast exception??
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic type casting for Java Objects
 
Similar Threads
String and String Buffer
Incomplete javadoc for com.javaranch.common.ServletData
datatypes in java
Difference between array of references and single reference
class Object. Method equals().