Thursday 17 March 2022

how to atomically update values in a ConcurrentHashMap in Java using Compute and ComputeIfAbsent methods?

Hi,  ConcurrentHashMap is a thread-safe implementation of the Java Map interface, designed to be used in multi-threaded environments. It allows multiple threads to access and modify the map simultaneously without causing data corruption or race conditions.

One of the key features of ConcurrentHashMap is that it provides atomic operations to update its elements. Atomic operations are operations that are performed as a single, indivisible unit of work, which means they are guaranteed to be executed completely or not at all. This is important in multi-threaded environments where multiple threads may try to modify the same element at the same time.

Read more »

Labels: , ,

Saturday 1 January 2022

Example of sorting an ArrayList in reverse order in Java

Hi, Today program demonstrates how to sort an ArrayList of integers in reverse order in Java using the Collections.sort method with the Collections.reverseOrder() method. It creates a new ArrayList of integers, adds four elements to it, and prints the contents of the ArrayList before and after sorting in reverse order.

Read more »

Labels: , ,

Friday 17 December 2021

"javac is not recognized as an internal or external command" error message that is appearing on your Windows or Linux machine?

The error message "javac is not recognized as an internal or external command" typically means that the Java compiler (javac) is not properly installed or configured on your system, or the system cannot find its location.

Here are some steps you can take to fix this issue in Windows:

Check if Java is installed: Open the Command Prompt and type "java -version" to see if Java is installed. If you see the version number, then Java is installed on your system. If not, you need to install Java.

Read more »

Labels: , ,

Monday 1 March 2021

Top 10 best Books to Learn Java for C and C++ Programmer?

 Java is a popular programming language that has been around for over two decades. It is widely used in the industry and is a favorite among developers due to its simplicity, portability, and versatility. C and C++ programmers who are interested in learning Java may find the transition to be relatively straightforward, as Java shares some similarities with C and C++.

To aid in the process of learning Java, there are many books available that cater specifically to the needs of C and C++ programmers. These books cover a wide range of topics, including Java syntax, object-oriented programming, data structures, network programming, performance optimization, and more. In this context, we have listed the top 10 books that are recommended for C and C++ programmers who are looking to learn Java.

Read more »

Labels: , , ,

Thursday 12 March 2020

@Override annotation in Java? Best practices

Hi, Today we will see @Override annotation in Java is used to indicate that a method in a subclass is intended to override a method in its superclass. Using this annotation is a coding best practice because it helps to catch errors early in the development process by ensuring that the overridden method has the correct method signature, including the method name, return type, and parameter types.

Here is an example:

Suppose we have a superclass Animal with a method makeSound(), and we want to create a subclass Cat that overrides this method to make a different sound. We can define the Cat class like this:

public class Cat extends Animal {

    @Override

    public void makeSound() {

        System.out.println("Meow");

    }

}

Read more »

Labels: , , , ,