How-to articles, tricks, and solutions about JAVA

When do you use Java's @Override annotation and why?

The @Override annotation in Java is used to indicate that a method is intended to override a method declared in a superclass or interface.

How to use Jackson to deserialise an array of objects

Jackson is a powerful Java library for processing JSON data. You can use Jackson to deserialize an array of objects by following these steps:

How to convert a byte array to a hex string in Java?

To convert a byte array to a hexadecimal string in Java, you can use the following method:

How to extract a substring using regex

To extract a substring from a string using a regular expression in Java, you can use the following steps:

How to get the first element of the List or Set?

To get the first element of a List or Set in Java, you can use the following methods:

string to string array conversion in java

There are several ways to convert a string to a string array in Java. Here are a few options:

Short form for Java if statement

In Java, you can use the ternary operator (also known as the conditional operator) to create a short form for an if-else statement.

How do I convert a Map to List in Java?

You can use the entrySet() method of the Map interface to get a Set view of the mappings contained in the map, and then create a List from this set using the new

Unfortunately MyApp has stopped. How can I solve this?

If you are seeing the error "Unfortunately, MyApp has stopped," it means that your Android app has crashed. This can be caused by a number of factors, including:

How do I remove repeated elements from ArrayList?

To remove repeated elements from an ArrayList in Java, you can use the removeAll method and pass it a Collection containing the elements to be removed.

Creating an instance using the class name and calling constructor

To create an instance of a class using its class name and calling its constructor, you can use the Class.forName method and the newInstance method.

Eclipse - no Java (JRE) / (JDK) ... no virtual machine

If you see an error message in Eclipse saying "no Java (JRE) / (JDK) ... no virtual machine", it means that Eclipse is unable to find a valid Java installation on your system.

How to deploy a war file in Tomcat 7

To deploy a WAR (Web ARchive) file in Apache Tomcat 7, you can follow these steps:

How to create a generic array in Java?

In Java, it is not possible to create a generic array directly. However, you can create an array of a specific type and then cast it to a generic type.

SSL and cert keystore

An SSL (Secure Sockets Layer) keystore is a storage location for SSL certificates, which are used to establish secure, encrypted connections between a client and a server. The keystore is typically managed by a keystore manager, such as the Java Keytool,

Returning JSON object as response in Spring Boot

To return a JSON object as a response in Spring Boot, you can use the @ResponseBody annotation and the ObjectMapper class.

Mockito How to mock and assert a thrown exception?

To mock and assert a thrown exception in Mockito, you can use the doThrow() method and the verify() method.

How can I log SQL statements in Spring Boot?

To log SQL statements in Spring Boot, you can configure the logging level of the org.hibernate.SQL logger to DEBUG or TRACE.

Convert boolean to int in Java

To convert a boolean value to an integer in Java, you can use the intValue() method of the java.lang.Boolean class, or you can use a conditional operator.

Java: convert List<String> to a join()d String

To convert a List&lt;String&gt; to a single string by joining the elements of the list with a separator, you can use the join() method of the java.util.StringJoiner class.

Getting the filenames of all files in a folder

To get the filenames of all files in a folder in Java, you can use the File class and its list method.

Java: how to initialize String[]?

There are several ways to initialize a String array in Java. Here are a few examples:

How to convert hashmap to JSON object in Java

To convert a hashmap to a JSON object in Java, you can use the org.json library. Here's an example:

Difference between "wait()" vs "sleep()" in Java

In Java, the wait() method is used to pause the current thread and allow other threads to execute. It is called on an object and causes the current thread to wait until another thread calls the notify() or notifyAll() method on the same object.

How to print a float with 2 decimal places in Java?

To print a float with 2 decimal places in Java, you can use the printf method and specify a format string with the %.2f format specifier. Here's an example: