19

I have run into some piece of code and I cannot find the meaning of this particular part:

Object[] arrayOfObject = (Object[])(Object[])localObjectInputStream.readObject();
Help[] arrayOfHelp = (Help[])(Help[])arrayOfObject[0];

The question concernes (Object[]) on the first line and (Help[]) repeated twice. It looks very much like casting but then why double casting into the same type?

Your help is very much appreciated!

Clijsters
  • 4,031
  • 1
  • 27
  • 37
ShHolmes
  • 453
  • 3
  • 10

4 Answers4

32

There is no point of doing so. Makes no extra difference but just kills the readability and causes confusion.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
17

There's no good reason to apply the same cast twice, it's simply an error*/quirk of the original author.

There are very few places where a double-cast even with different types is meaningful. But with the same type, no, no point to that at all.


* (a fairly harmless one, though it's not pretty)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • While trying to find the answer to my own question, I've encountered some article about Java casting. And it seems that if "foo" is, roughly saying, not related to ClassA, then we need to cast it first to a type, which IS related to ClassA. Not sure if it's only theoretical, don't program on Java. – ShHolmes Jan 29 '18 at 08:27
  • @SherlokHolmes: If you did, and the article was talking about reference types (my "two objects casts" = "two casts using reference types"), the article is simply incorrect. (Again, I'd have to think about it, but I think there is some purpose to a double-cast with primitives around numeric promotion.) – T.J. Crowder Jan 29 '18 at 08:51
  • @T.J.Crowder Autoboxing example, casting `Long` to `Short`: `Short myShort = (short) (long) myLong;`. You first have to cast to `long` to unbox the `Long`, then narrow that to `short`, and compiler will autobox it to `Short` for you. Of course, `Short myShort = myLong.shortValue()` would be easier, but if you want to use casts, you have to use two of them. – Andreas Mar 26 '18 at 16:38
  • @Andreas: Yes, we went into all that in a long comments thread now deleted. The point of the answer is the **same** cast twice is never useful. – T.J. Crowder Mar 26 '18 at 17:20
4

I assume this is just a "bad" (in meaning of style and unnecessary) programming.

rbf
  • 541
  • 4
  • 16
  • 4
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/18654090) – ddarellis Jan 29 '18 at 10:12
  • @ddarellis Actually, the answer really is as simple as this answer states. It is simply bad programming, or, as realbigflo stated, unnecessary. – Loduwijk Jan 29 '18 at 20:49
1

Seems like some framework generated code where code generator might had some buggy code generation in type casting or may be a simple copy paste mistake where someone might had pasted twice. But programmatic there is no benefit of casting twice.

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28