0

It may be a stupid question but as i was going to create a arithmetic exception case i.e 44/0 ... i created this below program

public class Simple{
  public static void main(String args[]){
    float a = 78.0f;
    float b= 0.0f;
    float c= a/b;
    System.out.println(c);
  }
}

Result :

Infinity

But the Positive Infinity is a final static field in the Float Wrapper class. So does that means that every primitive is autoboxed to wrapper class object at runtime? If not , then how came it returned infinity?

GhostCat
  • 137,827
  • 25
  • 176
  • 248

1 Answers1

2

Float.POSITIVE_INFINITY is a static field on the Float class, true. But it is of type float (not Float), so it is itself a primitive. No boxing is happening here.

The fact of the matter is that a float, being an IEEE 754 single-precision floating point number, cannot just hold a range of regular floating-point numbers, but also a few special values, like ±∞ and NaN. And because Java doesn't have special syntax to write these values, they are offered as static fields on the Float class instead.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Aww thanks .. I notice that its type is float but i was confused that how it is going to hold that value.. Thanks much appreciated. will accept your answer in 4 minutes :) – Tarunjeet Singh Salh Jul 20 '18 at 18:56