Raja has Published 359 Articles

How to make a class thread-safe in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

1K+ Views

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads.The HashMap is a non-synchronized collection class. If we need to perform thread-safe operations on it then we must need to synchronize ... Read More

How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

4K+ Views

StringBuffer(Thread-safe)StringBuffer is thread-safe meaning that they have synchronized methods to control access so that only one thread can access StringBuffer object's synchronized code at a time.StringBuffer objects are generally safe to use in a multi-threaded environment where multiple threads may be trying to access the same StringBuffer object at the ... Read More

Can we define a static constructor in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

5K+ Views

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur.In general, static means class level. A constructor will be used to assign initial values for the instance variables. Both static and constructor are different ... Read More

How to resize an array in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

5K+ Views

An array cannot be resized dynamically in Java. One approach is to use java.util.ArrayList(or java.util.Vector) instead of a native array.Another approach is to re-allocate an array with a different size and copy the contents of the old array to the new array.Example:class ResizableArray {    public static void main (String[] args) ... Read More

What is the importance of "Java.lang.Class" in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

488 Views

The java.lang.Class is one of the most important class in Java and it can provide several utility methods like getClass(), forName() which is used to find and load a class. It can also provide methods like Class.newInstance() which is the backbone of reflection and allow us to create an instance of a class ... Read More

Must we implement all the methods in a class that implements an interface in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

9K+ Views

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.There are only two choices −Implement every method defined by the interface.Declare the class as an abstract class, as a result, forces you to ... Read More

How to set JAVA_HOME environment variables on Windows OS in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

2K+ Views

Once you have installed JDK version on your windows machine, you have to set up Environment Variables.Please find below steps to set the java pathGo to My Computer ---> Right Click on it ---> Advanced System Settings ---> Advanced Tab ---> Click on Environment VariablesNow you have to alter the ... Read More

How to rethrow an exception in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

19K+ Views

Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the ... Read More

Can a constructor throw an exception in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

6K+ Views

Yes, constructors are allowed to throw an exception in Java.A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class. Each ... Read More

Advertisements