How-to articles, tricks, and solutions about JAVA

How can I convert a long to int in Java?

To convert a long value to an int value in Java, you can use the intValue method of the java.lang.Long class. This method returns the value of the specified long as an int.

Java inner class and static nested class

In Java, an inner class is a class that is defined within another class. An inner class has access to the members (fields and methods) of the outer class, and can be used to encapsulate the implementation of a component of the outer class.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure is an exception that is thrown when there is a failure in the communication link between your Java program and the MySQL database. This can happen for a variety of reason

Getting an element from a Set

A Set is a collection of elements in which each element can only occur once. A Set does not have a specific order and does not provide a way to access its elements by their position.

How can I check whether an array is null / empty?

To check if an array is null in Java, you can use the == operator and compare the array to null. For example:

How do I time a method's execution in Java?

To time the execution of a method in Java, you can use the System.nanoTime method to get the current time before and after the method is called, and then subtract the start time from the end time to get the elapsed time. Here's an example of how you can d

Convert float to String and String to float in Java

To convert a float to a String in Java, you can use the Float.toString method or the String.valueOf method. For example:

How to capitalize the first letter of a String in Java?

To capitalize the first letter of a string in Java, you can use the toUpperCase method of the java.lang.String class. Here's an example of how you can do this:

Math.random() explanation

Math.random() is a method in the java.lang.Math class that returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).

'Java' is not recognized as an internal or external command

If you are getting the error 'Java' is not recognized as an internal or external command, it means that the Java executable is not in your system's PATH. This means that when you try to run the java command, the system does not know where to find it.

Java Generate Random Number Between Two Given Values

To generate a random number between two given values in Java, you can use the nextInt method of the java.util.Random class. For example:

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

java.lang.NumberFormatException: For input string: "N/A" occurs when you try to parse a string that cannot be converted to a number, such as the string "N/A".

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

Java String array: is there a size of method?

In Java, you can use the length field of an array to determine the size of the array. For example, given an array arr, you can get the size of the array using arr.length.

Encoding as Base64 in Java

To encode a string as Base64 in Java, you can use the java.util.Base64 class. Here's an example of how you can use the Base64 class to encode a string:

How to read and write excel file

There are several ways to read and write Excel files in Java. Here are a few options:

How do I resolve ClassNotFoundException?

A ClassNotFoundException occurs when the Java virtual machine (JVM) is unable to find a class that has been referenced in your code. This can happen for a number of reasons, including:

How do I "decompile" Java class files? [closed]

There are several tools that you can use to "decompile" Java class files and view the source code. Some popular ones include:

How do I get a class instance of generic type T?

To get a class instance of a generic type T, you can use the new operator and specify the type argument when creating an instance of the class. Here's an example:

String.equals versus ==

In Java, the == operator is used to compare the primitive values of two variables, while the equals() method is used to compare the contents of two objects.

How to import a jar in Eclipse?

To import a JAR file into Eclipse, follow these steps:

Converting double to string

In Java, you can convert a double to a string using the Double.toString() method or the String.valueOf() method.

What is the best Java email address validation method?

There are a few different approaches you can take to validate email addresses in Java. Here are some options:

Non-static variable cannot be referenced from a static context

In Java, a non-static (also known as an instance) variable or method can only be accessed from an instance of the class. A static (also known as a class) variable or method, on the other hand, can be accessed directly from the class, without the need for

Comparing Java enum members: == or equals()?

In Java, it is generally recommended to use the equals() method to compare enum members, rather than the == operator.