Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The meaning of Java code (Object[])(Object[])arrayOfBytes[1]

Tags:

java

casting

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!

like image 503
ShHolmes Avatar asked Jan 29 '18 08:01

ShHolmes


3 Answers

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

like image 114
Suresh Atta Avatar answered Oct 28 '22 16:10

Suresh Atta


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)

like image 32
T.J. Crowder Avatar answered Oct 28 '22 17:10

T.J. Crowder


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

like image 25
rbf Avatar answered Oct 28 '22 18:10

rbf