5

An article on the Oracle Java Community sites gives as an example (for a JPA Converter, but that's not relevant, I guess) the following method:

public Boolean convertToEntityAttribute(String y) {
    String val = (String) y;
    if(val.equals("Y")){
        return true;
    } else {
        return false;
    }
}

What is the use of casting the String y to a String val? Is there a valid reason to do this?

Original article: What's New in JPA

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
  • 8
    `return val.equals("Y")` would suffice yes... – Tunaki Dec 07 '15 at 11:26
  • No need this redundant casting. – Ruchira Gayan Ranaweera Dec 07 '15 at 11:28
  • Looks like they let the intern write that example... – m0skit0 Dec 07 '15 at 11:28
  • 1
    Mistake by a developer,like you and me. Thanks for Identifying :) – Abhiram mishra Dec 07 '15 at 11:28
  • 2
    Tunaki - I'd of written return "Y".equals(val) – farrellmr Dec 07 '15 at 11:29
  • 2
    That whole sample code is awful. First of all, `AttributeConverter` needs two type parameters (as you'd expect, they are the two types you convert from/to), which the author neglects. As a result, the example doesn't even compile. Then there's the unnecessary cast and the icing on the cake is the `if (boolean) {return true;} else {return false;}` construct. – biziclop Dec 07 '15 at 11:37
  • @farrellmr, though that's the better way of writing code, it will not give the same result as original code... So while **refactoring** you shouldn't do such changes without knowing the consequences. :) – Codebender Dec 07 '15 at 15:08

3 Answers3

13

Such cast is completely unnecessary. I can imagine that it was before

public Boolean convertToEntityAttribute(Object y) {
    String val = (String) y;
    ...
}

But later the argument type was changed to String and the author simply forgot to remove the cast.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
4

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

This redundant casting is useless.

Current code can be simplify to

public Boolean convertToEntityAttribute(String y) {
    return "Y".equals(y);
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 3
    note that this is a different thing - code in the article would throw NPE on null values, whereas this would not. In some cases, NPE on null values is desired. – eis Dec 07 '15 at 11:30
  • @eis both cases would not cause NPE, you can cast null to any reference type without getting any exception http://stackoverflow.com/questions/18723596/no-exception-while-type-casting-with-a-null-in-java – Ian2thedv Dec 07 '15 at 11:33
  • 1
    @Ian2thedv but you certainly can't call val.equals() on null value without getting an exception – eis Dec 07 '15 at 11:34
  • @eis You will not get `NPE` by this. What are you referring ? – Ruchira Gayan Ranaweera Dec 07 '15 at 11:35
  • 2
    @RuchiraGayanRanaweera You will in the original code, so this is not equivalent to that. – biziclop Dec 07 '15 at 11:39