Copyright

Java: Float vs. Double

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: Char Data Type

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Different Data Types
  • 0:59 Float Data Type
  • 1:49 Double Data Type
  • 2:16 Examples of Data Types
  • 3:41 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Is there a need for two similar yet distinct data types in Java? Yes, and the difference between a float and a double is a large one in Java. This lesson explores these two data types and their applications.

Different Data Types

Typically, a double scoop of ice cream is more than a float. The same is true in Java, but on a different scale. Both float and double data types are considered primitive, because they're core to the programming language and require nothing special to use. They're also approximate, non-exact numbers, because they each have limits on the values they'll store. This limit to the number of digits is called precision, or how exact the number is. They aren't always exact and as such can be labeled approximate.

Part of the problem is how the numbers are stored in the computer's memory. The short answer is that they're converted into binary numbers (1s and 0s), and it isn't always possible to store the exact number value with complete precision (down to the very last number). This is the reason that float or double values are not great for banking or currency!

Before we start picking on our otherwise useful data types, let's take a look at each one in terms of how they function.

Float Data Type

The float data type can hold four bytes of memory (32 bits), and store about 7 digits. Float is useful when you have large arrays of floating-point (decimal) numbers. This helps save some memory for these processes. Therefore a number like 3.14159265359 would most likely get rounded to 3.141593. In Java, you can express the number literally, or in scientific notation (e.g. 1.99E02).

Also, you can store the literal value of the float. For example, check out the float value here:


Java Literal Float Example


This tells the computer to store exactly this value. Remember, though, if you start doing math on that value, you'll still run into the approximation issue! We will see more of that later.

Double Data Type

A double data type can hold 8 bytes (64 bits), and store about 15 digits after the decimal point. It's almost double the float, and so we often say that a float is single-point precision, and a double is double-point precision. But remember the issue with precision in float. The same thing happens with a double; things will get rounded or truncated after a certain value.

Let's take a look at some examples of the data types in action so we can see what's going on.

Examples of Data Types

The following is a simple example of how the numbers come out differently. Let's first look at float versus double when it's mostly harmless. For most non-computing applications, the differences between float and double are few. Even a rate of pay, stored to 7 digits, will have little impact when rounded as a float.

The following code accepts input for two numbers (in this case we'll enter the same number twice, to see what happens):


Java Float Double


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support