How-to articles, tricks, and solutions about JAVA

Converting A String To Hexadecimal In Java

To convert a string to a hexadecimal string in Java, you can use the encodeHexString method of the org.apache.commons.codec.binary.Hex class.

How to get a file's Media Type (MIME type)?

To get the media type (also known as MIME type) of a file in Java, you can use the Files.probeContentType method of the java.nio.file.Files class.

Converting double to integer in Java

To convert a double to an int in Java, you can use the intValue() method of the Double class.

console.writeline and System.out.println

console.writeline is not a valid method in Java.

Adding header for HttpURLConnection

To add a header to an HTTP request using HttpURLConnection, you can use the setRequestProperty method.

How do you know a variable type in java?

You can use the instanceof operator to determine the type of a variable in Java.

How do I replace a character in a string in Java?

To replace a character in a string in Java, you can use the String.replace(char oldChar, char newChar) method.

How to get current moment in ISO 8601 format with date, hour, and minute?

You can use the Instant class from the Java 8 java.time package to get the current moment in ISO 8601 format with date, hour, and minute.

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:

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.

How to mock a final class with mockito

To mock a final class with Mockito, you can use the PowerMockito library.

Error:java: javacTask: source release 8 requires target release 1.8

This error message usually indicates that you are trying to compile your Java code with a version of the javac compiler that is not compatible with the version of the Java language that your code is written in.

Spring Data JPA - "No Property Found for Type" Exception

This exception is usually thrown when you are trying to access a property that does not exist in the entity class you are querying.

How can I use pointers in Java?

In Java, pointers are not used in the same way that they are used in other programming languages such as C or C++.

Simple HTTP server in Java using only Java SE API

To create a simple HTTP server in Java using only the Java SE API, you can use the java.net package.

How to return multiple values?

There are a few different ways to return multiple values from a method in Java:

Why is char[] preferred over String for passwords?

It is generally considered more secure to use a char[] array to store passwords because it can be wiped from memory more easily than a String object.

Setting active profile and config location from command line in spring boot

To set the active profile and the configuration location from the command line in Spring Boot, you can use the spring.profiles.active and spring.config.name properties.

Converting JSON data to Java object

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

How to format decimals in a currency format?

You can use the NumberFormat class to format decimals in a currency format.

Making a mocked method return an argument that was passed to it

To make a mocked method return an argument that was passed to it, you can use the Mockito.when method and pass it the argument that you want to return as the answer.

Spring JPA selecting specific columns

To select specific columns using Spring JPA, you can use the @Query annotation and specify the columns in the SELECT clause of the JPQL query.

How can I convert my Java program to an .exe file?

There are several ways to convert a Java program to an executable file (.exe):

How to append a newline to StringBuilder

To append a newline to a StringBuilder object in Java, you can use the append method and pass it a newline character.