We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept
What is a Scoped service?

A scoped service instance is created only once within a request scope and is reused for all calls within that request.

services.AddScoped<IMyClass, MyClass>();

ASP.NET Core •  Added 3 months ago

What is a Transient service?

A transient service instance is created each time a calling component method or dependent class requests for an instance from the container.

services.AddTransient<IMyClass, MyClass>();

ASP.NET Core •  Added 3 months ago

What is a Singleton service?

A singleton service instance is created only once during entire application lifetime, common across all requests.

services.AddSingleton<IMyClass, MyClass>();

ASP.NET Core •  Added 3 months ago

What are service lifetimes?

A service lifetime defines how a service instance is maintained or created by the IoC container when a calling method or constructor requests for a new service instance. There are three types of services based on how their instance lifetime is managed:

  1. Singleton: New instance is created only once during its entire lifetime, across requests
  2. Transient: New Instance created each time a component requests for an object
  3. Scoped: New instance is created only once per request and is reused for all calls within a request

ASP.NET Core •  Added 3 months ago

How do you register services?

A class can be registered as a service by adding it to the IServiceCollection instance under the ConfigureServices() method in the Startup class.

A class MyClass can be registered as a service in the below ways:

services.AddT<MyClass>(new MyClass()); (or)
services.AddT<new MyClass());
services.AddT<IMyClass, MyClass>(); (or)
services.AddT<IMyClass>(new MyClass());

Where T can be Singleton, Transient or Scoped.

ASP.NET Core •  Added 3 months ago

What is meant by a service?

A service is an interface which is resolved for a configured concrete implementation with a specified lifetime, which is managed by the IoC container and is injected via constructor or method whenever requested by the calling method or class.

It is configured using the ConfigureServices() method in the Startup class.

ASP.NET Core •  Added 3 months ago

why is string is immutable in java?
  • In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified.
  • Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object.
  • Java String objects are immutable as String objects are generally cached in the String pool.
  • Since String literals are usually shared between multiple clients, action from one client might affect the rest.
  • It enhances security, caching, synchronization, and performance of the application.

Java Concepts •  Added 3 months ago

What is the difference between this() and super() in Java?

In Java, super() and this(), both are special keywords that are used to call the constructor.

this:

  1. this() represents the current instance of a class
  2. Used to call the default constructor of the same class
  3. Used to access methods of the current class
  4. Used for pointing the current class instance
  5. Must be the first line of a block

super:

  1. super() represents the current instance of a parent/base class
  2. Used to call the default constructor of the parent/base class
  3. Used to access methods of the base class
  4. Used for pointing the superclass instance
  5. Must be the first line of a block

Java Concepts •  Added 3 months ago

What is meant by Object Oriented Programming

Object-Oriented Programming or OOPs is a programming style which focuses on the below core features:

  • Inheritance : Inheritance is a process where one class acquires the properties of another.
  • Encapsulation : Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
  • Abstraction : Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
  • Polymorphism : Polymorphism is the ability of a variable, function or object to take multiple forms

OOP •  Added 3 months ago

What is the difference between equals() and == in Java?
  • Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
  • ''=='' or double equal operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects.
  • public boolean equals(Object o) is the method provided by the Object class.
  • The default implementation uses == operator to compare two objects.
  • For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Java Concepts •  Added 3 months ago

What is the difference between a local variable and an instance variable?

Local Variable: a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class wont be even aware of that variable.

Instance Variable: a variable which is bounded to its object itself. These variables are declared within a class, but outside a method. Every object of that class will create its own copy of the variable while using it. Thus, any changes made to the variable wont reflect in any other instances of that class and will be bound to that particular instance only.

Java Concepts •  Added 3 months ago

Can you explain the signature of main method in Java

Signature of main() method:

public static void main(String[] args)
{
   // code comes here
}

Public: It is an Access modifier, which specifies from where and who can access the method

Static: by making class as static,JVM initialize main method with out creating any object for that

Main: The point from where the program starts its execution

Void: It is a keyword and used to specify that a method doesn’t return anything

String[] args: It stores Java command line arguments and is an array of type java.lang.String class

Java Concepts •  Added 3 months ago

What are the differences between the constructors and methods?

Constructor:

  • A constructor is used to initialize the state of an object.
  • A constructor is invoked implicitly.
  • A constructor must not have a return type.
  • The Java compiler provides a default constructor if you don't have any constructor in a class.
  • The constructor name must be same as the class name.

Method:

  • A method is used to expose the behavior of an object.
  • A method must have a return type.
  • The method is invoked explicitly.
  • The method is not provided by the compiler in any case.
  • The method name may or may not be same as class name.

Java Concepts OOP •  Added 3 months ago

What is a constructor?

A constructor is a "kind of" method in java which is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. A constructor definition is similar to a method definition, except that the constructor should have same name as the class and doesn't contain a return type.

public class MyClass {
    public MyClass() {
      // class level definitions
    }
}

Java Concepts •  Added 3 months ago

What is an object?

In general sense, Object can be assumed as a blue print of the class, which is having the instance variables as the state of the object and the methods as the behavior of the object.

OOP •  Added 3 months ago