How-to articles, tricks, and solutions about JAVA

Using two values for one switch case statement

To use two values in a switch case statement in Java, you can use the case label for each value, or you can use the case label with a range of values.

UTF-8 byte[] to String

To convert a byte array to a string in UTF-8 encoding, you can use the following method in Java:

Variable might not have been initialized error

If you are getting the "variable might not have been initialized" error in Java, it means that you are trying to use a local variable that has not been assigned a value.

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).

Wait for page load in Selenium

In Selenium, you can use the WebDriverWait class to wait for a page to load.

Warning about SSL connection when connecting to MySQL database

If you are getting a warning about an SSL connection when connecting to a MySQL database, it may be because you are using an older version of the MySQL client library that does not support SSL connections by default.

Way to get number of digits in an int?

To get the number of digits in an int in Java, you can use the log10() method of the Math class and then add 1 to the result.

Ways to iterate over a list in Java

There are several ways to iterate over a List in Java. Here are some of the most common approaches:

What are all the different ways to create an object in Java?

In Java, you can create an object in the following ways:

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.

What are Java command line options to set to allow JVM to be remotely debugged?

To allow the Java Virtual Machine (JVM) to be remotely debugged, you can use the following command line options:

What are the -Xms and -Xmx parameters when starting JVM?

The -Xms and -Xmx options are used to set the initial and maximum heap sizes, respectively, for the Java Virtual Machine (JVM).

What are the differences between a HashMap and a Hashtable in Java?

There are several differences between a HashMap and a Hashtable in Java:Synchronization: Hashtable is synchronized, while HashMap is not.

What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

The hbm2ddl.auto configuration property in Hibernate determines the mode in which Hibernate should operate when it comes to generating the database schema. The possible values for this property are:

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

java.lang.ArrayIndexOutOfBoundsException is an exception that is thrown when you try to access an element of an array with an index that is out of bounds. This can happen if you try to access an index that is either negative or greater than or equal to

What causes javac to issue the "uses unchecked or unsafe operations" warning

The javac compiler may issue the "uses unchecked or unsafe operations" warning if your code uses operations that may result in an Unchecked warning at runtime.

What could cause java.lang.reflect.InvocationTargetException?

The java.lang.reflect.InvocationTargetException is a checked exception that is thrown when an exception is thrown by an invoked method or constructor.

What do 3 dots next to a parameter type mean in Java?

In Java, the three dots (...) next to a parameter type indicate that the parameter is a varargs parameter.

What does 'synchronized' mean?

In Java, the synchronized keyword is used to provide mutually exclusive access to a shared resource. When a thread tries to execute a synchronized block of code, it will first acquire a lock on the object that the code is synchronized on. If another threa

What does "Could not find or load main class" mean?

"Could not find or load main class" is an error message shown when a Java program is run, but the main class cannot be found or loaded.

What does %5B and %5D in POST requests stand for?

The strings "%5B" and "%5D" are URL-encoded versions of the square brackets "[" and "]", respectively.

What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?

A "Cannot find symbol" or "Cannot resolve symbol" error in Java means that the compiler cannot find a symbol (such as a class, method, or variable) that it needs to complete the compilation. This can happen for a variety of reasons, including:

What does the 'static' keyword do in a class?

In a class, the static keyword is used to declare a static member, which belongs to the class itself rather than an instance of the class. This means that you can access a static member without creating an instance of the class.

What does the Java assert keyword do, and when should it be used?

The assert keyword in Java is used to define an assertion, which is a statement that you expect to be true at a specific point in your program. If the assertion is not true, then the program will throw an AssertionError.