Comparing integers with Java Snippet, Java IF Table, ...

Hi all,

 

I will compare e.g. two integer columns column1 and column2 with the Java Snipper (simple)  node by using the following code:

if ($column1$ == $column2$)

{return 0;}

else

{return 1;}

 

It return 0 for e.g. if the value of both columns are 100, but it returns 1 if the value of both columns are 140.

Same problem appears if I construct a similar workflow that uses Java IF node to decide whether these values are identical or not.

Who has an explanation or a solution how to change this java code?

Greetings, Frank

 

Hello,

The Oracle/Sun Java VMs cache/intern the first few (starting from 0) java.lang.Integer values, so those are referentially point to the same values and the == returns true for them. I guess 140 is beyond that limit.

You can use either (c_col1 != null && c_col1.equals(c_col2)) || (c_col1 == null && c_col2 == null) or (c_col1 == null && c_col2 == null) || (c_col1 != null && c_col2 != null && c_col1.intValue() == c_col2.intValue())) conditions. (If you know these cannot be missing, c_col1.equals(c_col2) or c_col1.intValue() == c_col2.intValue() would be enough.)

Hope this helps, gabor

Hi,

 

thank you for this explanation. It helped to solve the problem.

 

Frank