Bad attempt to calculate absolute value

        int server = (Math.abs(q.hashCode()) % solrLoadBalanceNumOfServers) + 1;

Jenkins warns me with this note:

Bad attempt to calculate the absolute value of a signed 32-bit hash code

This code generates a hash code and then calculates the absolute value of this hash code. If the hash code is equal Integer.MIN_VALUE, then the result will be negative (since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE).

One of the lines 2 ^ 32 has a hash code Integer.MIN_VALUE, including "polygenic greases" "GydZG_" and "DESIGN OF WORKS".

Does anyone know why I received this warning for this absolute value of the calculations?

+4
source share
2 answers

A warning in itself explains why you receive a warning.

Math.abs() -. -2,147,483,648 2,147,483,647. 2,147,483,647, Math.abs(-2,147,483,648) -2,147,483,648, 2 147 483 648. - -2,147,483,648, server , .

, Math.abs().

int server = Math.abs(q.hashCode() % solrLoadBalanceNumOfServers) + 1;
+13

, , FindBugs (), . / .

Integer.MIN_VALUE = - (2 ^ 31) Integer.MAX_VALUE = 2 ^ 31-1. , Integer.MIN_VALUE Integer.MAX_VALUE. , .

+2

Source: https://habr.com/ru/post/1538918/


All Articles