Azure Spring Apps is a fully managed service from Microsoft
(built in collaboration with VMware), focused on building and
deploying Spring Boot applications on Azure Cloud without
worrying about Kubernetes.
The Enterprise plan comes with some interesting features, such
as commercial Spring runtime support, a 99.95% SLA and some deep
discounts (up to 47%) when you are ready for production.
And, you can participate in a very quick (1 minute) paid user
research from the Java on Azure product team.
Partner – Aegik AB – NPI EA (cat=JPA)
Slow MySQL query performance is all too common. Of course
it is. A good way to go is, naturally, a dedicated profiler that
actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do
things like real-time query performance, focus on most used tables
or most frequent queries, quickly identify performance issues and
basically help you optimize your queries.
Critically, it has very minimal impact on your server's
performance, with most of the profiling work done separately - so
it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL
server, hit the record button, and you'll have results
within minutes:
Accelerate Your Jakarta EE Development with Payara Server!
With best-in-class guides and documentation, Payara
essentially simplifies deployment to diverse
infrastructures.
Beyond that, it provides intelligent insights and actions to
optimize Jakarta EE applications.
The goal is to apply an opinionated approach to get to
what's essential for mission-critical applications - really solid
scalability, availability, security, and long-term support:
The AI Assistant to boost Boost your productivity writing unit
tests - Machinet AI.
AI is all the rage these days, but for very good reason. The
highly practical coding companion, you'll get the power of
AI-assisted coding and automated unit test generation.
Machinet's Unit Test AI Agent utilizes your own project
context to create meaningful unit tests that intelligently aligns
with the behavior of the code.
And, the AI Chat crafts code and fixes errors with ease,
like a helpful sidekick.
Get non-trivial analysis (and trivial, too!) suggested right
inside your IDE or Git platform so you can code smart, create
more value, and stay confident when you push.
Get CodiumAI for free and become part of a community of
over 280,000 developers who are already experiencing improved and
quicker coding.
Looking for the ideal Linux distro for running modern Spring
apps in the cloud?
Meet Alpaquita Linux: lightweight, secure, and powerful
enough to handle heavy workloads.
This distro is specifically designed for running Java
apps. It builds upon Alpine and features significant
enhancements to excel in high-density container environments while
meeting enterprise-grade security standards.
Specifically, the container image size is ~30% smaller than
standard options, and it consumes up to 30% less RAM:
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.
DbSchema is a super-flexible database designer, which can
take you from designing the DB with your team all the way to
safely deploying the schema.
The way it does all of that is by using a design model, a
database-independent image of the schema, which can be shared in a
team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to
interact with the database using diagrams, visually compose
queries, explore the data, generate random data, import data or
build HTML5 database reports.
Slow MySQL query performance is all too common. Of course
it is. A good way to go is, naturally, a dedicated profiler that
actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do
things like real-time query performance, focus on most used tables
or most frequent queries, quickly identify performance issues and
basically help you optimize your queries.
Critically, it has very minimal impact on your server's
performance, with most of the profiling work done separately - so
it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL
server, hit the record button, and you'll have results
within minutes:
Creating PDFs is actually surprisingly hard. When we
first tried, none of the existing PDF libraries met our needs. So
we made DocRaptor for ourselves and later launched it as one
of the first HTML-to-PDF APIs.
We think DocRaptor is the fastest and most scalable way to
make PDFs, especially high-quality or complex PDFs. And as
developers ourselves, we love good documentation, no-account trial
keys, and an easy setup process.
The choice between Double vs. BigDecimalin Java can significantly impact performance as well as the precision and accuracy of floating-point numbers. In this tutorial, we’ll compare and contrast the characteristics, advantages, and disadvantages of these two classes, their use cases, and how to address precision and rounding issues with them.
2. Double
The Double class is a wrapper for the double primitive data type, which is well-suited for general-purpose floating-point arithmetic and works well in many scenarios. However, it has some limitations. The most prominent concern is its limited precision. Due to the nature of binary representation, double numbers might suffer from rounding errors when dealing with decimal fractions.
For example, the double literal 0.1 is not exactly equal to the decimal fraction 0.1, but rather to a slightly larger value:
The BigDecimal class represents an immutable, arbitrary-precision, signed decimal number. It can handle numbers of any size without loss of precision. Imagine having a powerful magnifying glass that can zoom in on any part of the number line, allowing us to work with large or incredibly tiny numbers.
It consists of two parts: an unscaled value (an integer with arbitrary precision), and the scale (which indicates the number of digits after the decimal point). For example, the BigDecimal 3.14 has an unscaled value of 314 and a scale of 2.
The BigDecimal class offers better precision than Double, as it can perform calculations with arbitrary-precision decimals, avoiding the rounding errors arising from Double’s binary representation. That’s because BigDecimal uses integer arithmetic internally, which is more accurate than floating-point arithmetic.
Let’s see some examples of how to use the BigDecimal class in Java:
private BigDecimal bigDecimal1 = new BigDecimal("124567890.0987654321");
private BigDecimal bigDecimal2 = new BigDecimal("987654321.123456789");
@Test
public void givenTwoBigDecimals_whenAdd_thenCorrect() {
BigDecimal expected = new BigDecimal("1112222211.2222222211");
BigDecimal actual = bigDecimal1.add(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenMultiply_thenCorrect() {
BigDecimal expected = new BigDecimal("123030014929277547.5030955772112635269");
BigDecimal actual = bigDecimal1.multiply(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenSubtract_thenCorrect() {
BigDecimal expected = new BigDecimal("-863086431.0246913569");
BigDecimal actual = bigDecimal1.subtract(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenDivide_thenCorrect() {
BigDecimal expected = new BigDecimal("0.13");
BigDecimal actual = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
assertEquals(expected, actual);
}
4. Comparisons and Use Cases
4.1. Comparison Between Double and BigDecimal
Converting from Double to BigDecimal is relatively straightforward. The BigDecimal class provides constructors that accept a double value as a parameter. However, the conversion doesn’t eliminate the precision limitations of a Double. Conversely, converting from BigDecimal to Double can result in data loss and rounding errors when fitting into Double’s constrained scope.
Let’s see both scenarios, that is, converting properly and losing precision:
In terms of speed and range, the utilization of hardware-level floating-point arithmetic in Java’s Double makes it faster than BigDecimal. The Double class covers a broad spectrum, accommodating both large and small numbers. However, its confinement within a 64-bit structure introduces precision limitations, especially for extremely large or small numbers. In contrast, BigDecimal presents a more extensive range of values and better precision across a wide array of values.
There are also differences in memory usage. Java’s Double is more compact, which results in more efficient memory usage. On the other hand, BigDecimal’s strength in arbitrary-precision arithmetic entails higher memory consumption. This can have implications for our application performance and scalability, especially in memory-intensive contexts.
4.2. Use Cases
Double effortlessly interfaces with other numeric types, making it a convenient choice for basic arithmetic. It’s the go-to option when performance is a priority. Double’s speed and memory efficiency make it a solid choice for applications such as graphics and game development, which often involve real-time rendering and complex visual effects. Here, performance is crucial to maintain smooth user experiences.
On the other hand, BigDecimal shines when dealing with monetary calculations, where precision errors can result in substantial financial losses. It’s also a savior in scientific simulations requiring absolute precision. While BigDecimal may be slower and more memory-intensive, the assurance it provides in terms of accuracy can be invaluable in critical scenarios.
As a result, BigDecimal is better suited for tasks in financial applications, scientific simulations, engineering and physical simulations, data analysis and reporting, and other domains where precision is critical.
4.3. Precision and Rounding Considerations
With BigDecimal, we get to decide how many decimal places our calculations will have. This is useful when we need exact decimal calculations as it can store each decimal digit as-is.
We can also choose how rounding happens in our calculations. Different rounding modes have different effects on our results, such as:
UP: increases the number to the next higher value (when we want to ensure that a value is never less than a certain amount)
DOWN: decreases the number to the preceding lower value (when we want to ensure that a value is never greater than a certain amount)
HALF_UP: rounds up if the discarded fraction is greater than 0.5
HALF_DOWN: rounds down if the discarded fraction is less than 0.5
This level of control over rounding and precision is another reason why BigDecimal is better for financial calculations when we need things to be accurate and uniform.
Double introduces chances of tiny errors creeping in because of how computers represent numbers. Representing repeating decimals, like 1/3, can get tricky as they’ll result in an infinite binary expansion.
Simple numbers like 0.1 can get similarly messy when we try to represent them in binary (base 2). We’d get a repeating fraction like 0.00011001100110011… Computers have a limited number of bits to represent these fractions, so they have to round them off at some point. As a result, the stored value isn’t exactly 0.1, and this can lead to tiny errors when we perform calculations.
4.4. Comparison Table
Let’s summarize what we’ve learned about Double vs. BigDecimal in a table:
Aspect
Double
BigDecimal
Precision
Limited
Arbitrary
Range
Broad (both large and small)
Extensive
Memory Usage
Compact
Higher
Performance
Faster
Slower
Use Cases
General purpose
Financial, Scientific
5. Conclusion
In this article, we’ve discussed the nuances between the Java Double and BigDecimal types and the trade-offs between precision and performance when using them.
As usual, the code samples are available over on GitHub.
Partner – Bellsoft – NPI EA (cat = Spring)
Looking for the ideal Linux distro for running modern Spring
apps in the cloud?
Meet Alpaquita Linux: lightweight, secure, and powerful
enough to handle heavy workloads.
This distro is specifically designed for running Java
apps. It builds upon Alpine and features significant
enhancements to excel in high-density container environments while
meeting enterprise-grade security standards.
Specifically, the container image size is ~30% smaller than
standard options, and it consumes up to 30% less RAM:
Creating PDFs is actually surprisingly hard. When we
first tried, none of the existing PDF libraries met our needs. So
we made DocRaptor for ourselves and later launched it as one
of the first HTML-to-PDF APIs.
We think DocRaptor is the fastest and most scalable way to
make PDFs, especially high-quality or complex PDFs. And as
developers ourselves, we love good documentation, no-account trial
keys, and an easy setup process.
Slow MySQL query performance is all too common. Of course
it is.
The Jet Profiler was built entirely for MySQL, so it's
fine-tuned for it and does advanced everything with relaly minimal
impact and no server changes.
Basically, write code that works the way you meant it to.
Partner – Machinet – NPI EA (cat = Testing)
AI is all the rage these days, but for very good reason. The
highly practical coding companion, you'll get the power of
AI-assisted coding and automated unit test generation.
Machinet's Unit Test AI Agent utilizes your own project
context to create meaningful unit tests that intelligently aligns
with the behavior of the code.