How-to articles, tricks, and solutions about JAVA

How to convert Strings to and from UTF8 byte arrays in Java

To convert a String to a UTF-8 encoded byte array in Java, you can use the getBytes method of the String class and specify the character encoding as "UTF-8".

How to convert the following json string to java object?

To convert a JSON string to a Java object, you can use the fromJson method of the Gson class.

How to convert/parse from String to char in java?

In Java, you can convert a string to a character using the charAt() method of the String class. This method takes an index as an argument and returns the character at that index.

How to count the number of occurrences of an element in a List

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.

How to create a directory in Java?

To create a directory in Java, you can use the mkdir() method of the File class.

How to Create a File in Java

Learn 3 ways of creating files in Java with examples. Use java.io.File class, java.io.FileOutputStream class or Java NIO Files.write() class for creating new files in Java.

How to create a generic array in Java?

In Java, it is not possible to create a generic array directly. However, you can create an array of a specific type and then cast it to a generic type.

How to create a sub array from another array in Java?

There are a few different ways to create a subarray from another array in Java:

How to create a temporary directory/folder in Java?

To create a temporary directory/folder in Java, you can use the createTempDirectory() method of the Files class in the java.nio.file package.

How to create a two-dimensional array in Java?

To create a two-dimensional array in Java, you can use the following syntax:

How to create ArrayList from array in Java

To create an ArrayList from an array in Java, you can use the ArrayList constructor that takes an Collection as an argument.

How to create correct JSONArray in Java using JSONObject

To create a JSON array using JSONObject in Java, you can use the put() method of the JSONObject class to add elements to the array.

How to create JSON Object using String?

To create a JSON object from a string in Java, you can use the org.json library. Here's an example of how to do this:

How to create RecyclerView with multiple view types

To create a RecyclerView with multiple view types in Android, you will need to use a RecyclerView.Adapter that supports multiple view types.

How to Create Temporary File in Java

Learn the two static methods of createTempFile of the File class. Use the deleteOnExit() method to ensure that the temporary created file will automatically be deleted.

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

How to decompile a whole Jar file?

To decompile a JAR file, you can use a Java decompiler such as JD-GUI.

How to decompile DEX into Java source code?

To decompile DEX (Dalvik Executable) files into Java source code, you can use a DEX decompiler such as JD-GUI or DEX2JAR.

How to deploy a war file in Tomcat 7

To deploy a WAR (Web ARchive) file in Apache Tomcat 7, you can follow these steps:

How to determine an object's class?

To determine the class of an object in Java, you can use the getClass() method of the Object class. The getClass() method returns a Class object that represents the object's class.

How to determine day of week by passing specific date?

To determine the day of the week for a specific date in Java, you can use the get() method of the Calendar class.

How to directly initialize a HashMap in Java?

To directly initialize a HashMap in Java, you can use the put() method to add elements to the map.

How to do URL decoding in Java?

To decode a URL in Java, you can use the URLDecoder class from the java.net package.

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.