2
When I was learning Java, I had a Class that had a property like Enum, and, at a certain point, I wondered if what was coming in a Method was equal to a constant of the Enum, something like that:
public void teste( TipoAlteracaoValor valor ) {
if( valor.equals(TipoAlteracaoValor.ALTERACAO_VALOR) ) {
System.out.println( "é Alteração ");
}
//Deveria ser assim:
if( TipoAlteracaoValor.ALTERACAO_VALOR.equals(valor)) {
System.out.println("é Alteração" );
}
}
But I was told that this is wrong to do, it is right to compare the Constant of the Enum with the value that is coming from the parameter. However I don’t remember... what is the technical reason for using this comparison?
Nothing but an additional check
valor != null
do not resolve (with the disadvantage of you having to make this additional check :)– Piovezan
Uhum. I still prefer it without checking, it’s one if less. : 3
– uaiHebert