Core Java FAQ
About volatile keyword?
volatile is used to indicate that a variable's value will be modified by different threads.
Declaring a volatile Java variable means:
—————
What is Singleton ? and Double-checked locking ?
Need to follow :
* private constructor , can't create object from outside of the class
* Synchronized methods are used to ensure that the class is thread-safe.
* This class cannot be subclassed because the constructor is private. This may or may
not be a good thing depending on the resource being protected.
//Single-checked locking
//Thread 1 enters the synchronized block, and, before it can assign the singleton member variable, the thread is preempted. Subsequently, another thread can enter the if block. The second thread will wait for the first thread to finish, but we will still wind up with two distinct singleton instances
public static Sequence getInstance() {
if(singleton == null) {
synchronized(Sequence.class) {
singleton = new Sequence();
}
}
return singleton;
}
//final and best solution is
public class Sequence {
private static Sequence singleton;
private Sequence()
{
}
//Double-checked locking
//Imagine Thread 1 enters the synchronized block and is preempted.Subsequently, a second thread enters the if block.When Thread 1 exits the synchronized block, Thread 2 makes a second check to see if the singleton instance is still null.Since Thread 1 set the singleton member variable, Thread 2's second check will fail, and a second singleton will not be created.
public static Sequence getInstance() {
if(singleton == null) {
synchronized(Sequence.class) {
if(singleton == null) {
singleton = new Sequence();
}
}
}
return singleton;
}
}
—————
Sort an ArrayList?
—————
what is class loader in java ? explain the types of class loaders?
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of class loaders. Delegation is an important concept to understand when learning about class loaders.
—————
How many times "hello" will print in the following program ?
Answer:
Totally 75 times. ++i and i++, are both same in the for loop. The for loop operation i intialization,coditionchecking then exceute the block code, then increment or decrement. so inthis case if you put ++i or i++ means the value will be ramin same.
—————
What is the Output for the following program?
Question :
—————
What is the Output of the following program? (looks like previous one but not please check the code)
—————
Explain different way of using thread?
The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help.
—————
What is the difference between sleep(), wait() and suspend()?
—————
I want to create only one object for the class. What technique or design pattern you will use?
Make the class as single-ton class
(Single ton is a one of the java design pattern)
singleton class steps to remember
1.create private static global variable and intialize as null (this variable is going to have the instance of the class)
2.make the constructor private
3.create a static method (if thread based access means make this method as synchronized)
4. inside the static method check the global variable is null ,if null means create a new instance nd assign to global variable and return the object.
5.if the global vriable not null means the object already reated so return the object
6. Override the clone method and throw the exception inside on it.
Example:
—————
Topic: Core Java FAQ
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
Date: 23/12/2023
Subject: Mr.
—————
