Raja has Published 760 Articles

How many ways to iterate a TreeSet in Java?

raja

raja

Updated on 29-Nov-2023 10:07:15

689 Views

A Treeset is a subclass of AbstractSet class and implements NavigableSet Interface. By Default, a Treeset gives an ascending order of output and it will use a Comparable interface for sorting the set elements. Inside a Treeset, we can add the same type of elements otherwise it can generate a ... Read More

How to solve an IllegalArgumentException in Java?

raja

raja

Updated on 28-Nov-2023 10:26:54

25K+ Views

An IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it ... Read More

Can we override a start() method in Java?

raja

raja

Updated on 28-Nov-2023 09:51:48

2K+ Views

Yes, we can override the start() method of a Thread class in Java. We must call the super.start() method to create a new thread and need to call run() method in that newly created thread. If we call the run() method directly from within our start() method, it can be ... Read More

Object level lock vs Class level lock in Java?

raja

raja

Updated on 28-Nov-2023 09:43:01

5K+ Views

Both Object level lock and Class level lock are used to achieve synchronization mechanisms in a multi-threaded application. Object Level Lock Every object in Java has a unique lock. If a thread wants to execute a synchronized method on a given object, first it has to get a lock of that object. ... Read More

Importance of @Override annotation in Java?

raja

raja

Updated on 28-Nov-2023 09:33:44

9K+ Views

The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method. The @Override annotation can be useful for two reasons. It extracts a warning from the compiler if the ... Read More

Can we synchronize a run() method in Java?

raja

raja

Updated on 28-Nov-2023 09:22:45

2K+ Views

Yes, we can synchronize a run() method in Java, but it is not required because this method has been executed by a single thread only. Hence synchronization is not needed for the run() method. It is good practice to synchronize a non-static method of other class because it is invoked ... Read More

Differences between Collection and Collections in Java?

raja

raja

Updated on 27-Nov-2023 15:57:27

6K+ Views

The Collection is an interface whereas Collections is a utility class in Java. The Set, List, and Queue are some of the subinterfaces of Collection interface, a Map interface is also part of the Collections Framework, but it doesn't inherit Collection interface. The important methods of Collection interface are add(), remove(), ... Read More

How to print the elements of a HashMap in Java?

raja

raja

Updated on 27-Nov-2023 15:49:03

2K+ Views

A HashMap is a subclass of AbstractMap class and it is used to store key & value pairs. Each key is mapped to a single value in the map and the keys are unique. It means we can insert a key only once in a map and duplicate keys are not ... Read More

What will happen if we directly call the run() method in Java?

raja

raja

Updated on 27-Nov-2023 15:43:48

138 Views

A direct call of a Thread object's run() method does not start a separate thread and it can be executed within the current thread. To execute Runnable.run from within a separate thread, do one of the following. Construct a thread using the Runnable object and call start() method on the Thread. Define ... Read More

How to get the string representation of numbers using toString() in Java?

raja

raja

Updated on 27-Nov-2023 11:17:23

516 Views

The toString() method is an important method of Object class and it can be used to return the string or textual representation of an object. The object class’s toString() method returns a string as the name of the specified object’s class which is followed by ‘@’ sign and the hashcode ... Read More

Advertisements