How-to articles, tricks, and solutions about JAVA

Difference between & and && in Java?

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

Remove Item from ArrayList

To remove an item from an ArrayList in Java, you can use the remove() method of the ArrayList class.

Java: method to get position of a match in a String?

To get the position of a match in a String in Java, you can use the indexOf() method of the String class.

How to set selected item of Spinner by value, not by position?

To set the selected item of a Spinner by value (not by position) in Android, you can use the setSelection() method of the Spinner class and pass it the index of the item that you want to select.

Setting default values for columns in JPA

To set default values for columns in JPA (Java Persistence API), you can use the @Column annotation and the columnDefinition attribute.

java.io.FileNotFoundException: the system cannot find the file specified

The java.io.FileNotFoundException: the system cannot find the file specified error is usually thrown when a program tries to access a file that does not exist, or that it does not have permission to access.

If statement with String comparison fails

If an if statement with a string comparison fails in Java, it usually means that the strings being compared are not equal.

A for-loop to iterate over an enum in Java

To iterate over an enum in Java using a for loop, you can use the values() method of the enum type to get an array of all the enum values, and then use a standard for loop to iterate over the array.

Get only part of an Array in Java?

To get only a part of an array in Java, you can use the Arrays.copyOfRange() method, or you can use the System.arraycopy() method.

Difference between break and continue statement

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

Left padding a String with Zeros

To left pad a String with zeros in Java, you can use the String.format() method and the %0Nd format specifier, where N is the total length of the padded string.

Eclipse error: "The import XXX cannot be resolved"

If you get the error "The import XXX cannot be resolved" in Eclipse, it means that the class or package that you are trying to import cannot be found in the classpath of your project.

Create instance of generic type in Java?

To create an instance of a generic type in Java, you can use the newInstance() method of the Class class, along with the Type and TypeVariable classes.

Extract source code from .jar file

To extract the source code from a .jar file, you can use a decompiler such as JD-GUI or Cavaj Java Decompiler.

How to reference a method in javadoc?

To reference a method in Javadoc, you can use the {@link} tag.

Spring Boot - How to log all requests and responses with exceptions in single place?

In a Spring Boot application, you can log all requests and responses, along with exceptions, in a single place by using a combination of Spring Boot's logging framework and request/response interceptors.

Java: parse int value from a char

To parse an integer value from a char in Java, you can use the Character.getNumericValue() method.

How to get current location in Android

To get the current location in Android, you can use the LocationManager class and the LocationProvider interface.

What's the difference between map() and flatMap() methods in Java 8?

In Java 8, the map() and flatMap() methods are part of the Stream API, and are used to transform the elements of a stream.

How to create RecyclerView with multiple view types

To create a RecyclerView with multiple view types in Android, you will need to use a RecyclerView.Adapter that supports multiple view types.

Terminating a Java Program

There are several ways to terminate a Java program, depending on the context in which the program is running.

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.

Including dependencies in a jar with Maven

To include dependencies in a JAR file with Maven, you can use the maven-assembly-plugin to create an assembly that includes all the dependencies of your project.

How to get a thread and heap dump of a Java process on Windows that's not running in a console

To get a thread and heap dump of a Java process on Windows that is not running in a console, you can use the jstack and jmap tools that are included with the Java Development Kit (JDK).

What are all the escape characters?

In Java, an escape character is a character that is preceded by a backslash (\) and is used to represent special characters or character sequences.