How-to articles, tricks, and solutions about JAVA

Why can't I use switch statement on a String?

In the Java programming language, you can use a switch statement to choose between a fixed number of alternatives.

How to tell Jackson to ignore a field during serialization if its value is null?

There are a few ways to tell Jackson to ignore a field during serialization if its value is null. One way is to use the @JsonInclude annotation with the Include.NON_NULL value on the field or class level.

Eclipse "Error: Could not find or load main class"

If you are seeing an error message that says "Error: Could not find or load main class", it is likely that there is a problem with the classpath for your project.

Copying files from one directory to another in Java

To copy a file from one directory to another in Java, you can use the Files.copy method from the java.nio.file package.

super() in Java

In Java, the super keyword is used to refer to the superclass of the current class. It is often used to call the constructor of the superclass, or to access methods or fields of the superclass that have been overridden in the current class.

Spring Boot - Cannot determine embedded database driver class for database type NONE

If you are seeing the error "Cannot determine embedded database driver class for database type NONE" in a Spring Boot application, it means that the application is trying to auto-configure an embedded database, but it is unable to determine which database

How to convert an ArrayList containing Integers to primitive int array?

To convert an ArrayList containing Integer objects to a primitive int array in Java, you can use the toArray method and a casting operation.

Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?

On macOS, the JAVA_HOME environment variable is typically set in the /etc/launchd.conf file.

android on Text Change Listener

In Android, you can use a TextWatcher to listen for changes to the text in a TextView or EditText view.

How to verify that a specific method was not called using Mockito?

To verify that a specific method was not called using Mockito, you can use the verifyZeroInteractions method.

How can I pad an integer with zeros on the left?

To pad an integer with zeros on the left in Java, you can use the String.format() method and specify a minimum field width for the integer. For example:

Http Basic Authentication in Java using HttpClient?

To perform HTTP basic authentication in Java using the HttpClient library, you can use the UsernamePasswordCredentials class and the BasicCredentialsProvider class.

How can I remove a substring from a given String?

There are several ways to remove a substring from a given string in Java. Here are a few options:

java.net.UnknownHostException: Invalid hostname for server: local

The java.net.UnknownHostException: Invalid hostname for server: local exception usually indicates that a hostname or an IP address could not be resolved. This can happen for a variety of reasons, such as:

How can I ask the Selenium-WebDriver to wait for few seconds in Java?

To make the Selenium WebDriver wait for a specified number of seconds in Java, you can use the Thread.sleep method. Here's an example of how you can do this:

What does 'synchronized' mean?

In Java, the synchronized keyword is used to provide mutually exclusive access to a shared resource. When a thread tries to execute a synchronized block of code, it will first acquire a lock on the object that the code is synchronized on. If another threa

RecyclerView onClick

To handle clicks on items in a RecyclerView, you can set an OnClickListener on the View that represents each item in the RecyclerView.

Finding the max/min value in an array of primitives using Java

To find the maximum value in an array of primitives in Java, you can use the Arrays.stream() method to create a stream from the array, and then use the Stream.max() method to find the maximum element in the stream. Here is an example of how to do this for

Why is subtracting these two times (in 1927) giving a strange result?

Without more context, it is difficult to say why subtracting two times would give a strange result. Here are a few potential explanations:

Scanner is skipping nextLine() after using next() or nextFoo()?

It is common for the Scanner class's next() and nextFoo() methods (where Foo is any primitive type such as Int, Double, etc.) to skip over newline characters in the input. This is because these methods are designed to read only the next token in the input

Java Does Not Equal (!=) Not Working? [duplicate]

It is possible that the != operator is not working as expected in your Java code because you are using it to compare object references, rather than the contents of the objects themselves.

Get generic type of class at runtime

If you want to get the generic type of a class at runtime, you can use the Type class in the System namespace.

How to determine an object's class?

To determine the class of an object in Java, you can use the getClass() method of the Object class. The getClass() method returns a Class object that represents the object's class.

How to convert a char array back to a string?

To convert a char array back to a String in Java, you can use the String class's constructor that takes a char array as an argument.

How do I set the proxy to be used by the JVM

To set the proxy to be used by the Java Virtual Machine (JVM), you can use the -Dhttp.proxyHost, -Dhttp.proxyPort, and -Dhttp.nonProxyHosts options when starting the JVM.