How-to articles, tricks, and solutions about 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.
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.
To convert a double to an int in Java, you can use the intValue() method of the Double class.
To add a header to an HTTP request using HttpURLConnection, you can use the setRequestProperty method.
You can use the instanceof operator to determine the type of a variable in Java.
To replace a character in a string in Java, you can use the String.replace(char oldChar, char newChar) method.
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.
There are a few different ways to create a subarray from another array in Java:
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.
To mock a final class with Mockito, you can use the PowerMockito library.
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.
This exception is usually thrown when you are trying to access a property that does not exist in the entity class you are querying.
In Java, pointers are not used in the same way that they are used in other programming languages such as C or C++.
To create a simple HTTP server in Java using only the Java SE API, you can use the java.net package.
There are a few different ways to return multiple values from a method in Java:
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.
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.
To convert JSON data to a Java object, you can use the fromJson method of the Gson class.
You can use the NumberFormat class to format decimals in a currency format.
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.
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.
There are a few ways to convert a character to an integer in Java.
There are several ways to convert a Java program to an executable file (.exe):
To append a newline to a StringBuilder object in Java, you can use the append method and pass it a newline character.