Is there a valid reason to do this?
None whatsoever1.
But the flipside is that the Java compiler knows that the typecast is unnecessary and optimizes it away. So the only "damage" is to readability.
For example.
[stephen@blackbox tmp]$ cat Test.java
public class Test {
public void test (String x) {
String s = (String) x;
System.out.println(s);
}
}
[stephen@blackbox tmp]$ javac Test.java
[stephen@blackbox tmp]$ javap -c Test
Compiled from "Test.java"
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void test(java.lang.String);
Code:
0: aload_1
1: astore_2
2: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
5: aload_2
6: invokevirtual #3 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
9: return
}
[stephen@blackbox tmp]$
The statement String s = (String) x;
is compiled to a simple load and a store; no checkcast
instruction.
I wouldn't be surprised if the JIT compiler was capable of optimizing away a redundant checkcast
... if it saw one.
1 - ... in hand written code. In source code that was generated, a redundant typecast could be serve the purpose of making it easier to write the source code generator. After all, the readability of generated code is largely irrelevant.