eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

Managing numeric data effectively is a key aspect of Java programming, as selecting the appropriate data type can greatly influence performance and accuracy. The float and double data types are two widely used options for handling decimal numbers. Although they share a common purpose, they vary significantly regarding precision, memory requirements, and typical applications.

This article explores these differences in detail, aiming to guide developers in choosing the right type for tasks such as scientific computations, graphics processing, and financial analyses.

2. Key Characteristics and Differences

Java provides two primitive data types for floating-point arithmetic: float and double. Both adhere to the IEEE 754 standard, ensuring consistent behavior across platforms. However, their size, precision, and performance differ significantly.

2.1. Memory Size

The memory size of float and double is a fundamental distinction that directly impacts their storage capabilities and memory consumption.

A float is a 32-bit single-precision floating-point type, occupying four bytes of memory. This compact size makes it well-suited for memory-constrained environments like embedded systems and mobile devices. Additionally, its smaller footprint can help minimize cache misses and boost performance in memory-intensive applications.

In contrast, a double is a 64-bit double-precision floating-point type, requiring eight bytes of memory. While it demands more storage, this larger size allows it to represent values with higher precision and a broader range, making it indispensable for tasks involving complex calculations or large datasets.

To illustrate this difference in memory usage, we can use the following test case:

@Test
public void givenMemorySize_whenComparingFloatAndDouble_thenDoubleRequiresMoreMemory() {
    assertEquals(4, Float.BYTES, "Float should occupy 4 bytes of memory");
    assertEquals(8, Double.BYTES, "Double should occupy 8 bytes of memory");
}

This test confirms that float uses 4 bytes, while double uses 8 bytes, highlighting the difference in memory requirements between these two data types.

2.2. Precision

Precision defines the number of significant digits a data type can represent accurately, and the difference between float and double has notable implications for their usage.

A float can handle up to seven significant decimal digits, meaning values exceeding this precision may be rounded or truncated, potentially introducing inaccuracies in calculations with high precision requirements.

On the other hand, a double supports up to 15 significant decimal digits, making it the preferred choice for applications such as scientific computations and financial modeling, where precision is critical. The greater precision of double helps minimize rounding errors, ensuring more dependable results.

To illustrate this difference, let’s consider the following example:

@Test
public void givenPrecisionLimits_whenExceedingPrecision_thenFloatTruncatesAndDoubleMaintains() {
    float floatValue = 1.123456789f;
    assertEquals(1.1234568f, floatValue, "Float should truncate beyond 7 digits");
    
    double doubleValue = 1.1234567891234566d; // Exceeds 15 digits of precision for double
    assertEquals(1.123456789123457, doubleValue, 1e-15, "Double should round beyond 15 digits");
}

This test demonstrates that values exceeding the precision limit of float are truncated, while double maintains accuracy up to 15 digits and rounds beyond that. The delta of 1e-15 accounts for minor rounding differences in double precision.

2.3. Range

The range of a floating-point data type defines the smallest and largest values it can represent, and the ranges of float and double differ significantly.

A float offers a range of approximately -3.4e-38 to +3.4e+38, which is sufficient for many standard applications. However, this range can fall short in scenarios involving extremely large or small numbers.

In contrast, a double extends this range significantly, from -1.7e-308 to +1.7e+308, making it the preferred choice for applications that demand greater numerical reach, such as scientific simulations or astronomical calculations.

To demonstrate, let’s consider the following test case:

public void givenRangeLimits_whenExceedingBounds_thenFloatUnderflowsAndDoubleHandles() {
    float largeFloat = 3.4e38f;
    assertTrue(largeFloat > 0, "Float should handle large positive values");

    float smallFloat = 1.4e-45f;
    assertTrue(smallFloat > 0, "Float should handle very small positive values");

    double largeDouble = 1.7e308;
    assertTrue(largeDouble > 0, "Double should handle extremely large values");

    double smallDouble = 4.9e-324;
    assertTrue(smallDouble > 0, "Double should handle extremely small positive values");
}

This test highlights how float and double behave near their respective range limits. While float may experience limitations or even underflow to zero for very small values, double accurately represents numbers across a much broader spectrum.

2.4. Performance

Performance considerations for float and double often revolve around their size and precision. float might be slightly faster on some older hardware due to its smaller size, allowing for quicker processing and reduced memory bandwidth usage. This makes it suitable for performance-critical applications where precision is less important.

On modern hardware, the performance difference between float and double is negligible. Most processors are optimized for 64-bit operations, meaning double calculations may execute as quickly as the float.

Therefore, the choice should focus on the precision and range requirements rather than raw performance.

2.5. Summary

The table below highlights the key characteristics of float and double, concisely referencing their distinctions. It serves as a guide to help developers select the appropriate data type based on factors like memory usage, precision, and common applications:

Feature float double
Size 32-bit (4 bytes) 64-bit (8 bytes)
Precision ~7 significant decimal digits ~15 significant decimal digits
Range -3.4e-38 to +3.4e+38 -1.7e-308 to +1.7e+308
Performance Slightly faster on older hardware Comparable on modern hardware

3. Common Pitfalls

When working with float and double, we may encounter several common issues.

Accumulating small rounding errors over multiple calculations can lead to significant inaccuracies, making it essential to consider alternatives like BigDecimal for financial computations where precision is critical. Additionally, using values near the boundaries of float and double may result in overflows or underflows, leading to unexpected results.

For instance, attempting to store a value smaller than the representable float range, such as 1e-50f, can cause an underflow to zero. This is because the smallest normalized float value is approximately 1.4e-45f. Values smaller than this will become denormalized (subnormal) numbers or, if they’re too small to be represented as denormalized values, will underflow to zero.

This behavior is demonstrated in the following test case:

@Test
public void givenUnderflowScenario_whenExceedingFloatRange_thenFloatUnderflowsToZero() {
    float underflowValue = 1.4e-45f / 2; // Smaller than the smallest normalized float value
    assertEquals(0.0f, underflowValue, "Float should underflow to zero for values smaller than the smallest representable number");
}

This test checks that when a value exceeds the lower bound of the float‘s range, it correctly underflows to zero, as expected for values too small to be represented.

4. Conclusion

Choosing between float and double in Java requires understanding the trade-offs in precision, memory usage, and application needs. While float is ideal for memory-constrained and performance-critical environments, double is preferred for applications requiring higher precision and a broader range of values.

By assessing the application’s requirements, we can ensure both efficient and accurate numeric computations.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments