How-to articles, tricks, and solutions about JAVA
To initialize an int array in a constructor in Java, you can use an initializer list as follows:
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:
A java.net.SocketException: Connection reset can be caused by a variety of issues, such as:
To compare two char values in Java, you can use the == operator, just like you would with any other primitive type. For example:
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.
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:
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.
To call one constructor from another in Java, you can use the this keyword. Here is an example:
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.
To mock a void method with Mockito, you can use the doAnswer method. Here is an example of how you can use it:
To set the JAVA_HOME environment variable on Mac OS X 10.9, follow these steps:
To generate an MD5 hash in Java, you can use the MessageDigest class from the java.security package.
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.
To URL encode the query string parameters of a URL in Java, you can use the URLEncoder class from the java.net package.
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:
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.
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,
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.
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.
In Java, you can use the concat() method of the java.lang.String class or the + operator to concatenate (join) two or more strings.
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.
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:
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.
Mockito is a popular mocking framework for Java. It allows you to create mock objects and set up test behavior for them.
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.