• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic type casting for Java Objects

 
bhaswar goswami
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50177
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use reflection and you can do this.

What is reflection


Thanks
Magesh
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic