☕️JAVA Language Community
Treatment for Long Method @javaCode☕️
📌Treatment📌
As a rule of thumb, if you feel the need to comment on something inside a method, you should take this code and put it in a new method. Even a single line can and should be split off into a separate method, if it requires explanations. And if the method has a descriptive name, nobody will need to look at the code to see what it does.
1. To reduce the length of a method body, use Extract Method.
2. If local variables and parameters interfere with extracting a method, use Replace Temp with Query, Introduce Parameter Object or Preserve Whole Object.
3. If none of the previous recipes help, try moving the entire method to a separate object via Replace Method with Method Object.
4. Conditional operators and loops are a good clue that code can be moved to a separate method. For conditionals, use Decompose Conditional. If loops are in the way, try Extract Method.
@javaCode☕️
As a rule of thumb, if you feel the need to comment on something inside a method, you should take this code and put it in a new method. Even a single line can and should be split off into a separate method, if it requires explanations. And if the method has a descriptive name, nobody will need to look at the code to see what it does.
1. To reduce the length of a method body, use Extract Method.
2. If local variables and parameters interfere with extracting a method, use Replace Temp with Query, Introduce Parameter Object or Preserve Whole Object.
3. If none of the previous recipes help, try moving the entire method to a separate object via Replace Method with Method Object.
4. Conditional operators and loops are a good clue that code can be moved to a separate method. For conditionals, use Decompose Conditional. If loops are in the way, try Extract Method.
@javaCode☕️
#Java_Interview_Question
7) What is classloader?
The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
@javaCode☕️
7) What is classloader?
The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
@javaCode☕️
#Java_Interview_Question
8) Is Empty .java file name a valid source file name?
Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:
//save by .java only
class A{
public static void main(String args[]){
System.out.println("Hello java");
}
}
//compile by javac .java
//run by java A
compile it by javac .java
run it by java A
@javaCode☕️
8) Is Empty .java file name a valid source file name?
Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:
//save by .java only
class A{
public static void main(String args[]){
System.out.println("Hello java");
}
}
//compile by javac .java
//run by java A
compile it by javac .java
run it by java A
@javaCode☕️
#Java_Interview_Question
10) If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?
It is empty. But not null.
@javaCode☕️
10) If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?
It is empty. But not null.
@javaCode☕️
#Design_Patterns
Design Patterns:
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
@javaCode☕️
Design Patterns:
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
@javaCode☕️
#Design_Patterns
Uses of Design Patterns
Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.
Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.
In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.
@javaCode☕️
Uses of Design Patterns
Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.
Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.
In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.
@javaCode☕️
#Java_Interview_Question
11) What if I write static public void instead of public static void?
Program compiles and runs properly.
@javaCode☕️
11) What if I write static public void instead of public static void?
Program compiles and runs properly.
@javaCode☕️
#Java_Interview_Question
12) What is the default value of the local variables?
The local variables are not initialized to any default value, neither primitives nor object references.
@javaCode☕️
12) What is the default value of the local variables?
The local variables are not initialized to any default value, neither primitives nor object references.
@javaCode☕️
#Java_Interview_Question
13) What is difference between object oriented programming language and object based programming language?
Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.
@javaCode☕️
13) What is difference between object oriented programming language and object based programming language?
Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.
@javaCode☕️
#Java_Interview_Question
14) What will be the initial value of an object reference which is defined as an instance variable?
The object references are all initialized to null in Java.
@javaCode☕️
14) What will be the initial value of an object reference which is defined as an instance variable?
The object references are all initialized to null in Java.
@javaCode☕️
#Design_Patterns
Creational design patterns
These design patterns are all about class instantiation. This pattern can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done.
@javaCode☕️
Creational design patterns
These design patterns are all about class instantiation. This pattern can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done.
@javaCode☕️
#Design_Patterns
🔴Creational design patterns🔴
✅Abstract Factory
Creates an instance of several families of classes
✅Builder
Separates object construction from its representation
✅Factory Method
Creates an instance of several derived classes
✅Object Pool
Avoid expensive acquisition and release of resources by recycling objects that are no longer in use
✅Prototype
A fully initialized instance to be copied or cloned
✅Singleton
A class of which only a single instance can exist
@javaCode☕️
🔴Creational design patterns🔴
✅Abstract Factory
Creates an instance of several families of classes
✅Builder
Separates object construction from its representation
✅Factory Method
Creates an instance of several derived classes
✅Object Pool
Avoid expensive acquisition and release of resources by recycling objects that are no longer in use
✅Prototype
A fully initialized instance to be copied or cloned
✅Singleton
A class of which only a single instance can exist
@javaCode☕️
#Java_Interview_Question
15) What is constructor?
Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.
@javaCode☕️
15) What is constructor?
Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 15) What is constructor? Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation. @javaCode☕️
More details:
👉Constructor in Java
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
👉Rules for creating java constructor
There are basically two rules defined for the constructor.
Constructor name must be same as its class name
Constructor must have no explicit return type
Types of java constructors
👉There are two types of constructors:
1️⃣Default constructor (no-arg constructor)
A constructor that have no parameter is known as default constructor.
2️⃣Parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
@javaCode☕️
👉Constructor in Java
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
👉Rules for creating java constructor
There are basically two rules defined for the constructor.
Constructor name must be same as its class name
Constructor must have no explicit return type
Types of java constructors
👉There are two types of constructors:
1️⃣Default constructor (no-arg constructor)
A constructor that have no parameter is known as default constructor.
2️⃣Parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
@javaCode☕️
#Design_Patterns
#Abstract_Factory_Design_Pattern
❓Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products".
The new operator considered harmful.
❓Problem
If an application is to be portable, it needs to encapsulate platform dependencies. These "platforms" might include: windowing system, operating system, database, etc. Too often, this encapsulation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.
🤓To be continued tomorrow...
@javaCode☕️
#Abstract_Factory_Design_Pattern
❓Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products".
The new operator considered harmful.
❓Problem
If an application is to be portable, it needs to encapsulate platform dependencies. These "platforms" might include: windowing system, operating system, database, etc. Too often, this encapsulation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.
🤓To be continued tomorrow...
@javaCode☕️
#Design_Patterns
#Abstract_Factory_Design_Pattern
❓Discussion
Provide a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The "factory" object has the responsibility for providing creation services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.
This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application - where it is instantiated. The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory.
Because the service provided by the factory object is so pervasive, it is routinely implemented as a Singleton.
@javaCode☕️
#Abstract_Factory_Design_Pattern
❓Discussion
Provide a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The "factory" object has the responsibility for providing creation services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.
This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application - where it is instantiated. The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory.
Because the service provided by the factory object is so pervasive, it is routinely implemented as a Singleton.
@javaCode☕️
#Design_Patterns
#Abstract_Factory_Design_Pattern
Structure
The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates the new operator and the concrete, platform-specific, product classes. Each "platform" is then modeled with a Factory derived class.
@javaCode☕️
#Abstract_Factory_Design_Pattern
Structure
The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates the new operator and the concrete, platform-specific, product classes. Each "platform" is then modeled with a Factory derived class.
@javaCode☕️