How-to articles, tricks, and solutions about JAVA
To compare strings by their alphabetical order in Java, you can use the compareTo() method of the String class.
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.
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.
In JUnit 4, you can use the @FixMethodOrder annotation to specify the order in which test methods should be executed.
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).
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.
In Kotlin, you can use the companion object to define static methods and properties.
In Java, the File.separator field is a string that represents the separator character used in file paths on the current operating system.
In Java, you can use either a StringBuilder or string concatenation to create a string representation of an object.
To add a directory to the classpath in an application run profile in IntelliJ IDEA, follow these steps:
To convert a String array to an ArrayList in Java, you can use the following steps:
In Java, the this keyword refers to the current object.
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.
To open a .jar file on a Windows system, you can do one of the following:
To create a file using FileOutputStream in Java if it does not exist, you can use the following code:
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.
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.
To remove a part of a string in Java, you can use the replace() method of the String class.
To send an email in Android using the JavaMail API without using the default/built-in app, you can use the following steps:
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).
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.
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.
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.
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.