devxlogo

Java

Understanding the FindAny Method

Learn how to use the findAny method in java.util.Stream. This returns an Optional describing an element, or an empty Optional if Stream is empty. Also, remember that the same stream

Escape Characters in Regular Expressions

In Java, characters as  cannot appear in a regular expression without being escaped. For this, rely on the Pattern.quote() method as in the following example: public static boolean contains(String t1, String

Finding the Java Version Programatically

import java.util.*;public class FindJavaVersion{ public static void main(String args[]) { FindJavaVersion findJavaVersion = new FindJavaVersion(); findJavaVersion.printDetails(); } private void printDetails() { Properties systemProperties = System.getProperties(); System.out.println(“Java version: ” + systemProperties.get(“java.version”));

Counting Digits in a Given Number

A typical mechanism of converting to a string, traversing and counting has a lot of processing to complete. Instead, the code below shows a combination of methods in Math package

Finding the Available Drive Types in the System

Getting to know the available drive types in the system is essential for certain activities in Java. The following code snippet lists the available drive types in the system: */import

Boost the Switch Statement in JDK 12

Starting with JDK 12, the “switch” statement is extended so that it can be used as either a statement or an expression. A sample: switch (day) { case MONDAY, FRIDAY,

Creating a Specific Date in Java

To create a new date, you will need a Calendar instance and then you can set the Calendar instance to the date you need. Calendar calendar = Calendar.getInstance(); The line

Formatting Output in Java

Proper formatting of output is essential. There are times when the details presented are not formatted. The code snippet below displays details in a formatted manner that is a better