java

Iteration – a Stream Generator for Recursive Minds

Iteration – a Stream Generator for Recursive Minds

The Chores For anyone having been in programming a couple of years, chances are you've run into an interview question or an assignment like iterator for binary tree. There are variants like for the "pre-order", "post-order" or "in-order" traversal; or sometimes you got a n-ary tree instead of a bnary tree. A reason this kind of question is used in interviews is because they require some meticulous attention to details, to make sure you've considered all edge cases etc. Another familiar concept that are sometimes discussed: how do you create a Stream<Integer> representing the (infinite) Fibonacci sequence? The one thing…
Read More
Understanding @Primary in Spring

Understanding @Primary in Spring

If you read my post about the @Qualifier annotation, you have noticed that defining two beans of the same type can be a challenge. By distinguishing it with a qualifier name, @Qualifier helps Spring determine which bean to inject. The @Primary annotation will help Spring decide which of those same types of beans it should pick primarily. For the bean annotated with the @Primary annotation, the @Qualifier will not be necessary. It is only required for the other bean. Example 1: Using @primary and @Qualifier in Spring Let’s see the example in practice: public interface GreetingService { String greet(); }…
Read More
More on Exception Handling

More on Exception Handling

A handler for an exception is found by propagating the exception backward through a chain of method calls, starting from the current method. Java’s exception-handling model is based on three operations: declaring an exception, throwing an exception, and catching an exception, as shown in figure below. Declaring Exceptions In Java, the statement currently being executed belongs to a method. The Java interpreter invokes the main method to start executing a program. Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. Because system errors and runtime errors can happen to any code,…
Read More
3 Ways to Use the @Lazy Annotation in Spring

3 Ways to Use the @Lazy Annotation in Spring

Does your Spring application take too long to start? Maybe this annotation could help you. This annotation indicates to Spring that a bean needs to be lazily initiated. Spring will not create this bean at the start of the application. Instead, it will create it only when the bean is requested for the first time. This allows Spring start-up to be faster. However, the first interaction with the bean can be slow because of the time required to create and inject the bean at runtime. You can use this annotation directly in the bean class (beans annotated with @Component and…
Read More
The BigInteger and BigDecimal Classes

The BigInteger and BigDecimal Classes

The BigInteger and BigDecimal classes can be used to represent integers or decimal numbers of any size and precision. If you need to compute with very large integers or high-precision floating-point values, you can use the BigInteger and BigDecimal classes in the java.math package. Both are immutable. The largest integer of the long type is Long.MAX_VALUE (i.e., 9223372036854775807). An instance of BigInteger can represent an integer of any size. You can use new BigInteger(String) and new BigDecimal(String) to create an instance of BigInteger and BigDecimal, use the add, subtract, multiply, divide, and remainder methods to perform arithmetic operations, and use…
Read More
Mastering the Strategy Pattern: A Real-World Example in E-commerce Shipping with Java

Mastering the Strategy Pattern: A Real-World Example in E-commerce Shipping with Java

Introduction:In the previous article, we explored the importance of design patterns in software development and how they provide proven solutions to common problems. We discussed how choosing the right pattern is like selecting the appropriate tool from your toolbox. In this article, we'll dive deeper into the Strategy pattern and provide a practical, real-world example of its implementation using Java. What is the Strategy Pattern?The Strategy pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. It lets the algorithm vary independently from clients that use it. The…
Read More
No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.