Raja has Published 760 Articles

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

3K+ 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

3K+ 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

4K+ 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

Does Java support multi-dimensional Arrays?

raja

raja

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

402 Views

No, Java does not support multi-dimensional arrays.Java supports arrays of arrays.In Java, a two-dimensional array is nothing but, an array of one-dimensional arrays.                  int[][] arr = new int[2][4];The expression arr[i] selects the one-dimensional array and the expression arr[i][j] selects the element from ... Read More

What are the differences between an Exception class and an Error class in Java?

raja

raja

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

551 Views

Both an Exception class and an Error class are subclasses of java.lang.Throwable class, we can able to handle the exceptions at runtime but the errors we cannot handle.Exceptions are the objects representing the logical errors that occur at the run time and makes JVM enters into the state of "ambiguity".The ... Read More

How to handle the exception using UncaughtExceptionHandler in Java?

raja

raja

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

757 Views

The UncaughtExceptionHandler is an interface inside a Thread class. When the main thread is about to terminate due to an uncaught exception the java virtual machine will invoke the thread’s UncaughtExceptionHandler for a chance to perform some error handling like logging the exception to a file or uploading the log to the server before it ... Read More

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

raja

raja

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

323 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

7K+ 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

1K+ 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

16K+ 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

Advertisements