How-to articles, tricks, and solutions about JAVA

Comparing strings by their alphabetical order

To compare strings by their alphabetical order in Java, you can use the compareTo() method of the String class.

Cannot make a static reference to the non-static method

The "Cannot make a static reference to the non-static method" error occurs when you try to call a non-static method from a static context.

Failed to install android-sdk: "java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema"

The java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema error can occur if the required Java XML Binding (JAXB) classes are not present on the classpath.

How to run test methods in specific order in JUnit4?

In JUnit 4, you can use the @FixMethodOrder annotation to specify the order in which test methods should be executed.

Viewing contents of a .jar file

To view the contents of a .jar file, you can use the jar command-line utility that is included with the Java Development Kit (JDK).

Reflection generic get field value

To get the value of a generic field using reflection in Java, you can use the get() method of the Field class, which returns the value of the field as an Object.

What is the equivalent of Java static methods in Kotlin?

In Kotlin, you can use the companion object to define static methods and properties.

When should I use File.separator and when File.pathSeparator?

In Java, the File.separator field is a string that represents the separator character used in file paths on the current operating system.

StringBuilder vs String concatenation in toString() in Java

In Java, you can use either a StringBuilder or string concatenation to create a string representation of an object.

Java ArrayList copy

There are several ways to create a copy of an ArrayList in Java:

How to add directory to classpath in an application run profile in IntelliJ IDEA?

To add a directory to the classpath in an application run profile in IntelliJ IDEA, follow these steps:

Convert String array to ArrayList

To convert a String array to an ArrayList in Java, you can use the following steps:

What is the meaning of "this" in Java?

In Java, the this keyword refers to the current object.

Converting Integer to Long

To convert an Integer object to a Long object in Java, you can use the longValue() method of the Integer class, which returns the value of the Integer object as a long.

How to open/run .jar file (double-click not working)?

To open a .jar file on a Windows system, you can do one of the following:

Java FileOutputStream Create File if not exists

To create a file using FileOutputStream in Java if it does not exist, you can use the following code:

Redirect to an external URL from controller action in Spring MVC

To redirect to an external URL from a controller action in Spring MVC, you can use the RedirectView class and return it from the controller action.

What is the use of printStackTrace() method in Java?

The printStackTrace() method is a method of the Throwable class (the parent class of Exception) that prints the stack trace of the exception to the standard error stream.

Remove part of string in Java

To remove a part of a string in Java, you can use the replace() method of the String class.

Sending Email in Android using JavaMail API without using the default/built-in app

To send an email in Android using the JavaMail API without using the default/built-in app, you can use the following steps:

How to test that no exception is thrown?

To test that no exception is thrown in a Java method, you can use the assertDoesNotThrow method from the org.junit.jupiter.api.Assertions class (part of the JUnit 5 library).

Serializing with Jackson (JSON) - getting "No serializer found"?

If you are getting the error "No serializer found" when trying to serialize an object to JSON using Jackson, it usually means that Jackson does not know how to serialize one or more fields in the object.

How to count the number of occurrences of an element in a List

To count the number of occurrences of an element in a List in Java, you can use the Collections.frequency(Collection, Object) method, which returns the number of times the specified element appears in the collection.

Switch on Enum in Java

To use a switch statement on an enumeration (enum) in Java, you can use the enum type as the control expression for the switch statement.

Check if a String contains a special character

To check if a String contains a special character in Java, you can use a regular expression to match any character that is not a letter or a digit.