He leído este post negativo y cero positivo .
A mi entender, el siguiente código debería dar true y true como salida.
Sin embargo, está dando falsey truecomo salida.
Estoy comparando el cero negativo con un cero positivo.
public class Test {
public static void main(String[] args) {
float f = 0;
float f2 = -f;
Float F = new Float(f);
Float F1 = new Float(f2);
System.out.println(F1.equals(F));
int i = 0;
int i2 = -i;
Integer I = new Integer(i);
Integer I1 = new Integer(i2);
System.out.println(I1.equals(I));
}
}
¿Por qué tenemos un comportamiento diferente para 0's para Integery Float?
iy i2son exactamente lo mismo Luego, cuando crea nuevos Integercorreos electrónicos, ambos envuelven exactamente el mismo valor. I1.equals(I)será verdad
int i = Integer.MIN_VALUE, i2 = -i;...
newpara los tipos de envoltura aquí. Solo use, por ejemploInteger i = 0, i2 = -i; System.out.println(i.equals(i2)); Float f1 = 0f, f2 = -f1; System.out.println(f1.equals(f2));