devxlogo

Java

Find the Number of Processors Available

There are abundant resources available in the java.lang package. Learn more about the server/host on which you are working. The Runtime class has numerous functionalities. To learn the number of

Validate Arguments via Google Guava

Google Guava Preconditions are quite useful for validating arguments of methods. Here it is an example: public void setAge(int age) { Preconditions.checkArgument(assert age = 0, “Age cannot be negative”); …}

How to Check Whether a Port is Free

We can check if a given port is free by trying to construct a ServerSocket with the port that needs to be checked: import java.io.*;import java.net.*;public class CheckingIfPortIsFree{ public static

Determine Files Present in a Zip File

A zip file is a compressed file that can hold one or more files. The following code snippet lets us scan the contents of a given zip file. Code snippet:

Add Log4j2 in a Spring Boot Application

Adding Log4j2 in a Spring Boot application can be done in two steps. For Maven, in pom.xml, exclude Spring Boot’s default logging: org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging Further, add Log4j2 dependency:

Working with ArrayList Operations

ArrayList has an overloaded method that accepts a string, as well as an Integer, as an argument. The remove method can remove elements based on the string, along with the

Java Support for the Hijri Calendar

The Hijri calendar system is somewhat different from the Gregorian calendar. This example does not discuss the complete Hijri calendar. This is only to educate you regarding the existence of

Variable Arguments in Java

Moving away from the age old way of fixed arguments, Java supports variable arguments. The code sample below illustrates variable arguments. If the method has multiple arguments, it is mandatory