How is thread safety achieved in Java?
When a thread is working on an object, it prevents other threads to work on the same object. This is known as thread safety. It can be achieved using 4 ways-
- By Synchronization- Only one thread will be allowed to complete a task at a time.
- By the volatile keyword- The ‘volatile’ keyword is used to make sure the object can be used by multiple threads.
- By atomic variable- the atomic variables are used to make sure that the threads do not crash into each other.
What is a Java Singleton class?
In design pattern and Object Oriented Programming, a Singleton class is a class in Java that can have only one object. It tells that only one instance should be created and that should be used by other classes.
What is a constructor and what are its types?
Constructors are special methods that are used to initialize objects. It is called as soon as an object of a class is created. It is used to set initial values of attributes of objects. It has the same name as the class name. There are 3 types of constructors:
- Parameterized constructor.
What is the Collections Framework in Java?
Collections in Java is a Framework that is used for manipulating and performing operations on a group of objects. The Collections Framework consists of many classes like ArrayList, LinkedList, HashSet, etc., and interfaces like List, Queue, Dequeue, and Set interfaces.
Explain the Java Object class.
The Object class in Java is the super most class. It is the parent class for all the classes in Java. All classes indirectly or directly inherit from the Object class.
Explain the difference between interface and abstract class.
|Interface can support multiple inheritance.||Abstract class does not support multiple inheritance.|
|It can extend only to another interface.||It can extend only one Java class and can implement multiple interfaces.|
|It can have only abstract methods.||It can have both abstract and non-abstract methods.|
|They do not have access modifiers.||They have access modifiers.|
|Members of an interface are public by default.||An abstract class can have private or protected members.|
|Variables declared in an interface are final by default.||An abstract class can contain non-final methods.|
Explain the difference between the >> operator and >>> operator.
>> and >>> operators are used to right-shift the bits. The >> operator preserves the sign bit, whereas the >>> operator does not preserve the sign bit.
Explain the use of Generics in Java language.
Generics in Java allow us to create a single class, interface, and method that can be used with different objects. It helps in code reusability. When we create a class that can be used with any type of data, it is known as a Generics class. They are parameterized types.