Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by raja
Page 38 of 38
How to make a class thread-safe in Java?
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 it explicitly.Example:import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Iterator; public class HashMapSyncExample { public static void main(String args[]) { HashMap hmap = new HashMap(); hmap.put(2, "Raja"); hmap.put(44, "Archana"); hmap.put(1, "Krishna"); ...
Read MoreHow to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\\n
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 same time.StringBuilder(Non-thread-safe)StringBuilder is not synchronized so that it is not thread-safe. By not being synchronized, the performance of StringBuilder can be better than StringBuffer.If we are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance. This is also true of other situations such as ...
Read MoreCan we define a static constructor in Java?
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 and opposite to each other. We need to assign initial values for an instance variable we can use a constructor. We need to assign static variables we can use Static Blocks.ExampleLive Demopublic class StaticConstructorTest { int x = 10; // Declaratiopn of Static Constructor static StaticConstructorTest() { ...
Read MoreHow to resize an array in Java?
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) { int[] a = {1, 2, 3}; a = (int[])resizeArray(a, 5); a[3] = 4; a[4] = 5; for (int i=0; i 0) System.arraycopy(oldArray, 0, newArray, 0, preserveLength); return newArray; } } Output:1 2 3 4 5
Read MoreWhat is the importance of "Java.lang.Class" in Java?
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 without using new() operator.Importance of java.lang.ClassInstances of the class Class represent classes, interfaces, enum and annotation in a running Java application.Whenever a java file is compiled, the compiler will insert a public, static, final field named Class of the type java.lang.Class into generated .class fileEach and every class exposes its code in the form of an ...
Read MoreMust we implement all the methods in a class that implements an interface in Java?
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 subclass the class (and implement the missing methods) before you can create any objects.The only case the class do not need to implement all methods in the interface is when any class in its inheritance tree has already provided concrete (i.e. non-abstract) method implementations then the subclass is under no ...
Read MoreHow to set JAVA_HOME environment variables on Windows OS in Java?\\n
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 “Path” variable under system variables such that it contains a path to Java Environment. Select the path variable and click on the “Edit” buttonBy default, Java is installed in “C:\Program Files\Java\jre version\bin” in case you have changed the location of installation, then add that pathClick on OK button and now ...
Read MoreHow to rethrow an exception in Java?\\n
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 rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) { System.out.println("An exception ...
Read MoreCan a constructor throw an exception in Java?
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 object of a class will have its own state (Instance variables) and access to methods of its class.Throw an Exception from a ConstructorA checked exception can be used to indicate a legitimate problem when trying to create an instance, while an unchecked exception typically indicates a bug either in the ...
Read More