How-to articles, tricks, and solutions about JAVA

How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer?

You can download the latest Java Runtime Environment (JRE) or Java Development Kit (JDK) as a zip file from the official website of Oracle (https://www.oracle.com/java/technologies/javase-downloads.html).

How can I increment a date by one day in Java?

To increment a date by one day in Java, you can use the plusDays() method of the java.time.LocalDate class from the java.time package.

How can I initialise a static Map?

To initialize a static Map in Java, you can use the Map interface's of method, which was added in Java 9. The of method creates an immutable Map with a fixed set of key-value pairs.

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.

How can I make Java print quotes, like "Hello"?

To print quotes in Java, you need to use the escape character \ to indicate that the quote is part of the string and not the end of the string.

How can I open Java .class files in a human-readable way?

Java .class files are compiled Java bytecode, which is not easily readable by humans.

How can I pad a String in Java?

To pad a string in Java, you can use the String.format() method and specify a width for the string.

How can I pad an integer with zeros on the left?

To pad an integer with zeros on the left in Java, you can use the String.format() method and specify a minimum field width for the integer. For example:

How can I parse/format dates with LocalDateTime? (Java 8)

In Java 8 and later, you can use the java.time.LocalDateTime class to represent a date and time without a time zone. To parse a date and time string into a LocalDateTime object, you can use the java.time.format.DateTimeFormatter class.

How can I pass a parameter to a Java Thread?

To pass a parameter to a Java thread, you can use a Runnable object and pass the parameter to its constructor.

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

java.lang.NumberFormatException: For input string: "N/A" occurs when you try to parse a string that cannot be converted to a number, such as the string "N/A".

How can I properly compare two Integers in Java?

To compare two Integer objects in Java, you can use the equals() method.

How can I read a large text file line by line using Java?

To read a large text file line by line in Java, you can use a BufferedReader and pass it a FileReader object to read the file. Here's an example of how you can do this:

How can I remove a substring from a given String?

There are several ways to remove a substring from a given string in Java. Here are a few options:

How can I solve "java.lang.NoClassDefFoundError"?

java.lang.NoClassDefFoundError is an error that occurs when the Java Virtual Machine (JVM) can't find a required class definition at runtime. This can happen for a variety of reasons, including:

How can I sort a List alphabetically?

To sort a List alphabetically in Java, you can use the Collections.sort method and pass in your List as an argument.

How can I sort Map values by key in Java?

To sort the values of a Map by key in Java, you can use the TreeMap class, which is a Map implementation that maintains its entries in ascending key order.

How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

To determine whether you are running in a 64-bit JVM or a 32-bit JVM from within a Java program, you can use the sun.arch.data.model system property.

How can I turn a List of Lists into a List in Java 8?

You can use the flatMap method from the Stream API to turn a List<List<T>> (a list of lists) into a List<T> in Java 8. Here's an example of how you can do this:

How can I upload files to a server using JSP/Servlet?

To upload files to a server using JSP/Servlet, you can use the following steps:

How can I use pointers in Java?

In Java, pointers are not used in the same way that they are used in other programming languages such as C or C++.

How do I "decompile" Java class files? [closed]

There are several tools that you can use to "decompile" Java class files and view the source code. Some popular ones include:

How do I address unchecked cast warnings?

An unchecked cast warning in Java occurs when the compiler cannot verify that a cast is safe at compile time.

How do I break out of nested loops in Java?

To break out of nested loops in Java, you can use the break statement. The break statement terminates the innermost loop that it is contained in, and transfers control to the statement immediately following the loop.

How do I call one constructor from another in Java?

To call one constructor from another in Java, you can use the this keyword. Here is an example: