☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Java_Interview_Question

80) What is difference between throw and throws?


1)throw is used to explicitly throw an exception.

throws is used to declare an exception.


2)checked exceptions can not be propagated with throw only.

checked exception can be propagated with throws.


3)throw is followed by an instance.

throws is followed by class.


4)throw is used within the method.

throws is used with the method signature.


5)You cannot throw multiple exception

@javaCode☕️
☕️JAVA Language Community
#Prototype_Design_Pattern @javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Example

The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

@javaCode☕️
#Java_Interview_Question

81) Can an exception be rethrown?

Yes.

@javaCode☕️
#Java_Interview_Question

82) Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?

Yes but only unchecked exception not checked.

@javaCode☕️
#Java_Interview_Question
More details:

👉ExceptionHandling with MethodOverriding in Java

There are many rules if we talk about methodoverriding with exception handling.

The Rules are as follows:

1️⃣If the superclass method does not declare an exception

If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.


2️⃣If the superclass method declares an exception

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Check list

1️⃣Add a clone() method to the existing "product" hierarchy.

2️⃣Design a "registry" that maintains a cache of prototypical objects. The registry could be encapsulated in a new Factory class, or in the base class of the "product" hierarchy.

3️⃣Design a factory method that: may (or may not) accept arguments, finds the correct prototype object, calls clone() on that object, and returns the result.

4️⃣The client replaces all references to the new operator with calls to the factory method.

@javaCode☕️
#Java_Interview_Question

83) What is exception propagation ?

Forwarding the exception object to the invoking method is known as exception propagation.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 83) What is exception propagation ? Forwarding the exception object to the invoking method is known as exception propagation. @javaCode☕️
More details:

👉Java Exception propagation

An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method,If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.This is called exception propagation.

▪️Rule: By default Unchecked Exceptions are forwarded in calling chain (propagated).

Exception can be handled in any method in call stack either in main() method,p() method,n() method or m() method.

▪️Rule: By default, Checked Exceptions are not forwarded in calling chain (propagated).

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Rules of thumb

1️⃣Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used properly. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.

2️⃣Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.

3️⃣Factory Method: creation through inheritance. Prototype: creation through delegation.

4️⃣Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

5️⃣Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize.
Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.

6️⃣Prototype co-opts one instance of a class for use as a breeder of all future instances.

7️⃣Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. In this context, Prototype can avoid expensive "creation from scratch", and support cheap cloning of a pre-initialized prototype.

8️⃣Prototype is unique among the other creational patterns in that it doesn't require a class – only an object. Object-oriented languages like Self and Omega that do away with classes completely rely on prototypes for creating new objects.

@javaCode☕️
#Java_Interview_Question

84) What is the meaning of immutable in terms of String?

The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.

@javaCode☕️
#Java_Interview_Question

85) Why string objects are immutable in java?

Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

@javaCode☕️
#Design_Patterns

#Singleton_Design_Pattern

👉Intent

Ensure a class has only one instance, and provide a global point of access to it.
Encapsulated "just-in-time initialization" or "initialization on first use".

@javaCode☕️
#Java_Interview_Question

86) How many ways we can create the string object?

There are two ways to create the string object, by string literal and by new keyword.

@javaCode☕️
👍1
#Java_Interview_Question

87) How many objects will be created in the following code?

String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object.

@javaCode☕️
#Design_Patterns

#Singleton_Design_Pattern

👉Problem

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

@javaCode☕️
#Java_Interview_Question

88) Why java uses the concept of string literal?

To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 88) Why java uses the concept of string literal? To make Java more memory efficient (because no new objects are created if it exists already in string constant pool). @javaCode☕️
More details:

Java String literal is created by using double quotes. For Example:

String s="welcome";
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:

String s1="Welcome";
String s2="Welcome";//will not create new instance

❗️Note: String objects are stored in a special memory area known as string constant pool.

@javaCode☕️