Reading PDF File Using Java
Baeldung » Java
by baeldung
19h ago
1. Overview Portable Document Format (PDF) is a common file format for documents. It's used to distribute electronic documents that need to preserve their original format. In this tutorial, we'll explore two of the most popular libraries for reading PDF files in Java: Apache PDFBox and iText. 2. Setup We'll use Maven to manage dependencies. Furthermore, we'll add a sample PDF file to the project root directory. The file contains a simple phrase “Hello World!”. Next, we'll read the sample PDF file and test the extracted text against an expected result. 3. Using Apache PDFBox Apache PDFBox is a ..read more
Visit website
Java Weekly, Issue 475
Baeldung » Java
by baeldung
1d ago
1. Spring and Java >> Java 21: Performance Improvements Revealed [minborgsjavapot.blogspot.com] In Java 21, old code might run significantly faster due to recent internal performance optimizations made in the Java Core Libraries. Always good to see performance getting better. >> Measuring compact strings memory savings [javaspecialists.eu] How much memory would we save by replacing char[] with byte[] in Strings? Let's see the impact of JEP 254 in action. >> Mockito 5 Supports Mocking Constructors, Static Methods and Final Classes Out of the ..read more
Visit website
Return Absolute Difference of Two Integers in Java
Baeldung » Java
by Kai Yuan
3d ago
1. Overview In this tutorial, we'll explore how to get the absolute difference between two given integers. 2. Using the Math.abs() Method The problem is pretty straightforward. Let's understand it quickly with a few examples: num1=3, num2=4: absDiff=1 num1=3, num2=-4: absDiff=7 num1=-3, num2=-4: absDiff=1 Looking at the examples above, given two integers, num1 and num2, the result is the absolute value of (num1 – num2). Further, Java standard library has provided the Math.abs() method to return the absolute value. Therefore, we can easily translate the calculation into Java code: int absDif ..read more
Visit website
Initialize an ArrayList with Zeroes or Null in Java
Baeldung » Java
by baeldung
3d ago
1. Overview In this tutorial, we'll explore different ways to initialize a Java ArrayList with all values null or zero. We can also play with the initializations as we like and initialize the lists with different numerical values ​​or objects. 2. Using for Loop When thinking about the problem of initializing the ArrayList with a desired value or object, the first solution that comes to our mind is using a simple for loop. And rightfully so, this is a straightforward and viable solution: ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i< 10; i++) { arrayLis ..read more
Visit website
Memory Types in JVM
Baeldung » Java
by Ana Peterlić
3d ago
1. Overview In this short tutorial, we’ll have a quick overview of the memory types in the Java Virtual Machine (JVM). The JVM uses different types of memory for different purposes, each with its own characteristics and behaviors. Understanding the different types of memory in JVM is important for designing efficient and stable applications. 2. Heap Memory When the JVM starts up, it creates the heap memory. This memory type represents a crucial component of the JVM as it stores all the objects created by the application. The size of the memory may increase or decrease while the application run ..read more
Visit website
Determine if a Class Implements an Interface in Java
Baeldung » Java
by baeldung
5d ago
1. Overview In this tutorial, we'll take a look at several ways to determine if an object or a class implements a specific interface. 2. Using Java Reflection API The Java Reflection API provides several ways to check whether an object or a class implements an interface. Using this API prevents adding a third-party library to our project. 2.1. The Data Model The Java Runtime Environment(JRE) provides some ways to retrieve the implemented interfaces of a class. First, let's define a model with some interfaces and classes. For this example, we'll define an interface MasterInterface and two sub-i ..read more
Visit website
Stream to Iterable in Java
Baeldung » Java
by baeldung
6d ago
1. Introduction The Java Streams API was introduced in Java 8 and provides functionalities for processing sequences of elements. Streams API supports chaining operations on a Collection of objects in a pipeline in order to produce the desired outcome. In this tutorial, we'll look into ways of using a Stream as an Iterable. 2. Iterable and Iterator Iterable<T> is an interface available since Java 1.5. A class implementing this interface allows the object of the class to be the target of the for-each loop statement. The implementing class doesn't store any information about its iterat ..read more
Visit website
Generate Unit Test Data in Java Using Instancio
Baeldung » Java
by baeldung
6d ago
1. Overview Setting up data in unit tests is typically a manual process involving many boilerplate code. This is especially true when testing complex classes that contain many fields, relationships, and collections. What's more, the values themselves are often unimportant. Instead, what we usually need is the presence of a value. This is typically expressed with code like person.setName(“test name”). In this tutorial, we'll look at how Instancio can help generate unit test data by creating fully-populated objects. We'll cover how objects can be created, customized, and reproduced in case of te ..read more
Visit website
Check if an Integer Value is null or Zero in Java
Baeldung » Java
by Kai Yuan
1w ago
1. Overview In this quick tutorial, we'll learn a few different ways to check if a given Integer instance's value is null or zero. For simplicity, we're going to use unit test assertions to verify if each approach works as expected. So, next, let's see them in action. 2. Using the Standard Way Using the logical OR operator could be the first idea to perform the check. It simply checks if the given Integer number is null or zero. Let's create a method to implement this check for easier verification: public static boolean usingStandardWay(Integer num) { return num == null || num == 0; } Th ..read more
Visit website
HTTP Interface in Spring 6
Baeldung » Java
by Daniel Strmecki
1w ago
1. Overview The Spring Framework release 6, as well as Spring Boot version 3, enables us to define declarative HTTP services using Java interfaces. The approach is inspired by popular HTTP client libraries like Feign and is similar to how we define repositories in Spring Data. In this tutorial, we'll first look at how to define an HTTP interface. Then, we’ll check the available exchange method annotations, as well as the supported method parameters and return values. Next, we’ll see how to create an actual HTTP interface instance, a proxy client that performs the declared HTTP exchanges. Final ..read more
Visit website

Follow Baeldung » Java on Feedspot

Continue with Google
OR