How-to articles, tricks, and solutions about JAVA

How to set or change the default Java (JDK) version on macOS?

To set the default Java (JDK) version on macOS, you can use the java_home command line tool. Here's how:

When to use static methods

In Java, static methods are methods that belong to a class rather than an instance of the class. They can be called without creating an instance of the class, using the name of the class and the dot operator (.).

What is a stack trace, and how can I use it to debug my application errors?

A stack trace is a report of the active stack frames at a particular point in time during the execution of a program. It is a useful tool for debugging because it can help you understand the sequence of method calls that led to an error or exception in yo

How do I join two lists in Java?

There are several ways to join two lists in Java. Here are a few options:

How do I use optional parameters in Java?

In Java, you can use method overloading to achieve the effect of optional parameters. Method overloading is the practice of having multiple methods with the same name but different parameter lists.

How to get maximum value from the Collection (for example ArrayList)?

In Java, default methods are methods that are defined in an interface and have a default implementation. They were introduced in Java 8 as a way to add new functionality to interfaces without breaking backward compatibility.

Basic Java Float and Integer multiplication casting

To perform a multiplication between a float and an integer in Java, you can simply use the * operator as you would with any other numeric data types. The result of the multiplication will be a float, even if one of the operands is an integer.

How to create a two-dimensional array in Java?

To create a two-dimensional array in Java, you can use the following syntax:

How to add new elements to an array in Java?

To add new elements to an array in Java, you have a few options: If you know the size of the array in advance and the array is not full, you can simply assign a new value to an unused element in the array.

How to assert that a certain exception is thrown in JUnit tests?

To assert that a certain exception is thrown in a JUnit test, you can use the @Test annotation's expected attribute. Here's an example:

What are the differences between a HashMap and a Hashtable in Java?

There are several differences between a HashMap and a Hashtable in Java:Synchronization: Hashtable is synchronized, while HashMap is not.

Implements vs extends: When to use? What's the difference?

In Java, the extends keyword is used to inherit from a superclass, and the implements keyword is used to implement an interface. Here are the key differences between the two:

How to install the JDK on Ubuntu Linux

To install the JDK (Java Development Kit) on Ubuntu Linux, follow these steps:

How to remove the last character from a string?

There are a few ways to remove the last character from a string in Python. Here are three approaches you can use:

Reverse a string in Java

To reverse a string in Java, you can use the following approaches:

In Java, how do I check if a string contains a substring (ignoring case)?

To check if a string contains a substring (ignoring case) in Java, you can use the contains method of the String class and the equalsIgnoreCase method.

How to sort a Map<Key, Value> by values in Java

To sort a Map&lt;Key, Value&gt; by values in Java, you can create a custom comparator that compares the values and pass it to the sort() method of the Map.Entry class.

How to match "any character" in regular expression?

To match "any character" in a regular expression, you can use the . (dot) character. The . character matches any single character, except for a newline.

How to directly initialize a HashMap in Java?

To directly initialize a HashMap in Java, you can use the put() method to add elements to the map.

How to sort a List/ArrayList?

To sort a List or ArrayList in Java, you can use the sort method of the Collections class from the java.util package. The sort method takes a List and sorts it in ascending order according to the natural ordering of its elements.

How to initialize an array in Java?

There are several ways to initialize an array in Java. Here are some examples: Using the new operator:

Why is processing a sorted array faster than processing an unsorted array in Java?

Processing a sorted array can be faster than processing an unsorted array because certain algorithms and operations have a lower average time complexity when the input is already sorted.

Convert java.util.Date to String

To convert a java.util.Date object to a String, you can use the format method of the SimpleDateFormat class from the java.text package. The SimpleDateFormat class provides a convenient way to convert a Date object to a String based on a specified format.

How to create ArrayList from array in Java

To create an ArrayList from an array in Java, you can use the ArrayList constructor that takes an Collection as an argument.

What could cause java.lang.reflect.InvocationTargetException?

The java.lang.reflect.InvocationTargetException is a checked exception that is thrown when an exception is thrown by an invoked method or constructor.