How-to articles, tricks, and solutions about JAVA

Any shortcut to initialize all array elements to zero?

To initialize all elements of an array to zero in Java, you can use the Arrays.fill method from the java.util package.

Append a single character to a string or char array in java?

To append a single character to a string or char array in Java, you can use the + operator or the concat method for strings, or you can use the Arrays.copyOf method for char arrays.

ArrayList of int array in java

To create an ArrayList of int arrays in Java, you can use the following syntax:

Avoiding NullPointerException in Java

A NullPointerException is a runtime exception that is thrown when an application attempts to use an object reference that has a null value. To avoid this exception, you need to make sure that the object reference is not null before you use it.

Base64 Java encode and decode a string

To encode and decode a string in Base64 in Java, you can use the java.util.Base64 class.

Basic Java Float and Integer multiplication casting

To perform a multiplication between a float and an integer in Java, you can simply use the * operator as you would with any other numeric data types. The result of the multiplication will be a float, even if one of the operands is an integer.

Best way to convert an ArrayList to a string

To convert an ArrayList to a string in Java, you can use the join method of the String class, which allows you to join the elements of the ArrayList into a single string. Here's an example of how you might do this:

Best way to create enum of strings?

The best way to create an enum of strings in Java is to define the enum type with a String field, and then specify the possible values for the enum as string literals.

Break or return from Java 8 stream forEach?

To break or return from a Java 8 Stream.forEach() operation, you can use the break or return statements as you would normally do in a loop.

byte[] to file in Java

To write a byte[] to a file in Java, you can use the following code:

C# Java HashMap equivalent

In Java, the HashMap class is the equivalent of the Dictionary class in C#. It is a map data structure that stores (key, value) pairs and provides fast lookup and insertion of elements.

C++ performance vs. Java/C#

C++ is generally considered to be a faster and more performant language than Java or C#.

Calculate date/time difference in java

To calculate the difference between two dates in Java, you can use the java.time package (part of Java 8 and later) or the java.util.Calendar class (part of the older java.util package).

Calculating days between two dates with Java

To calculate the number of days between two dates in Java, you can use the Period class from the java.time package introduced in Java 8.

Calendar date to yyyy-MM-dd format in java

To format a Calendar date in the yyyy-MM-dd format in Java, you can use the SimpleDateFormat class.

Calling Non-Static Method In Static Method In Java

To call a non-static method from a static method in Java, you need to create an instance of the class and call the non-static method on that instance.

Calling remove in foreach loop in Java

If you want to remove elements from a collection while iterating over it using a for-each loop in Java, you must use an iterator instead of the looping construct.

Can an abstract class have a constructor?

Yes, an abstract class can have a constructor in Java.

Can an int be null in Java?

No, an int data type in Java cannot be null.

Can I add jars to Maven 2 build classpath without installing them?

Yes, you can add jars to the Maven build classpath without installing them.

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

This error is usually encountered when trying to parse a JSON string that does not start with a JSON array, but rather a JSON object.

Can you find all classes in a package using reflection?

Yes, it is possible to find all classes in a package using reflection in Java.

Can't execute jar- file: "no main manifest attribute"

The "no main manifest attribute" error is usually caused by a missing or incorrect Main-Class attribute in the manifest file of a Java .jar file.

Cannot make a static reference to the non-static method

The "Cannot make a static reference to the non-static method" error occurs when you try to call a non-static method from a static context.