How-to articles, tricks, and solutions about JAVA

Java Initialize an int array in a constructor

To initialize an int array in a constructor in Java, you can use an initializer list as follows:

java.net.SocketException: Connection reset

A java.net.SocketException: Connection reset is a runtime exception that is thrown when a connection is reset. This can be caused by a variety of issues, such as:

What's causing my java.net.SocketException: Connection reset?

A java.net.SocketException: Connection reset can be caused by a variety of issues, such as:

Comparing chars in Java

To compare two char values in Java, you can use the == operator, just like you would with any other primitive type. For example:

Does a primitive array length reflect the allocated size or the number of assigned elements?

In Java, the length of a primitive array reflects the number of allocated elements in the array. This is the maximum number of elements that the array can hold.

What is the point of "final class" in Java?

In Java, a class can be marked as final, which means that it cannot be subclassed. This can be useful in a number of situations:

What is a serialVersionUID and why should I use it?

A serialVersionUID is a unique identifier for a serializable class. It is used to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.

How do I call one constructor from another in Java?

To call one constructor from another in Java, you can use the this keyword. Here is an example:

What's the difference between @Component, @Repository & @Service annotations in Spring?

In Spring, the @Component annotation is used to mark a Java class as a candidate for component scanning. The @Repository annotation is a specialization of @Component for use in the persistence layer.

How to mock void methods with Mockito

To mock a void method with Mockito, you can use the doAnswer method. Here is an example of how you can use it:

How to set JAVA_HOME environment variable on Mac OS X 10.9?

To set the JAVA_HOME environment variable on Mac OS X 10.9, follow these steps:

How can I generate an MD5 hash in Java?

To generate an MD5 hash in Java, you can use the MessageDigest class from the java.security package.

Convert InputStream to byte array in Java

To convert an InputStream to a byte array in Java, you can use the readAllBytes() method of the java.nio.file.Files class from the java.nio.file package.

Java URL encoding of query string parameters

To URL encode the query string parameters of a URL in Java, you can use the URLEncoder class from the java.net package.

Double decimal formatting in Java

To format a double value as a decimal in Java, you can use the DecimalFormat class from the java.text package. Here's an example of how to use it:

How can I increment a date by one day in Java?

To increment a date by one day in Java, you can use the plusDays() method of the java.time.LocalDate class from the java.time package.

How do I find out my MySQL URL, host, port and username?

To find out your MySQL connection details, you will need to look in your MySQL configuration files. The specific location of these files will depend on how you installed MySQL, but common locations include /etc/my.cnf,

What does the Java assert keyword do, and when should it be used?

The assert keyword in Java is used to define an assertion, which is a statement that you expect to be true at a specific point in your program. If the assertion is not true, then the program will throw an AssertionError.

How to convert Java String into byte[]?

To convert a Java string into a byte array, you can use the getBytes() method of the java.lang.String class. This method returns an array of bytes representing the string in a specific encoding.

String concatenation: concat() vs "+" operator

In Java, you can use the concat() method of the java.lang.String class or the + operator to concatenate (join) two or more strings.

How can I get the current date and time in UTC or GMT in Java?

To get the current date and time in UTC or GMT in Java, you can use the Instant class from the java.time package. The Instant class represents a single point in time in the ISO-8601 calendar system, with a resolution of nanoseconds.

Check whether a String is not Null and not Empty

To check if a string is not null and not empty in Java, you can use the length() method of the java.lang.String class to check if the string is empty, and the != operator to check if the string is not null. Here is an example of how you can do this:

Java Hashmap: How to get key from value?

To get the key for a given value in a java.util.HashMap, you can use the entrySet() method of the java.util.HashMap class to get a set of the mappings in the map, and then iterate over the set and check the value of each mapping.

Mocking static methods with Mockito

Mockito is a popular mocking framework for Java. It allows you to create mock objects and set up test behavior for them.

How to convert String to long in Java?

To convert a string to a long in Java, you can use the parseLong() method of the java.lang.Long class. This method parses the string as a signed decimal long, and returns the resulting long value.