From the course: Oracle Java Certification: 1. Data Types

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Overflow and underflow

Overflow and underflow - Java Tutorial

From the course: Oracle Java Certification: 1. Data Types

Start my 1-month free trial

Overflow and underflow

- [Instructor] What happens when we assign primitive types values that are out of the range of values the types can represent? There are two possible outcomes. Overflow and underflow. Neither of these are terrific outcomes. How can we define overflow? It is basically when Java throws away information when the data cannot fit in a data type resulting in inaccurate representation of input. Imagine a counter that can contain three digits. So, the max number is 999. Once you go past 999, it will reset itself to zero, a very inaccurate representation of what you've actually counted. That is overflow. So, then what's underflow? Imagine you have a number 1.0535. However, your container can only accommodate four significant digits. So, it ends up storing 1.054. This might be okay but imagine you have to do arithmetic with this number and you're losing precision every time you make a calculation. This cascades into a larger error the more calculations you make. When either of these conditions…

Contents