• 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

Dynamic type casting for Java Objects

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use reflection and you can do this.

What is reflection


Thanks
Magesh
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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

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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic