Java

Explore More Methods in the java.lang.Math Package

The java.lang.Math package is very powerful. Understand more methods in this package that are related to the Euler’s number named e. public class MathMethods{ public static void main(String args[]) { MathMethods mathMethods = new MathMethods(); mathMethods.proceed(); } private void proceed() { //The methid Math.exp returns e^x, where x is the argument

How to Trigger a Synchronous GET Request

For triggering a synchronous GET request we can rely on HTTP Client API as follows: HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(“https://reqres.in/api/users/2”)) .build();HttpResponse response = client.send(request, BodyHandlers.ofString());

Simplifying Null Check in Java

To avoid null exceptions, we usually validate against null and emptiness as shown: if(object != null && !object.equals(“”)) {} We can simplify this as below: if(!””.equals(object)){}

Automation of Tasks

Automation of tasks is a good concept. Consider the example below in which you create a task and schedule it at your convenience to execute the needed actions. There are other types of scheduling as well. Explore them to know more. import java.util.Timer;import java.util.TimerTask;public class TaskScheduling{ public static void main(String

Terminating the Current Java Runtime Programmatically

You can terminate the Java runtime that you are in programmatically. The Java Runtime class provides a method halt(argument) to support this. Of course, caution is advised when using this method to terminate an applications. public class TerminatingJavaRuntime{ public static void main(String args[]) { TerminatingJavaRuntime terminatingJavaRuntime = new TerminatingJavaRuntime(); terminatingJavaRuntime.proceed();

Get the Abstract Methods of a Class

With the Java Reflection API, we can isolate the abstract methods from a class via the following snippet of code: List abstractMethods = new ArrayList();Class clazz = Foo.class;Method[] methods = clazz.getDeclaredMethods();for (Method method : methods) { if (Modifier.isAbstract(method.getModifiers())) { abstractMethods.add(method); }}

Finding the Current Flush Mode for the JPA EntityManager

Knowing the current flush mode can be done as follows: // via EntityManagerentityManager.getFlushMode();// in Hibernate JPA, via Session(entityManager.unwrap(Session.class)).getFlushMode();// starting with Hibernate 5.2(entityManager.unwrap(Session.class)).getHibernateFlushMode();