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. Introduction

In computer programming, the use case of OR is that it is either a logical construct for boolean logic or a bitwise mathematical operation for manipulating data at the bit level.

The logical operator is used for making decisions based on certain conditions, while the bitwise operator is used for fast binary computation, including IP address masking.

In this tutorial, we’ll learn about the logical and bitwise OR operators, represented by || and | respectively.

2. Use of Logical OR

2.1. How It Works

The logical OR operator works on boolean operands. It returns true when at least one of the operands is trueotherwise, it returns false:

  • true || true = true
  • true || false = true
  • false || true = true
  • false || false = false

2.2. Example

Let’s understand with the help of some boolean variables:

boolean condition1 = true; 
boolean condition2 = true; 
boolean condition3 = false; 
boolean condition4 = false;

When we apply logical OR on two true operands, the result will be true:

boolean result = condition1 || condition2;
assertTrue(result);

When we apply logical OR on one true and one false operand, the result will be true:

boolean result = condition1 || condition3; 
assertTrue(result);

And when we apply logical OR on two false operands, the result will be false:

boolean result = condition3 || condition4; 
assertFalse(result);

When there are multiple operands, the evaluation is effectively performed from left to right. So, the expression condition1 || condition2 || condition3 || condition4 will result in the same logic as:

boolean result1 = condition1 || condition2; 
boolean result2 = result1 || condition3;
boolean finalResult = result2 || condition4;
assertTrue(finalResult);

In practice, though, Java can take a shortcut on the above expression.

3. Short-Circuit

The logical OR operator has a short-circuit behaviour. This means it returns true as soon as one of the operands is evaluated as true, without evaluating the remaining operands.

Let’s consider the following example:

boolean returnAndLog(boolean value) { 
    System.out.println("Returning " + value); 
    return value; 
} 

if (returnAndLog(true) || returnAndLog(false)) { 
} 

Output:
Returning true

if (returnAndLog(false) || returnAndLog(true)) { 
}

Output:
Returning false
Returning true

Here we see that the second logical condition isn’t evaluated if an earlier condition is true.

We should note that this can lead to unexpected results if any of the methods called have a side effect. We get a different result if we rewrite the first example to capture the boolean values before the if statement:

boolean result1 = returnAndLog(true);
boolean result2 = returnAndLog(false);

if (result1 || result2) {
}

Output:
Returning true
Returning false

4. Use of Bitwise OR

4.1. How It Works

The bitwise OR is a binary operator and it evaluates OR of each corresponding bit of two integer operands. It returns 1 if at least one of the bits is 1, otherwise, it returns 0. Also, this operator always evaluates both the operands:

  • 1 | 1 = 1
  • 1 | 0 = 1
  • 0 | 1 = 1
  • 0 | 0 = 0

So, when we apply the bitwise OR on two integers, the result will be a new integer.

4.2. Example

Let’s consider an example:

int four = 4; //0100 = 4
int three = 3; //0011 = 3
int fourORthree = four | three;
assertEquals(7, fourORthree); // 0111 = 7

Now, we’ll look at how the above operation works.

First, each integer is converted to its binary representation:

  • The binary representation of 4 is 0100
  • The binary representation of 3 is 0011

And then, the bitwise OR of the respective bits are evaluated to arrive at the binary representation that represents the final result:

0100
0011
----
0111

Now, 0111, when converted back to its decimal representation, will give us the final result: the integer 7.

When there are multiple operands, the evaluation is done from left to right. So, the expression 1 | 2 | 3 | 4 will be evaluated as:

int result1 = 1 | 2; 
int result2 = result1 | 3;
int finalResult = result2 | 4;
assertEquals(finalResult,7);

5. Compatible Types

In this section, we’ll look at the data types that these operators are compatible with.

5.1. Logical OR

The logical OR operator can only be used with boolean operands. And, using it with integer operands results in a compilation error:

boolean result = 1 || 2;

Compilation error: Operator '||' cannot be applied to 'int', 'int

5.2. Bitwise OR

Along with integer operands, the bitwise OR can also be used with boolean operands. It returns true if at least one of the operands is true, otherwise, it returns false.

Let’s understand with the help of some boolean variables in an example:

boolean condition1 = true;
boolean condition2 = true;
boolean condition3 = false;
boolean condition4 = false;
 
boolean condition1_OR_condition2 = condition1 | condition2;
assertTrue(condition1_OR_condition2);

boolean condition1_OR_condition3 = condition1 | condition3;
assertTrue(condition1_OR_condition3);

boolean condition3_OR_condition4 = condition3 | condition4;
assertFalse(condition3_OR_condition4);

6. Precedence

Let’s review the precedence of logical and bitwise OR operator, among other operators:

  • Operators with higher precedence: ++ –– * + – / >> << > < == !=
  • Bitwise AND: &
  • Bitwise OR: |
  • Logical AND: &&
  • Logical OR: ||
  • Operators with lower precedence: ? : = += -= *= /= >>= <<=

A quick example will help us understand this better:

boolean result = 2 + 4 == 5 || 3 < 5;
assertTrue(result);

Considering the low precedence of the logical OR operator, the expression above will evaluate to:

  • ((2+4) == 5) || (3 < 5)
  • And then, (6 == 5) || (3 < 5)
  • And then, false || true

This makes the result true.

Now, consider another example with a bitwise OR operator:

int result = 1 + 2 | 5 - 1;
assertEquals(7, result);

The expression above will evaluate to:

  • (1+2) | (5-1)
  • And then, 3 | 4

Hence, the result will be 7.

7. Conclusion

In this article, we learned about using logical and bitwise OR operators on boolean and integer operands.

We also looked at the major difference between the two operators and their precedence among other operators.

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)
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments