Thursday 15 June 2023

How many ways are there to create object in java?

In Java, there are several ways to create objects. Here are the most common ways:

  1. Using the new keyword: The new keyword is used to create a new instance of a class. It invokes the class constructor and allocates memory for the object. For example:

    java
    MyClass obj = new MyClass();
  2. Using reflection: Reflection allows you to create objects dynamically at runtime. It provides classes like Class and Constructor that can be used to create objects without knowing the class name at compile-time. Here's an example:

    java
    Class<?> clazz = Class.forName("MyClass"); MyClass obj = (MyClass) clazz.newInstance();
  3. Using clone: The clone method creates a copy of an existing object. To use this approach, the class must implement the Cloneable interface and override the clone method. Here's an example:

    java
    MyClass originalObj = new MyClass(); MyClass clonedObj = originalObj.clone();
  4. Using deserialization: Deserialization is the process of converting an object from its serialized form (e.g., byte stream) back into an object. It allows you to recreate objects from stored data. Here's an example:

    java
    FileInputStream fileIn = new FileInputStream("object.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); MyClass obj = (MyClass) in.readObject(); in.close(); fileIn.close();
  5. Using static factory methods: Some classes provide static factory methods that create and return instances of the class. These methods have names like getInstance(), valueOf(), or newInstance(). They provide flexibility in object creation. Here's an example:

    java
    MyClass obj = MyClass.getInstance();

These are some common ways to create objects in Java. The appropriate method depends on the specific requirements of your application.

Wednesday 25 March 2020

Where to use Node.js

It’s a topic which every developer faces now a day. Every architect or developer having experience in java always reluctant to go for node.js and prefer java. Before inclining towards any language we need to understand the basic fundamental of that language. Understanding of basics will help us to find suitable language for our server which can fulfill our requirements best. The basic nature of java and node.js is different in way they handle the requests.
                                      Java is multi-threaded language where requests can be entertained by multiple threads so it uses the processor in better way. The basic programming nature of java in Synchronous. If it is executing a code, then it waits for its finish before going to next line of code. In simple words let’s take an example that we have two block of codes where in first block we have a HTTPS call and in second block we have code which logs a message in log file. The HTTPS call takes 5 seconds on an average so java code will wait on first code block for 5 seconds to receive the response and complete that call before proceed to second block which make the whole process a bit slow.


                        We can consider node.js as single thread language where whole application uses a single thread to fulfill all the requests on queue base architecture. All required actions are stored in queue and executed by single thread. The basic programming nature of node.js is asynchronous. If it is executing a code, then it does not wait for its finish before going to next line of code. In same example which we took for java that we have two block of codes where in first block we have a HTTPS call and in second block we have code which logs a message in log file. The HTTPS call takes 5 seconds on an average so node.js code will not wait on first code block for 5 seconds to receive the response and complete that call rather it execute it and proceed to second block the action which we require on response of first call is also queued.
                                       Node.js inherit this Asynchronous feature from JavaScript which makes it suitable for developing high IO operation, data intensive, non-blocking applications like Web-servers and node.js is not suited for application where high calculation, complex business logic or processor intensive operation are required as it has single thread which can get busy for executing that calculation.

Sunday 7 April 2019

Singleton Bean working in Spring

There are five main scopes for creating a bean in spring.
  1. singleton
  2. prototype
  3. request
  4. session
  5. global-session
 Singleton is default scope type if we don't mention any scope then its singleton by default. A general perception about spring singleton bean is that it behaves same as java singleton class but there are few fundamental differences. Spring IoC container creates only one object for singleton bean and store it in cache memory of all singleton beans. All requests for bean with same name or id will get the same object, It does not mean that class defined as bean is singleton also. lets take an example where we create two beans for same class with different bean name and id then spring will create two different objects in shared memory and will return object based upon name or id given in request.

@Configuration
public class AppConfig {
    @Bean(name={"sineltonTest"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest() {
        return new singeltonTest();
    }
   
    @Bean (name={"sineltonTest1"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest1() {
        return new singeltonTest();
    }
}

In above example spring will create two different objects for two beans ("sineltonTest", "sineltonTest1") in shared cache and if we use @Autowired with @Qualifire("singeltonTest1") then it will get object from cache created for bean with name "sineltonTest1" so bean is sigleton not the class. There is one more catch w.r.t singleton bean in spring that what happen when we create object of that class with new operator instead of @autowired? This question is asked basically to confuse the person in interview. The clear answer is that spring will not return object from cache now it depends upon exact implementation of the bean class if it is created as java singleton class then one instance is created and every time same instance will be returned otherwise every time new instance is created and returned.