Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Double casting"

Tags:

java

casting

I'm currently tutoring a high school student in AP Java and she asked me a question about "double casting". I did not ever hear of this term before, but apparently her teacher expects her to know it for their upcoming final.

The example her teacher provided was if you wanted to cast an Integer into a String, you would have to do the following to not get a compiler error:

Integer i = new Integer(5);
String s = (String)(Object) i;

The question is: when would you want to do this in real life?

The teacher only provided examples which result in a run time error. Also, I never really knew there was a term for this, but it just seems like a bad idea to do it because there's only an error when the two types are incompatible.

Thanks!

like image 697
corgichu Avatar asked Jan 21 '13 02:01

corgichu


People also ask

What is a cast double?

In theatrical terms, double-casting is when two actors are both cast in the same role, and take turns playing the role during alternating performances. It is different than casting an understudy. An understudy only performs when the actor in the role is away or ill.

What is it called when an actor plays multiple roles?

A dual role (also known as a double role) refers to one actor playing two roles in a single production. Dual roles (or a larger number of roles for an actor) may be deliberately written into a script, or may instead be a choice made during production, often due to a low budget.

What's an alternate actor?

Alternate: Someone who occasionally plays a role in a show in order to give the regular performer a rest. For example, a star might play a role six times a week, and her alternate plays the two matinees. Alternates are often retained for difficult parts.

What is typecasting in Theatre?

"Typecast [verb]: to assign (an actor or actress) repeatedly to the same type of role, as a result of the appropriateness of their appearance or previous success in such roles."


2 Answers

Whilst "double casting" certainly isn't a common term and you shouldn't seem any sort of casting of references much, you ought to know what happens (a ClassCastException).

For completeness, there are some cases where it wouldn't CCE:

  • If the value is actually null.
  • Stuff involving primitives. (er, Object to Integer to [unboxing] int, or int to [lossy] byte to [positive] char)
  • Change generic type arguments. List<String> to Object to List<Integer>.
like image 99
Tom Hawtin - tackline Avatar answered Oct 01 '22 17:10

Tom Hawtin - tackline


Yeah, I'm pretty sure that's not a thing. There's no reason that double casting would ever be necessary - it's possible it might get rid of a compile warning about unsafe casting (in which case you're probably doing it wrong), but otherwise that's just not right.

I mean, there's auto toString calling e.g. println("" + i), but even then you don't need to cast to an object first...

Edit: After reading Tom's answer I'm suddenly unsure about this answer - primitives and (particularly) generics can actually use this. I don't have the ability to test anything right now, but anyone reading this answer should definitely take a look at his (and probably upvote it).

I'm going to stick to the line that there are no (or at least extremely few and far between) good reasons to do this, however, and the provided example certainly has nothing to do with it.

like image 32
Jeff Avatar answered Oct 01 '22 17:10

Jeff