#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☕️
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☕️
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☕️
#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☕️
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☕️
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☕️
#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☕️
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☕️
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☕️
#Java_Interview_Question
89)How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
@javaCode☕️
89)How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Discussion
Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
Singleton should be considered only if all three of the following criteria are satisfied:
▪️Ownership of the single instance cannot be reasonably assigned
▪️Lazy initialization is desirable
▪️Global access is not otherwise provided for
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.
The Singleton pattern can be extended to support access to an application-specific number of instances.
The "static member function accessor" approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.
Deleting a Singleton class/instance is a non-trivial design problem. See "To Kill A Singleton" by John Vlissides for a discussion.
@javaCode☕️
#Singleton_Design_Pattern
👉Discussion
Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
Singleton should be considered only if all three of the following criteria are satisfied:
▪️Ownership of the single instance cannot be reasonably assigned
▪️Lazy initialization is desirable
▪️Global access is not otherwise provided for
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.
The Singleton pattern can be extended to support access to an application-specific number of instances.
The "static member function accessor" approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.
Deleting a Singleton class/instance is a non-trivial design problem. See "To Kill A Singleton" by John Vlissides for a discussion.
@javaCode☕️
#Java_Interview_Question
90) What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.
@javaCode☕️
90) What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.
@javaCode☕️
#Java_Interview_Question
91) What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.
@javaCode☕️
91) What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Java_Interview_Question
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
#Java_Interview_Question
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
☕️JAVA Language Community
#Singleton_Design_Pattern @javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Java_Interview_Question
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 94)What is nested class? A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class. @javaCode☕️
More details:
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Java_Interview_Question
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️