Friday, 31 May 2019

How to create immutable class in Java

An immutable class is one whose state can not be changed once created. There are certain guidelines to create a class immutable in Java.

1. Rules to create immutable classes

Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.

Why Strings are Immutable in Java?

An immutable object is an object which state is guaranteed to stay identical over its entire lifetime. This is really a good definition. Isn’t it? It means that the state of object once initialized, can never be changed anyhow.

What is Marker interfaces in Java and why required

Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. Example of market interface is Serializable, Clonnable and Remote interface. Now if marker interface doesn't have any field or method or behavior they why would Java needs it?

Wednesday, 29 May 2019

important Interface interview questions

1) Can abstract class have constructors in Java?

Yes, abstract class can declare and define constructor in Java. Since you can not create instance of abstract class,  constructor can only be called during constructor chaining, i.e. when you create instance of concrete implementation class.

What is a Functional interface in Java 8? What does @Functional annotation do?

What is Functional interface in Java 8

Well, a functional interface is nothing but an interface with just one abstract method e.g. Comparable, Runnable, EventListener, Comparator, etc. You can see these interfaces were present in Java even before JDK 8, but why do we call such interface functional interface?

Tuesday, 14 May 2019

Explain Function Overloading and Method Overloading in Java

When in a class, we have more then one method with similar name but with different type signatures i.e. with different number of parameters or with different types of parameters, then we say that the method is overloaded. Compiler will recognize which method to execute on the basis of the type and

What is Abstraction in Java? Abstract Class or Interface

Abstraction in Java: The words "data abstraction" and "information hiding" are generally used interchangeably. The two terms mean the same thing in this context. Abstraction is a simple representation of a complicated situation. It is a technique where we hide irrelevant details and represent only the essential aspects of a context so that one can focus on features one is interested; It helps to deal a complex system by concentrating on the essential features only. It is designed to make

Explain Association, Composition and Aggregation in Java

Association is nothing more than  a connection that exists between two classes.  We use the objects of these classes to set up the connection. Association can be  done in three ways one-to-one, one-to-many and many-to-many. Let’s have a look at an example showing how association is implemented in Java.

Sunday, 12 May 2019

Can you explain how Strings are interned in Java?

String class is designed with the Flyweight design pattern in mind. Flyweight is all about re-usability without having to create too many objects in memory.

Q1. What is the difference between “==” and “equals(…)” in comparing Java String objects?

When you use “==” (i.e. shallow comparison), you are actually comparing the two object references to see if they point to the same object. When you use “equals(…)”, which is a “deep comparison” that

Sunday, 5 May 2019

Spring Annotations [ Quick Reference ]

@Import Annotation in Spring Framework

@Import is the one such annotation used for consolidating all the configurations defined in various configuration files using @Configuration annotation. It is much similar to how we import the different XML configuration files to a single file. @Import annotation will used for the same purpose. This tutorial explains how to use @Import for Importing JavaConfig Files in Spring Projects.

How To Load Properties File using @Value Annotation

This example shows how to load the properties file values using the @Value annotation. Accessing the property file values in the Spring application involves the following steps:

Add the property file details to spring bean configuration file. You have to use the “classpath” prefix if you want to load the files from the classpath.

Saturday, 4 May 2019

Explain @RequestMapping Annotation in Spring MVC

@RequestMapping annotation is used for mapping web requests to particular handler classes or handler methods. The classes or methods that are annotated with @RequestMapping will be scanned and registered as the URI mappings for that specific class or method. Any future web requests will be redirected to that class or method. This annotation is part of Spring MVC module.

What is Difference Between @RequestParam and @PathVariable in Spring MVC

@RequestParam and @PathVariable

@RequestParam and @PathVariable annotations are used for accessing the values from the request. Both the the annotations have the significant purpose and use respectively. The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.

Explain Spring MVC Annotations @Controller @InitBinder @RequestMapping @RequestParam @SessionAttributes

Annotations are introduced from the Java 5.o release. Annotations brought one of the major change to the Java programming style by eliminating the XML configurations and replaced with @ symbol annotations for everything. Since then, all the major Java frameworks have started implementing the annotations support for their release. Spring Framework has started using the annotations from the release Spring 2.5. This tutorial lists some of the mostly used annotations in the Spring MVC module. The following are the list of spring mvc annotations which is specific to the Spring MVC module:

Explain @PathVariable – URI Template Patterns in Spring MVC

This tutorial explains how to customize / template pattern on  request URI for Spring MVC web application. @RequestMapping annotation used for defining the request URL for the class level and method levels. It is generic annotation where developer can configure the relative paths inside application context. However, it doesn’t offer flexible URI patterns on its own. We have to use @PathVariable for accepting the customized or more dynamic parameters in the request paths. You can configure the complete path inside @RequestMapping with the placeholders for the dynamic URIs.

Explain @Configuration Annotation in Spring

Spring 3.0 has introduced new way for configuring the spring beans. In fact this option was maintained by spring framework separately as JavaConfig project. Later this project is merged to the spring core framework and all the features are directly accessible from the spring core.

Explain @Profile Annotation in Spring 4

If you are working for a real time projects, it is often basic requirement to have the different environments like development, testing and production. In this scenario, the underlying resources (ie, database, file system, etc) for each environment would be different. The challenges arises when you want to move your source code to different environments. You would have to adjust the configurations to point to new environment details like Database, etc. The solution for the above problem is to create different profiles for each environment and enable them when you move to another environment.

Explain @Query Annotation in Spring Data JPA



In this tutorial, I am going to explain @Query annotation and how to create custom query using the @Query annotation.

The greatest advantage of using Spring Data is that it implements the repositories at run time for creating the queries. Developer need not worry about the SQL knowledge and writing error prone

Explain @Conditional Annotation in Spring 4.0



In this article I shall throw some light on one of the new features of Spring 4, Conditional Annotation Type. In the earlier versions of Spring you could handle conditions as below:

In Spring with versions earlier to 3.1, you had to use Spring Expression Language (SPeL).
With Spring 3.1, a new feature called profiles was introduced which helped us to handle conditions.
Let us see each of these approaches and then understand the Conditional Annotation type in Spring 4.

Explain @ModelAttribute in Spring MVC



This annotation can be used as the method arguments or before the method declaration. The primary objective of this annotation to bind the request parameters or form fields to an model object. The model object can be formed using the request parameters (as shown below in the example) or already stored in the session object. Note that, this @ModelAttribute methods are invoked before the controller methods with @RequestMapping are invoked. The logic behind the sequence is that, the model object has to be created before any processing starts inside the controller methods.

Explain @ControllerAdvice in Spring



Spring 3.2 introduces new annotation @ControllerAdvice annotation used for defining the exception handler with specific exception details for each method If any exception thrown on any part of the application will be handled by this component. In the previous release, we have configured GlobalException handling elements in the configuration files. With this new feature, it becomes more simple to handle the exceptions. Lets look at the example below to know how to implement in your project.

Explain @RequestHeader in Spring MVC


Every request has a request header part with the set of details sent to the web server. Typically request headers contains the details about browser version, language, what it will accept from the server, etc. are sent to inform the server. This details are many ways useful to the servers for sending back the response. As a developer, we use those details to create the response which is more suitable for that specific request.

Explain RestController Annotation in Spring


@RestController introduced as part Spring 4 release. The spring 4 version comes up with lot of exciting new features. One of the API improvements is new RestController annotation which is inherited from the @Controller annotation.

Explain @Resource, @PostConstruct and @PreDestroy Annotations Example


 Introduced in Spring 2.5, it added support for JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations. In my previous articles I have explained about the some of the popular annotations in Spring @Required, @Autowired and @Qualifier. Also please read about How to write Custom Spring Callback Methods?

Explain @Inject and @Named Annotations with Example


Support for JSR-330 annotations was introduced in Spring 3. These annotations are scanned the same way as the Spring annotations, only requirement would be to have the relevant jars in your classpath. You can use the following annotations in Spring 3 applications:

@Inject instead of Spring’s @Autowired to inject a bean.

What is Difference Between @Resource, @Autowired and @Inject in Spring Injection

Difference Between @Resource, @Autowired and @Inject in Spring Injection

It is very common confusion among the spring developers that what is the real difference between these three (@Resource, @Autowired and @Inject) annotations used for injecting the objects. I have come across this question from our readers so dedicated this post for explaining the main difference between these three annotations. Infact, these three work very much similar in most of the cases, there is slight differnce in few cases. I would explain that in this post. lets start from the basic details about these three annotations.

Explain @Required Annotation in Spring

@Required Annotation in Spring

Since Spring 2.5, annotation-based configuration has been an alternative to XML setups. Annotation based configuration rely on the bytecode metadata for wiring up components instead of angle-bracket declarations (Also Read : Introduction to Spring Boot). Annotations can be used on the relevant class, method, or field declaration.

Friday, 3 May 2019

Explain @Qualifier Annotation in Spring

@Qualifier Annotation in Spring

@Qualifier which helps fine-tune annotation-based autowiring. In the previous post we saw how we could use @Autowired annotation on setter methods, fields and constructors. There may be scenarios when we create more than one bean of the same type and want to wire only one of them with a property. This can be controlled using @Qualifier annotation along with the @Autowired annotation

Explain @Autowired Annotation in Spring

@Autowired Annotation in Spring

Spring bean dependencies are defined in the XML files the same can be automatically detected by the Spring container by using the @Autowired annotation. This would eliminate using the XML configurations. Introduced in Spring 2.5, the @Autowired annotation can be applied to

Explain Spring MVC Annotations : @Component, @Repository, @Service

Spring MVC Annotations : @Component, @Repository, @Service

@Component



Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...