title: Overflow and Underflow in Java Float and Double Data Types
body: <p>I have created the following code to test Float and Double Java numeric data types for underflow and overflow:</p>
  
  <pre><code>// Float Overflow &amp; Underflow
  float floatTest = Float.MAX_VALUE;
  floatTest++;
  out.println("Float Overflow: " + Float.MAX_VALUE + " + 1 = " + floatTest);
  floatTest = Float.MIN_VALUE;
  floatTest--;
  out.println("Float Underflow: " + Float.MIN_VALUE + " - 1 = " + floatTest);
  out.println("");
  
  // Double Overflow &amp; Underflow
  double doubleTest = Double.MAX_VALUE;
  doubleTest++;
  out.println("Double Overflow: " + Double.MAX_VALUE + " + 1 = " + doubleTest);
  doubleTest = Double.MIN_VALUE;
  doubleTest--;
  out.println("Double Underflow: " + Double.MIN_VALUE + " - 1 = " + doubleTest);
  out.println("");
  </code></pre>
  
  <p>Can someone explain the weird values I see in the result:
  <a href="https://i.stack.imgur.com/rFSBg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rFSBg.jpg" alt="enter image description here"></a></p>
  
  <p>When I do the similar test (code below) with byte, short, int and long:</p>
  
  <pre><code>// BYTE Overflow &amp; Underflow
  byte byteTest = Byte.MAX_VALUE;
  byteTest++;
  out.println("Byte Overflow: " + Byte.MAX_VALUE + " + 1 = " + byteTest);
  byteTest = Byte.MIN_VALUE;
  byteTest--;
  out.println("Byte Underflow: " + Byte.MIN_VALUE + " - 1 = " + byteTest);
  out.println("");
  
  // SHORT Overflow &amp; Underflow
  short shortTest = Short.MAX_VALUE;
  shortTest++;
  out.println("Short Overflow: " + Short.MAX_VALUE + " + 1 = " + shortTest);
  shortTest = Short.MIN_VALUE;
  shortTest--;
  out.println("Short Underflow: " + Short.MIN_VALUE + " - 1 = " + shortTest);
  out.println("");
  
  // INTEGER Overflow &amp; Underflow
  int intTest = Integer.MAX_VALUE;
  intTest++;
  out.println("Integer Overflow: " + Integer.MAX_VALUE + " + 1 = " + intTest);
  intTest = Integer.MIN_VALUE;
  intTest--;
  out.println("Integer Underflow: " + Integer.MIN_VALUE + " - 1 = " + intTest);
  out.println("");
  
  // LONG Overflow &amp; Underflow
  long longTest = Long.MAX_VALUE;
  longTest++;
  out.println("Long Overflow: " + Long.MAX_VALUE + " + 1 = " + longTest);
  longTest = Long.MIN_VALUE;
  longTest--;
  out.println("Long Underflow: " + Long.MIN_VALUE + " - 1 = " + longTest);
  out.println("");
  </code></pre>
  
  <p>The results look as expected:</p>
  
  <p><a href="https://i.stack.imgur.com/NQzZS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQzZS.jpg" alt="enter image description here"></a></p>
  
  <p>Can someone explain overflow and underflow in Java float and double and why am I seeing above results?</p>