101 Flashcards
What is wrong in the below method signature:-
public boolean default isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}
The default keyword should be first or second in the signature
public default boolean isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}
OR
default public boolean isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}
Qn.
- Is Java.lang.Runtime Class Singleton?
- Are Enums Singleton?
3.
Ans.
- Yes
- Yes
How Can you write the basic Singleton class ?
public class Singleton{
private static Singleton singleton = new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
return singleton;
}
}
Can we have a class inside a method or Constructor
Yes
And it should be only Final or Abstract
What is wrong in the below Singleton class code ?
class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() { }
public static Singleton getInstance()
{ return instance; }
}
The instance will get initiallized when the App Starts, it should be lazy loaded
How can you get Singleton initiallized when the App Starts Or make it lazy loaded?
use a static inner class
class Singleton {
private Singleton() { }
private static class Holder {
private static final Singleton instance =
new Singleton();
}
public static Singleton getInstance() {
return Holder.instance;
}
}
Why a class inside a method or Const should be final or abstract
Because I think the overridden should not be a problem
**
What is wrong in below approach?
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
// more code to create the instance…
}
return instance;
}
}
It is not thread safe
What is wrong in the below approach ?
class Singleton {
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
The problem is that every time the method is called it will cause more than one thread to wait
How to improve the below code
class Singleton {
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
Seperate the creation into a sync block
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
What is the problem in the below code
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
, the problem is at the Java Virtual Machine (JVM) level. The JVM, or sometimes the compiler, can opti- mize the code by reordering or caching the value of variables (and not making the updates visible).
If java caching is a problem in below sort of code, then how we can improve this ?
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
The solution is to use the volatile keyword, which guarantees that any read/write
operation of a variable shared by many threads would be atomic and not cached.
class Singleton {
private static volatile Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
Give Example of mutable class in Java
java.lang.String
Wrapper Classes
Is Java Big Classes are immutable
No
What is the only way to set the value of an immutable Object?
Constrtr
What are the 5 Properties of an immutable class
- It does not have setter
- Its value or attributes can only be initiallized thru constrtr
- Its all attributes are private and final(sometimes)
- It hides the access modifiers of all its member variables
- It is declared final
To which relationship interface resembles?
Is-A
A Package resembles a Set, Yes Or No
Yes
State Liskov`s Substitution Principle
If a subclass cannot inherit all methods of parent class, the subclass cannot be substitued places
Can we call a constrtr into another, If Yes what is the only way?
We can use this(), if use name, the compiler will assume it as method