How-to articles, tricks, and solutions about JAVA

How do I address unchecked cast warnings?

An unchecked cast warning in Java occurs when the compiler cannot verify that a cast is safe at compile time.

JUnit 5: How to assert an exception is thrown?

To read a text file in Java, you can use the BufferedReader class from the java.io package.

How to evaluate a math expression given in string form?

There are a few different ways to evaluate a math expression given in string form:

Java: How to read a text file

To read a text file in Java, you can use the BufferedReader class from the java.io package.

ArrayList of int array in java

To create an ArrayList of int arrays in Java, you can use the following syntax:

Does a finally block always get executed in Java?

In Java, a finally block is guaranteed to be executed, unless the virtual machine exits abruptly due to an uncaught exception or a call to System.exit.

RESTful call in Java

To make a RESTful call in Java, you can use the HttpURLConnection class.

Convert a string representation of a hex dump to a byte array using Java?

To convert a string representation of a hex dump to a byte array in Java, you can use the DatatypeConverter class's parseHexBinary method.

Java string split with "." (dot)

To split a string in Java using a dot (.) as the delimiter, you can use the split method of the String class.

Get integer value of the current year in Java

To get the integer value of the current year in Java, you can use the Calendar class and the get method.

Why is the Java main method static?

In Java, the main method is declared as static because the JVM (Java Virtual Machine) needs to be able to invoke it without creating an instance of the class that contains it.

Convert JsonObject to String

To convert a JsonObject to a String in Java, you can use the JsonObject.toString method.

Key existence check in HashMap

To check if a key exists in a HashMap in Java, you can use the containsKey method.

When to use @QueryParam vs @PathParam

In a Java RESTful web service, the @QueryParam annotation is used to bind a query parameter to a method parameter, while the @PathParam annotation is used to bind a path parameter to a method parameter.

Wait for page load in Selenium

In Selenium, you can use the WebDriverWait class to wait for a page to load.

Sorting HashMap by values

To sort a HashMap by values in Java, you can use the Map.Entry interface and the Comparator interface to create a comparator that compares the values in the map.

How can I check if a value is of type Integer?

To check if a value is of type Integer in Java, you can use the instanceof operator.

How to get the path of a running JAR file?

To get the path of a running JAR file, you can use the java.lang.Class class's getProtectionDomain method and then the getCodeSource method.

@RequestParam vs @PathVariable

In Spring MVC, the @RequestParam annotation is used to bind a request parameter to a method parameter.

How to do URL decoding in Java?

To decode a URL in Java, you can use the URLDecoder class from the java.net package.

Testing Private method using mockito

It is generally considered bad practice to test private methods, as they are an implementation detail that should not be exposed to the outside world.

'Field required a bean of type that could not be found.' error spring restful API using mongodb

The 'Field required a bean of type that could not be found' error in a Spring application typically indicates that the application is trying to inject a dependency into a field, but it cannot find a bean of the required type in the application context.

Why use getters and setters/accessors?

Getters and setters, also known as accessors, are methods that are used to get and set the values of an object's properties.

Check whether number is even or odd

To check whether a number is even or odd in Java, you can use the modulus operator (%) to determine the remainder of the number when it is divided by 2.