15 Aralık 2019 Pazar

Float Sınıfı - Immutable

Giriş
Bu sınıf immutable. Açıklaması şöyle.
All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.
compare metodu
Açıklaması şöyle.
If you read the Javadoc of Float.compare() they talk about "numerically equal". That means that values that represent the same theoretical number, but are differently encoded, are considered equal. Examples are 0.0 == -0.0 and subnormal numbers.
Metodun içi şöyle. Yani sadece bitlere göre karşılaştırma yapılıyor. Mantıksal eşitlik kontrolü yapılmıyor. Dolayısıyla compare(0.0f, -0.0f) çağrısının sonucu 1 çıkar.
public static int compare(float f1, float f2) {
  if (f1 < f2)
    return -1;           // Neither val is NaN, thisVal is smaller
  if (f1 > f2)
    return 1;            // Neither val is NaN, thisVal is larger

  // Cannot use floatToRawIntBits because of possibility of NaNs.
  int thisBits    = Float.floatToIntBits(f1);
  int anotherBits = Float.floatToIntBits(f2);

  return (thisBits == anotherBits ?  0 : // Values are equal
         (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
          1));                          // (0.0, -0.0) or (NaN, !NaN)
}
Örnek
Şöyle yaparız.
Collections.sort(employeeList, new Comparator<Emp>() {
    @Override
    public int compare(Emp t, Emp t1) {
        return Float.compare(t1.getExperience(), t.getExperience());
    }
});
equals metodu
Açıklaması şöyle.
there are two exceptions:
If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false
Örnek
Elimizde şöyle bir kod olsun.
float f = 0;
float f2 = -f;
Float F = new Float(f);
Float F1 = new Float(f2);
System.out.println(F1.equals(F)); //false

int i = 0;
int i2 = -i;
Integer I = new Integer(i);
Integer I1 = new Integer(i2);
System.out.println(I1.equals(I)); //true
Açıklaması şöyle.
Ints and floats are pretty different beasts in Java. Ints are encoded as two's complement, which has a single 0 value. Floats use IEEE 754 (the 32-bit variant for floats, and 64-bit for doubles). IEEE 754 is somewhat complex, but for purpose of this answer, you just need to know that it has three sections, the first of which is a sign bit. That means for any float, there's a positive and negative variant¹. That includes 0, so floats actually have two "zero" values, +0 and -0.
...
When you compare float primitives (and doubles), Java treats +0 and -0 as equal. But when you box them, Java treats them separately, as described in Float#equals. This lets the equals method be consistent with their hashCode implementation (as well as compareTo), which just uses the bits of the float (including that signed value) and shoves them as-is into an int.
floatToIntBits metodu
Şöyle yaparız.
Float.floatToIntBits(a)
parseFloat metodu
Şöyle yaparız.
string str = ...;
Float f = Float.parseFloat (str);

Hiç yorum yok:

Yorum Gönder