Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use the java 5 method cast of Class?

Tags:

java

java-5

Looking through some code I came across the following code

trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto));

and I'd like to know if casting this way has any advantages over

trTuDocPackTypdBd.update((TrTuDocPackTypeDto)packDto);

I've asked the developer responsible and he said he used it because it was new (which doesn't seem like a particularly good reason to me), but I'm intrigued when I would want to use the method.

like image 373
Steve Bosman Avatar asked Oct 28 '08 15:10

Steve Bosman


People also ask

Why do we need type casting in Java?

To sign off, learning Java typecasting is necessary for becoming a successful developer or programmer. The intent is to define functions and then ensure variables within the communication are performing as per the end-functionality.

What is class cast in Java?

The cast() method of java. lang. Class class is used to cast the specified object to the object of this class. The method returns the object after casting in the form of an object.

How do I resolve Java Lang ClassCastException error?

How to handle ClassCastException. To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

Is Java type casting expensive?

To answer your questions. Up casting usually costs virtually nothing, (when you change the reference type to a parent class of the object). Knowledge of the reference type is enough to decide if uptyping is valid, it just gets the class loader to look up the inheritance map.


2 Answers

These statements are not identical. The cast method is a normal method invocation (invokevirtual JVM instruction) while the other is a language construct (checkcast instruction). In the case you show above, you should use the second form: (TrTuDocPackTypeDto) packDto

The cast method is used in reflective programming with generics, when you have a Class instance for some variable type. You could use it like this:

public <T> Set<T> find(Class<T> clz, Filter criteria) {
  List<?> raw = session.find(clz, criteria); /* A legacy, un-generic API. */
  Set<T> safe = new HashSet<T>();
  for (Object o : raw) 
    safe.add(clz.cast(o));
  return safe;
}

This gives you a safe way to avoid the incorrect alternative of simply casting a raw type to a generic type:

/* DO NOT DO THIS! */
List raw = new ArrayList();
...
return (List<Widget>) raw;

The compiler will warn you, Unchecked cast from List to List<Widget>, meaning that in the ellipsis, someone could have added a Gadget to the raw list, which will eventually cause a ClassCastException when the caller iterates over the returned list of (supposed) Widget instances.

like image 115
erickson Avatar answered Oct 23 '22 02:10

erickson


The main case for doing it (IME) is when you need to safely cast in a generic class/method. Due to type erasure, you can't cast to T but if you've been provided a Class<? extends T> parameter then you can use that to cast and the result will be assignable to a variable of type T.

like image 37
Jon Skeet Avatar answered Oct 23 '22 02:10

Jon Skeet