3

As a simplified example, I have:

public static final int RUN_TYPE = 1;

if(RUN_TYPE == 1)
{

}

This gives me "Comparing identical" warning at the if. How can I get rid of this warning without disabling the "Comparing identical" warning globally?

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
kovacs lorand
  • 741
  • 7
  • 23
  • 6
    This is a very pointless comparison. – Sachin Kainth Mar 22 '13 at 10:17
  • You *could* [suppress all](http://stackoverflow.com/questions/6996631/how-to-specifically-suppress-comparing-identical-expressions-in-eclipse-helios) warnings for that comparison, but the comparison is indeed pointless – jedwards Mar 22 '13 at 10:21
  • 1
    @SachinKainth Think of something like `if (DEBUG) {}` where DEBUG is `public static final boolean DEBUG = true/false;` for example... – assylias Mar 22 '13 at 10:21
  • @Sachin: No it's not. I have to make a choice between 2 values. During development it's faster to hard-code it instead of reading a value from the console every time I run the app. – kovacs lorand Mar 22 '13 at 10:21
  • 4
    @kovacslorand If it's just for debugging/testing purposes, just make the variable non-final. – assylias Mar 22 '13 at 10:22
  • or use unit-testing and pass the desired values – iberbeu Mar 22 '13 at 10:22
  • 1
    The thing is that the line as it is, is literally if (1 == 1) or even if (true) depending on the compiler. Why make it final? – Mikkel Løkke Mar 22 '13 at 10:24
  • @Mikkel: I made it final because the value will never change after the initial assignment. – kovacs lorand Mar 22 '13 at 10:27
  • That means it will always be 1, because you can't reassign it. How could it be 2? – The Cat Mar 22 '13 at 10:37
  • @TheCat: It will be 2 if I type it with my keyboard. It's a hard coded value. It can be 1 or 2, depending on what I choose before running the app. – kovacs lorand Mar 22 '13 at 10:45
  • why not just add it to a properties file or something then? – The Cat Mar 22 '13 at 10:51
  • @kovacslorand But final doesn't mean that the value doesn't change, it means that it can't change. Thus the compiler knows it will always be 1, and making a comparison to 1, (ie an indication of a logic error) and thus the compiler throws a warning. – Mikkel Løkke Mar 22 '13 at 13:33

2 Answers2

3

"Comparing identical" warning is because the compiler knows the value, it knows that the RUNT_TYPE has value 1 and you are comparing it with constant 1 which doesn't makes sense.

if you compare it with another variable, which is not constant, the warning won't be there, because the value cannot be determined until runtime.

Example:

int ANOTHER_INT = 1;
if(RUN_TYPE == ANOTHER_INT {}

This won't give you "Comparing identical" warning.

Abdullah Shaikh
  • 2,567
  • 6
  • 30
  • 43
0

Since you variable is final there is less than little opportunity for it to change. Why do you need to test its value ?

EDIT

... then pass a property on the java command line using the -D flag and read this property in your code e.g.

if ( "WHATEVER".equals(System.getProperty("myproperty", "default_value"))) {
  ...
}
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • It's a hard-coded value on which I choose what the app will do (have two options for the variable: 1 or 2). I could do this by reading a value form the console every time I run the app, but that is tedious. – kovacs lorand Mar 22 '13 at 10:26