How-to articles, tricks, and solutions about JAVA

How to generate a random alpha-numeric string

To generate a random alpha-numeric string in Java, you can use the Random class and the nextInt and nextBoolean methods to generate random characters and append them to a StringBuilder. Here's an example of how you might do this:

How do I create a Java string from the contents of a file?

To create a Java string from the contents of a file, you can use the following code:

Convert ArrayList<String> to String[] array

To convert an ArrayList<String> to a String[] array in Java, you can use the toArray method of the ArrayList class and pass it an empty array of the appropriate type:

Failed to load the JNI shared Library (JDK)

This error can occur when you are trying to run a Java program and the Java Virtual Machine (JVM) cannot find the required shared libraries. There are a few different causes of this error and a few different things you can try to fix it:

Removing whitespace from strings in Java

There are a few different ways you can remove whitespace from strings in Java. Here are a few options: Using the replaceAll method:

How to convert array to list in Java

To convert an array to a list in Java, you can use the Arrays.asList() method. This method returns a fixed-size list backed by the specified array. Here's an example:

How to check if a String is numeric in Java

To check if a String is numeric in Java, you can use the isNumeric method of the StringUtils class from the org.apache.commons.lang3 library.

How to initialize List<String> object in Java?

To initialize a List<String> object in Java, you can use one of the following approaches: Using the new keyword:

How to get the user input in Java?

To get user input in Java, you can use the Scanner class. Here's an example of how to get a string input from the user:

How do I make a delay in Java?

To make a delay in Java, you can use the Thread.sleep method which will pause the current thread for a specified number of milliseconds. Here's an example:

How to convert jsonString to JSONObject in Java

To convert a JSON string to a JSONObject in Java, you can use the JSONObject constructor that takes a String as an argument, like this:

How to round a number to n decimal places in Java

There are several ways to round a number to n decimal places in Java:Using DecimalFormat

How to deal with "java.lang.OutOfMemoryError: Java heap space" error?

"java.lang.OutOfMemoryError: Java heap space" means that the application has exhausted the maximum heap space. To fix this error, you can try the following:

How to get the current date/time in Java

To get the current date and time in Java, you can use the <code>java.time</code> package introduced in Java 8.

How to parse JSON in Java

To parse a JSON string in Java, you can use the org.json library. This library provides a simple and easy-to-use interface for parsing and manipulating JSON data in Java.

Java string to date conversion

To convert a string to a date in Java, you can use the parse method of the SimpleDateFormat class.

How do I convert from int to String in Java?

To convert an int to a String in Java, you can use the Integer.toString() method. For example

Is Java "pass-by-reference" or "pass-by-value"?

In Java, arguments are passed to methods by value. This means that when you pass an argument to a method, the method receives a copy of the argument rather than a reference to the original object.

How do I read / convert an InputStream into a String in Java?

There are several ways to read an InputStream and convert it to a String in Java. One way is to use the BufferedReader and InputStreamReader classes to read the InputStream line by line, and use a StringBuilder to construct the final String:

What is the difference between public, protected, package-private and private in Java?

In the Java programming language, the accessibility of a class, method, or field can be controlled using access modifiers. There are four access modifiers in Java:

How do I determine whether an array contains a particular value in Java?

To check if an array contains a particular value in Java, you can use the contains() method of the List interface, which is implemented by the ArrayList class.

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

How to make a new List in Java

In Java, you can create a new list using the java.util.ArrayList class or the java.util.LinkedList class.

Reading a plain text file in Java

This code opens the file "filename.txt" and reads it line by line. Each line is printed to the console until the end of the file is reached.

What's the simplest way to print a Java array?

There are several ways to print an array in Java. Here are a few options: Using a loop