How-to articles, tricks, and solutions about JAVA

Declaring an unsigned int in Java

Java does not have an unsigned int data type.

Decode Base64 data in Java

To decode base64 data in Java, you can use the Base64.Decoder class introduced in Java 8. Here's an example of how to use it:

Deleting an object in java?

In Java, objects are automatically eligible for garbage collection when they are no longer reachable.

Determine if a String is an Integer in Java

To determine if a string is an integer in Java, you can use the isDigit() method of the Character class to check if each character in the string is a digit. 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.

Difference between @Mock and @InjectMocks

In the context of testing with the Mockito framework, the @Mock annotation is used to create a mock object of a class or interface, and the @InjectMocks annotation is used to inject the mock objects into a test class.

Difference between & and && in Java?

In Java, the & operator is a bitwise AND operator, and the && operator is a logical AND operator.

Difference between break and continue statement

In Java, the break and continue statements are used to control the flow of a loop.

Difference between DTO, VO, POJO, JavaBeans?

In Java, DTO stands for Data Transfer Object, and it is a simple object that is used to carry data between processes. DTOs are often used when transferring data over a network, or between layers of an application. They typically have private fields, and g

Difference between FetchType LAZY and EAGER in Java Persistence API?

In the Java Persistence API (JPA), the FetchType enum is used to specify the strategy for fetching data from the database.

Difference between HashMap, LinkedHashMap and TreeMap

In Java, HashMap, LinkedHashMap, and TreeMap are all implementations of the Map interface.

Difference between StringBuilder and StringBuffer

The StringBuilder and StringBuffer classes in Java are used to create mutable strings. A mutable string is a string that can be modified after it is created, unlike a regular string, which is immutable and cannot be modified.

Differences between Oracle JDK and OpenJDK

The Oracle JDK (Java Development Kit) and the OpenJDK are two implementations of the Java SE (Standard Edition) platform.

Display current time in 12 hour format with AM/PM

To display the current time in 12-hour format with AM/PM in Java, you can use the SimpleDateFormat class and the Calendar class.

Does a finally block always get executed in Java?

In Java, a finally block is guaranteed to be executed, unless the virtual machine exits abruptly due to an uncaught exception or a call to System.exit.

Does a primitive array length reflect the allocated size or the number of assigned elements?

In Java, the length of a primitive array reflects the number of allocated elements in the array. This is the maximum number of elements that the array can hold.

Does Java have support for multiline strings?

In Java, there is no native support for multiline strings.

Does Java support default parameter values?

Java does not have built-in support for default parameter values like some other programming languages.

Does java.util.List.isEmpty() check if the list itself is null?

No, the isEmpty() method of the java.util.List interface does not check if the list itself is null.

doGet and doPost in Servlets

doGet and doPost are methods of the javax.servlet.http.HttpServlet class that are used to handle HTTP GET and POST requests, respectively.

Double decimal formatting in Java

To format a double value as a decimal in Java, you can use the DecimalFormat class from the java.text package. Here's an example of how to use it:

Download a file with Android, and showing the progress in a ProgressDialog

To download a file with Android and show the progress in a ProgressDialog, you can use the following steps:

Downloading a file from spring controllers

To download a file from a Spring controller, you can use the ResponseEntity class along with the InputStreamResource class.

Downloading Java JDK on Linux via wget is shown license page instead

If you are trying to download the Java Development Kit (JDK) on Linux using wget and you are being shown the license page instead of the JDK download, it is likely because the download page has changed since the wget command was written.

Easiest way to convert a List to a Set in Java

To convert a List to a Set in Java, you can use the new HashSet<>(list) constructor to create a new HashSet and initialize it with the elements of the List.