Posts

Spring Batch Interview Questions

Image
Q:Explain spring batch framework. A:  Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems. Spring Batch builds upon the productivity, POJO-based development approach, and general ease of use capabilities people have come to know from the Spring Framework, while making it easy for developers to access and leverage more advanced enterprise services when necessary. Q:When to use Spring Batch? A:  Consider an environment where users have to do a lot of batch processing. This will be quite different from a typical web application which has to work 24/7. But in classic environments it’s not unusual to do the heavy lifting for example during the night when there are no regular users using your system. Batch processing includes typical tasks like reading and writing to files, transforming data, reading from or writing to databases, create reports, import ...

Java Tricky Questions

Image
1) Why String is immutable in java? Three reasons: 1) String pool requires string to be immutable otherwise shared reference can be changed from anywhere. 2) security because string is shared on different area like file system, networking connection, database connection , having immutable string allows you to be secure and safe because no one can change reference of string once it gets created. if string had been mutable anyone can surpass the security be logging in someone else name and then later modifying file belongs to other. 2) Why multiple inheritances are not supported in Java? Short answer is because of diamond pattern, diamond pattern creates ambiguity and make problem for compiler. Anyway java supports multiple inheritances via interfaces.I think more convincing reason for not supporting multiple inheritance is complexity involved in constructor chaining, casting etc rather than diamond problem. 3) How to detect deadlock and fix it? when two or more threads waiting for each ...

Java 8 Supplier examples

In this post, we are going to see about java 8 Supplier interface. Supplier is functional interface which does not take any argument and produces result of  type T .It has a functional method called  T get()  As Supplier is functional interface, so it can be used as assignment target for lambda expressions. Here is source code of Java 8 supplier interface. package java.util. function ; /** * Represents a supplier of results. * * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href=”package-summary.html”>functional interface</a> * whose functional method is {@link #get()}. * * @param <T> the type of results supplied by this supplier * * @since 1.8 */ @FunctionalInterface public interface Supplier<T> {      /**      * Gets a result.      *    ...

Java 8 Consumer examples

Consumer definition Consumer  takes single argument and do not return any result. Here is the definition of  Consumer interface. @ FunctionalInterfacepublic interface Consumer<T> { /** * * Performs this operation on the given argument. * * @param t the input * argument */ void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } It has a functional method called  accept()  and default method andThen() . Consumer examples accept() method example Let’s use Consumer interface to print String: mport java.util. function .Consumer;   public class Java8ConsumerExample {   public static void main( String [] args) {     Consumer< String > consumerString=s->System.out.println(s);   consumerString.accept(“Arpit”); } } We have created  consumer  object which takes  String  ob...

Java 8 Predicate examples

Predicate  is single argument functional interface which returns true or false. It takes one argument and returns result in form of true or false.         // Using anonymous class          Predicate< Integer > predicate= new Predicate< Integer >() {              @Override            public boolean test( Integer i) {                  return i > 100;            }         };           // Using lambda expression          Predicate< Integer > predicate = i -> i > 100;  Here is ...

Java 8 Optional

Image
Did you ever get  NullPointerException  as Java developer? If you are experienced Java developer, you might have got NullPointerException at some point of time. You might agree that NullPointerException is pain for novice or expert core Java developer. You have to put a lot of defensive code in case you want to avoid NullPointerException. Let’s see with the help of an example. public class Employee { private String name; private int age; public Employee(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } Create a main class named “JavaFindEmployeeMain.java” public class JavaFindEmployeeMain { public static void main(String[] args) { List<Employee> employeeList = createEmployeeList (); Employee employee = findEmployee (employeeList, “Adam”); S...

Java 8 interview questions and answers

Image
What are new features which got introduced in Java 8? What are main advantages of using Java 8? What is lambda expression? Can you explain the syntax of Lambda expression? What are functional interfaces? How lambda expression and functional interfaces are related? Can you create your own functional interface? What is method reference in java 8? What is Optional? Why and how can you use it? What are defaults methods? What is the difference between Predicate and Function? Are you aware of Date and Time API introduced in Java 8? What the issues with Old Date and time API? Can you provide some APIs of Java 8 Date and TIme? How will you get current date and time using Java 8 Date and TIme API? Do we have PermGen in Java 8? Are you aware of MetaSpace? Given a list of employees, you need to filter all the employee whose age is greater than 20 and print the employee names.(Java 8 APIs only) Given the list of employees, count number of employees with age 25? Given the list of employees, find t...