How to Prevent java.lang.OutOfMemoryError in Java

Encountering a java.lang.OutOfMemoryError can be frustrating, but we can take several practical steps to prevent it. In this article, we explain what causes this error, provide real world examples, and discuss effective techniques to manage memory in our Java applications. Table of contents Understanding java.lang.OutOfMemoryError Common Causes and Prevention Techniques 1. Optimize Heap Size 2. …

Read more

Top 10 Java Frameworks to Watch in 2025

Java remains one of the most powerful and versatile programming languages, widely used in enterprise applications, web development, microservices, and cloud-based solutions. As technology evolves, new frameworks continue to emerge, improving development efficiency, scalability, and security. In this article, we explore the top 10 Java frameworks to watch in 2025. Java Frameworks 2025 Framework Microservices …

Read more

Top 10 Java Application Servers for 2025

Java application servers play a crucial role in deploying and managing Java-based applications. They provide essential features such as security, transaction management, scalability, and performance optimization. In this article, we explore the top 10 Java application servers that developers and enterprises commonly use. Java Application Servers Server Jakarta EE Support Cloud-Native Performance Scalability Use Case …

Read more

Zip4j – Java Library for ZIP Files (With Examples)

In this tutorial, we show how to use the Zip4j library for handling ZIP files like adding files and directories, updating existing ZIPs, password protection, and encryption. Table of Content: 1. Adding Zip4j Dependency 2. Creating a ZIP File and Adding Files 3. Adding a Directory to ZIP 4. Adding Files to an Existing ZIP …

Read more

Java – Swap Two Variables Without a Temp Variable

Swapping two variables is a common task in programming. Traditionally, we use a temporary variable to hold one value while swapping, but Java allows us to swap two variables without using a temp variable. In this tutorial, we will explore different ways to achieve this in Java with real-world examples. Table of contents: 1. Swapping …

Read more

Pretty Print JSON with JSON.simple

This article shows how to pretty print JSON with JSON.simple. Table of contents: 1. Setup JSON.simple 2. Default Compact Print JSON 3. Pretty Print JSON with JSON.simple 4. Write Pretty Print JSON to a File 5. Download Source Code 6. References P.S Tested with json-simple 4.0.1 1. Setup JSON.simple pom.xml <dependency> <groupId>com.github.cliftonlabs</groupId> <artifactId>json-simple</artifactId> <version>4.0.1</version> </dependency> …

Read more

How to exclude fields in Gson

In GSON, we can use the transient keyword, ExclusionStrategy, and @Expose to exclude fields during the JSON serialization or deserialization process. Table of contents: 1. Setup Google Gson 2. Using the "transient" Keyword 3. Using the "ExclusionStrategy" 4. Using the "ExclusionStrategy" and custom annotation 5. Using the @Expose annotation 6. Download Source Code 7. References …

Read more

Jackson Custom Serializer and Deserializer Examples

This article shows how to create a Jackson custom serializer and deserializer to parse JSON data that contains a LocalDate type. The Jackson custom serializer or deserializer is useful when we want to process a specific format that is not the default. Table of contents: 1. Setup Jackson 2. Jackson Custom Serializer for LocalDate 3. …

Read more

How to pretty print JSON using Moshi

This article shows how to use Moshi’s JsonAdaptor indent method to enable the pretty print JSON. Table of contents: 1. Download Moshi 2. Compact print JSON (Default) 3. Pretty print JSON using Moshi 4. Download Source Code 5. References P.S Tested with Moshi 1.15.1 1. Download Moshi Declare moshi in the pom.xml. pom.xml <dependency> <groupId>com.squareup.moshi</groupId> …

Read more

Moshi – java.math.BigDecimal requires explicit JsonAdapter to be registered

Moshi does not have built-in adapters to process types like java.math.BigDecimal; we need to manually provide a custom adapter to handle the BigDecimal type. Table of contents: 1. Download Moshi 2. BigDecimal requires explicit JsonAdapter 3. Make Moshi supports BigDecimal 3.1 Define BigDecimal Adapter 3.2 Register the Adapter with Moshi 4. Download Source Code 5. …

Read more

Jackson and Lombok examples

This article shows how to use Jackson and Lombok together. Table of contents: 1. Download Jackson and Lombok 2. Model with Lombok annotations 3. Parse JSON using Jackson 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 and Lombok 1.18.32 1. Download Jackson and Lombok Declares jackson-databind and lombok at pom.xml. pom.xml <dependency> …

Read more

Write JSON to a file with Jackson

This article shows you write JSON to a file with Jackson. Table of contents: 1. Download Jackson 2. Define a Java Object 3. Write JSON to a File 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Declare jackson-databind in the pom.xml. pom.xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version> </dependency> 2. Define …

Read more

How to ignore a field with Jackson

In Jackson, we can use @JsonIgnore to ignore a single field, @JsonIgnoreProperties to ignore multiple fields and @JsonIgnoreType to ignore a specified type during JSON serialization and deserialization. Table of contents: 1. Setup Jackson 2. Jackson @JsonIgnore – Ignores a field 3. Jackson @JsonIgnoreProperties – Ignores multiple fields 4. Jackson @JsonIgnoreType – Ignores a specified …

Read more

Jackson Java 8 date/time type `java.time.LocalDate` not supported by default

This article shows how Jackson can support the Java 8 date time APIs or JSR-310. Table of contents: 1. Download Jackson 2. Jackson does not support Java 8 date time APIs 3. Make Jackson support Java 8 date time APIs or JSR310 3.1 jackson-datatype-jsr310 3.1 Register JavaTimeModule() 4. Download Source Code 5. References P.S Tested …

Read more

Java String compareTo() examples

The Java String compareTo() method compares two strings lexicographically (alphabetical order). It returns a positive number, negative number, or zero. s1.compareTo(s2) if s1 < s2, it returns negative number. if s1 > s2, it returns positive number. if s1 == s2, it returns 0. Table of contents 1. "a".compareTo("c"), negative integer 2. "c".compareTo("a"), positive integer …

Read more

How to capitalize the first letter of a String in Java

In Java, we can use substring(0, 1).toUpperCase() + str.substring(1) to make the first letter of a String as a capital letter (uppercase letter) String str = "mkyong"; String str1 = str.substring(0, 1).toUpperCase(); // first letter = M String str2 = str.substring(1); // after 1 letter = kyong String result = str.substring(0, 1).toUpperCase() + str.substring(1); // …

Read more

How to convert String to Integer in Java

In Java, we can use Integer.valueOf(String) to convert a String to an Integer object; For unparsable String, it throws NumberFormatException. Integer.valueOf("1"); // ok Integer.valueOf("+1"); // ok, result = 1 Integer.valueOf("-1"); // ok, result = -1 Integer.valueOf("100"); // ok Integer.valueOf(" 1"); // NumberFormatException (contains space) Integer.valueOf("1 "); // NumberFormatException (contains space) Integer.valueOf("2147483648"); // NumberFormatException (Integer max …

Read more

What is new in Java 17

Java 17 is a long-term support (LTS) release, reached General Availability on 14 September 2021, download Java 17 here. Java 17 has 14 JEP items. 1. JEP 306: Restore Always-Strict Floating-Point Semantics 2. JEP 356: Enhanced Pseudo-Random Number Generators 3. JEP 382: New macOS Rendering Pipeline 4. JEP 391: macOS/AArch64 Port 5. JEP 398: Deprecate …

Read more

Convert object to byte[] in Java

This article shows how to convert an object to byte[] or byte array and vice versa in Java. 1. Convert an object to byte[] The below example show how to use ByteArrayOutputStream and ObjectOutputStream to convert an object to byte[]. // Convert object to byte[] public static byte[] convertObjectToBytes(Object obj) { ByteArrayOutputStream boas = new …

Read more