How-to articles, tricks, and solutions about JAVA
In Java, the & operator is a bitwise AND operator, and the && operator is a logical AND operator.
To remove an item from an ArrayList in Java, you can use the remove() method of the ArrayList class.
To get the position of a match in a String in Java, you can use the indexOf() method of the String class.
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.
To set default values for columns in JPA (Java Persistence API), you can use the @Column annotation and the columnDefinition attribute.
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 an if statement with a string comparison fails in Java, it usually means that the strings being compared are not equal.
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.
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.
In Java, the break and continue statements are used to control the flow of a loop.
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.
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.
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.
To extract the source code from a .jar file, you can use a decompiler such as JD-GUI or Cavaj Java Decompiler.
To reference a method in Javadoc, you can use the {@link} tag.
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.
To parse an integer value from a char in Java, you can use the Character.getNumericValue() method.
To get the current location in Android, you can use the LocationManager class and the LocationProvider interface.
In Java 8, the map() and flatMap() methods are part of the Stream API, and are used to transform the elements of a stream.
To create a RecyclerView with multiple view types in Android, you will need to use a RecyclerView.Adapter that supports multiple view types.
There are several ways to terminate a Java program, depending on the context in which the program is running.
Java .class files are compiled Java bytecode, which is not easily readable by humans.
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.
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).
In Java, an escape character is a character that is preceded by a backslash (\) and is used to represent special characters or character sequences.