Understanding the Unary Operator in Java

Java uses the symbol tilde (~) to represent the operator. The following examples are clearly illustrative of the use and outcome of this operator. Code sample: public class JavaUnary{public static void main(String args[]){JavaUnary javaUnary = new JavaUnary();javaUnary.proceed();}private void proceed(){System.out.println(“Unary (~) of 55: ” + ~ 55 ); //Output is 1

Validate Field Length

See how to validate the length of a field in Java via javax.validation.constraints.Size. We can do it as follows: @[email protected](min=3, message=”Name must be at least 3 characters long”)private String name;

Using the FileTime Class in Java

FileTime is a class in Java that helps you retrieve time-related information regarding files. One such method that we will see below is to get the modified time of a file in FileTime. Code snippet: import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.attribute.FileTime;import;public class FileTimeEx{ public static void main(String args[]) throws IOException

Working with Long Integer Literals

Literals of type long are expressed by adding an L suffix. For example: 0L // The decimal number zero (type ‘long’)1L // The decimal number one (type ‘long’)2147483648L // The value of Integer.MAX_VALUE + 1long big = 2147483648; // ERRORlong big2 = 2147483648L; // OK

Set Hibernate Batching Size

Setting the Hibernate batching size can be accomplished via hibernate.jdbc.batch_size. In Spring, we need to add in application properties per the following setting (the recommended batch size is between 5 and 30):

How to Check if Index Is in Range [0, n)

In order to check whether or not an index is in range [0, n) we can rely on a “if” statement, as below: if (index = n) { … } But, Java 9 comes with the method bjects.checkIndex() that checks if the given index is in range [0, n). This method

Creating an Immutable List via JDK 9

Starting with JDK 9, we can create an immutable list via the suite of List#of(…) methods. For example, an immutable list of 3 elements can be created as follows: import java.util.List;…List names = List.of(“Mike”, “Doru”, “Alin”);

Repeating Text n Times via Stream.generate()

There are several approaches for repeating a string n times. In functional-style, we can rely on Stream.generate(), as in the following example: String result = Stream.generate(() – TEXT) .limit(5) .collect((joining()));

Comparing Two Integers

Before Java 7, this task required a comparator and the compareTo() method. But, beginning with Java 7, is advisable to rely on, which is not exposed to overflow risks of compareTo(): // replace x and y with two intsint result =, y);

No more posts to show