Posts

Showing posts from October, 2015

Difference between Comparator and Comparable in Java

Comparable interface: Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method. For example: public class Country implements Comparable{ @Override public int compareTo(Object arg0) { Country country=(Country) arg0; return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ; }} If any class implements comparable inteface then collection of that object can be sorted automatically using Collection.sort() or Arrays.sort().Object will be sort on the basis of compareTo method in that class. Objects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface. Comparator interface: Class whose objects to be sorted do not need to implement this interface.Some third class can implement this interface to sort.e.g.CountrySortByIdComparator class can i...

Multithreading and Concurrency Interview Questions and Answers

1. What do we understand by the term concurrency? Concurrency is the ability of a program to execute several computations simultaneously. This can be achieved by distributing the computations over the available CPU cores of a machine or even over different machines within the same network. 2. What is the difference between processes and threads? A process is an execution environment provided by the operating system that has its own set of private resources (e.g. memory, open files, etc.). Threads, in contrast to processes, live within a process and share their resources (memory, open files, etc.) with the other threads of the process. The ability to share resources between different threads makes thread more suitable for tasks where performance is a significant requirement. 3. In Java, what is a process and a thread? In Java, processes correspond to a running Java Virtual Machine (JVM) whereas threads live within the JVM and can be created and stopped by the Java application dynamicall...

40 Java Collections Interview Questions and Answers

Image
What is Java Collections Framework? List out some benefits of Collections framework? Collections are used in every programming language and initial java release contained few classes for collections: Vector , Stack , Hashtable , Array . But looking at the larger scope and usage, Java 1.2 came up with Collections Framework that group all the collections interfaces, implementations and algorithms. Java Collections have come through a long way with usage of Generics and Concurrent Collection classes for thread-safe operations. It also includes blocking interfaces and their implementations in java concurrent package. Some of the benefits of collections framework are: Reduced development effort by using core collection classes rather than implementing our own collection classes. Code quality is enhanced with the use of well tested collections framework classes. Reduced effort for code maintenance by using collection classes shipped with JDK. Reusability and Interoperability Wh...

69 Spring Interview Questions and Answers

1. What is Spring? Spring is an open source development framework for Enterprise Java . The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model . 2. What are benefits of Spring Framework? Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB. Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique . The objects give their dependencies instead of creating or looking for dependent objects. Aspect oriented (AOP): Spring supports Aspect oriented programming and separates application business logic from system services. Container: Spring contains and manages the life cycle and con...