How-to articles, tricks, and solutions about JAVA

How to declare an ArrayList with values?

To declare an ArrayList with values in Java, you can use the Arrays.asList method and pass it an array or a list of values.

When is the finalize() method called in Java?

In Java, the finalize method is called by the garbage collector when it determines that an object is no longer reachable.

GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

If GSON is throwing a JsonSyntaxException with the message "Expected BEGIN_OBJECT but was BEGIN_ARRAY", it means that you are trying to parse a JSON array as if it were a JSON object.

How to read integer value from the standard input in Java

To read an integer value from the standard input (keyboard) in Java, you can use the Scanner class from the java.util package.

How do you kill a Thread in Java?

To stop a thread in Java, you can use the interrupt() method of the Thread class.

How to find the length of an array list?

To find the length (number of elements) of an ArrayList in Java, you can use the size() method of the ArrayList class.

Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception

The java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception is thrown when a Java application is unable to find the LogFactory class from the Apache Commons Logging library.

Get a JSON object from a HTTP response

To get a JSON object from a HTTP response in Java, you can use the JSONObject class from the org.json library.

Why do I need to override the equals and hashCode methods in Java?

The equals() and hashCode() methods are two methods that are defined in the Object class, which is the superclass of all classes in Java.

How can I properly compare two Integers in Java?

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

A Java collection of value pairs? (tuples?)

In Java, you can use the AbstractMap.SimpleEntry class from the java.util package to represent a value pair (also known as a tuple).

How to convert an int array to String with toString method in Java

To convert an int array to a string in Java, you can use the Arrays.toString() method from the java.util package.

Getting Keyboard Input

To get keyboard input in Java, you can use the Scanner class from the java.util package.

Spring Boot and multiple external configuration files

To use multiple external configuration files with Spring Boot, you can use the spring.config.name property to specify the names of the configuration files.

Java - removing first character of a string

To remove the first character of a string in Java, you can use the substring() method of the String class.

keytool error Keystore was tampered with, or password was incorrect

The "Keystore was tampered with, or password was incorrect" error typically occurs when you try to use the keytool utility to access a keystore with an incorrect password or when the keystore has been modified in some way.

Java; String replace (school project)?

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

"Content is not allowed in prolog" when parsing perfectly valid XML on GAE

The "Content is not allowed in prolog" error typically occurs when you try to parse an XML document that contains characters before the XML prolog (the <?xml ...?> declaration).

How to enter quotes in a Java string?

To enter quotes in a Java string, you can use the escape character \ to indicate that the quote is part of the string, rather than the end of the string.

What issues should be considered when overriding equals and hashCode in Java?

When overriding the equals() and hashCode() methods in Java, you should consider the following issues:

How to find the index of an element in an int array?

To find the index of an element in an int array in Java, you can use the indexOf() method of the Arrays class.

Simplest way to read JSON from a URL in Java

To read JSON from a URL in Java, you can use the readJsonFromUrl() method from the JSON simple library.

Hibernate show real SQL

To show the real SQL generated by Hibernate, you can enable the show_sql property in the Hibernate configuration file (hibernate.cfg.xml).

how to convert java string to Date object

To convert a string to a Date object in Java, you can use the parse() method of the SimpleDateFormat class.