#Java_Interview_Question
71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.
@javaCode☕️
71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.
@javaCode☕️
#Design_Patterns
#Object_Pool_Design_Pattern
👉Check list
1️⃣Create ObjectPool class with private array of Objects inside
2️⃣Create acquire and release methods in ObjectPool class
3️⃣Make sure that your ObjectPool is Singleton
👉Rules of thumb
▪️The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.
▪️Object Pools are usually implemented as Singletons.
@javaCode☕️
#Object_Pool_Design_Pattern
👉Check list
1️⃣Create ObjectPool class with private array of Objects inside
2️⃣Create acquire and release methods in ObjectPool class
3️⃣Make sure that your ObjectPool is Singleton
👉Rules of thumb
▪️The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.
▪️Object Pools are usually implemented as Singletons.
@javaCode☕️
#Java_Interview_Question
72) What is static import ?
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.
@javaCode☕️
72) What is static import ?
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 72) What is static import ? By static import, we can access the static members of a class directly, there is no to qualify it with the class name. @javaCode☕️
More details:
👉Java Static Import
The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.
✅Advantage of static import:
Less coding is required if you have access any static member of a class oftenly.
❌Disadvantage of static import:
If you overuse the static import feature, it makes the program unreadable and unmaintainable.
@javaCode☕️
👉Java Static Import
The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.
✅Advantage of static import:
Less coding is required if you have access any static member of a class oftenly.
❌Disadvantage of static import:
If you overuse the static import feature, it makes the program unreadable and unmaintainable.
@javaCode☕️
☕️JAVA Language Community
More details: 👉Java Static Import The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name. ✅Advantage of static import: Less coding is required…
#Java_Interview_Question
👉What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.
@javaCode☕️
👉What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.
@javaCode☕️
#Java_Interview_Question
👉 #Exception_Handling in Java
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions.
👉What is exception
Dictionary Meaning: Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
👉What is exception handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
👉Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.
👉Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.
@javaCode☕️
👉 #Exception_Handling in Java
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions.
👉What is exception
Dictionary Meaning: Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
👉What is exception handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
👉Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.
👉Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.
@javaCode☕️
👍1
☕️JAVA Language Community
Hierarchy of Java Exception classes @javaCode☕️
#Java_Interview_Question
👉Types of Exception
There are mainly two types of exceptions:
checked and unchecked where error is considered as unchecked exception.
▪️The sun microsystem says there are three types of exceptions:
1️⃣Checked Exception
2️⃣Unchecked Exception
3️⃣Error
👉Difference between checked and unchecked exceptions
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
@javaCode☕️
👉Types of Exception
There are mainly two types of exceptions:
checked and unchecked where error is considered as unchecked exception.
▪️The sun microsystem says there are three types of exceptions:
1️⃣Checked Exception
2️⃣Unchecked Exception
3️⃣Error
👉Difference between checked and unchecked exceptions
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
@javaCode☕️
#Java_Interview_Question
👉Common scenarios where exceptions may occur
▪️There are given some scenarios where unchecked exceptions can occur. They are as follows:
1️⃣ArithmeticException
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;
//ArithmeticException
2️⃣NullPointerException
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());
//NullPointerException
3️⃣NumberFormatException
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);
//NumberFormatException
4️⃣ArrayIndexOutOfBoundsException
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException
@javaCode☕️
👉Common scenarios where exceptions may occur
▪️There are given some scenarios where unchecked exceptions can occur. They are as follows:
1️⃣ArithmeticException
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;
//ArithmeticException
2️⃣NullPointerException
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());
//NullPointerException
3️⃣NumberFormatException
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);
//NumberFormatException
4️⃣ArrayIndexOutOfBoundsException
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException
@javaCode☕️
#Java_Interview_Question
73) What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors .It is mainly used to handle checked exceptions.
@javaCode☕️
73) What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors .It is mainly used to handle checked exceptions.
@javaCode☕️
#Java_Interview_Question
74) What is difference between Checked Exception and Unchecked Exception?
1)Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.
@javaCode☕️
74) What is difference between Checked Exception and Unchecked Exception?
1)Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.
@javaCode☕️
#Design_Patterns
#Creational_patterns
#Prototype_Design_Pattern
👉Intent
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Co-opt one instance of a class for use as a breeder of all future instances.
The new operator considered harmful.
@javaCode☕️
#Creational_patterns
#Prototype_Design_Pattern
👉Intent
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Co-opt one instance of a class for use as a breeder of all future instances.
The new operator considered harmful.
@javaCode☕️
#Java_Interview_Question
76) Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.
@javaCode☕️
76) Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.
@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern
👉Problem
Application "hard wires" the class of object to create in each "new" expression.
@javaCode☕️
#Prototype_Design_Pattern
👉Problem
Application "hard wires" the class of object to create in each "new" expression.
@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern
👉Discussion
Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.
The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.
@javaCode☕️
#Prototype_Design_Pattern
👉Discussion
Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.
The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.
@javaCode☕️
#Prototype_Design_Pattern
👉Structure
The Factory knows how to find the correct Prototype, and each Product knows how to spawn new instances of itself.
@javaCode☕️
👉Structure
The Factory knows how to find the correct Prototype, and each Product knows how to spawn new instances of itself.
@javaCode☕️
#Java_Interview_Question
77) What is finally block?
finally block is a block that is always executed.
@javaCode☕️
77) What is finally block?
finally block is a block that is always executed.
@javaCode☕️
#Java_Interview_Question
78) Can finally block be used without catch?
Yes, by try block. finally must be followed by either try or catch.
@javaCode☕️
78) Can finally block be used without catch?
Yes, by try block. finally must be followed by either try or catch.
@javaCode☕️
#Java_Interview_Question
79) Is there any case when finally will not be executed?
finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
@javaCode☕️
79) Is there any case when finally will not be executed?
finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
@javaCode☕️