Posts

Java's Magic: Bytecode

Image
The most powerful feature of java is platform independent.Platform independent is COMPILE ONCE, RUN ANYWHERE. It means you can compile your source code on one platform and run on any platform. The magic behind this feature is Bytecode. When you compile your code using javac command, compiler doesn't generate machine/platform specific code. It generates .class file. This .class file bytecode. You can run this .class file on any platform.

enums in Java

We can use enum to define a group of named constants. By using enum we can define our own data types. enum concept introduced in 1.5v. Java's enum are more powerful than enum in other languages. Declaration enum enum concept is internally implemented using class concept. Syntax of enum declaration are almost same as class declaration. enum keyword is used for declaring enums. //EXAMPLE CODE-1 public enum Month{ JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC; } In above example, We have declared Month as enum and JAN, FEB, MAR ...DEC are constants of type Month. A enum can have instance variables, method and constructor. Instance variables, method and constructor must be declared after enum constants declaration. If there are parameterised constructors in enum then enum constant must have these parameters. //EXAMPLE CODE-2 public enum Day { SUN("Sunday"), MON("Monday"), TUE("Tuesday"), WED("Wednesday"), THU(...

Package

This is encapsulation mechanism to group related classes and interfaces in single module. The main purposes of package are –           To resolved naming conflict           To improve modularity of program           To provide security by preventing access of classes and interface to outside of package  Rules –           If package is available in java file then first non-comment code should be package.           There can be only one package name in java file. Note – As java naming convention, package name should be reverse of internet domain name. eg -  com.icicibank.loan housingloan You can create class which is not in package. But it is always recommended that you should declare a class in package.

Java Interview Questions

What is difference between Hashtable and HashMap classes in Java ? What is CuncurrentMap? What is Fail fast iterator and fail safe iterator? How Fail fast iterator knows that the internal structure of collection is modified? What is priority query? What is annotations? how to create custom annotation and use it? What is dependency injection? What is use of Junit? What is AOP? Explain its terminology? What is design pattern & why should we use them? Explain different type of design patterns. What is file in Java? What is difference between OOP and AOP? What is difference between  application server and web server? What is difference between SOAP and RESTful web services? What are implicit objects of JSP? How to integrate Hibernate with Spring? What is difference between logical and physical transaction? Explain Memory management in Java. Explain memory management of String in java. What is wait() and notify() methods? Explain JVM architecture? What is St...

What is IoC Container in Spring

Spring Container is core of Spring framework. Spring Container is responsible for instantiating beans, configure  and assemble them together. It manage their compete life-cycle from creation to destruction. The Container gets its instruction on what object to instantiate, configure and assemble by reading configuration metadata. The configuration  metadata is represented by XML, Java annotation and Java code. There are two types of IoC Container in Spring- BeanFactory ApplicationContext 1. BeanFactory : BeanFactory is simplest container. This is defined by org.springframework.bean.factory.BeanFactory interface. The most commonly used BeanFactory implementation is the XmlBeanFactory class . BeanFactory instantiate bean when getBean("") method is called. 2. ApplicationContext : ApplicationContext is more advanced container. This is defined by org.springframework.context.ApplicationContext interface.This interface extends org.springframework.bean.factory.BeanFactor...

What is Dependency Injection

Image
Dependency Injection is style of object configuration in which Object fields and collaborators are set by external entity. Dependency Injection is alternation to having object configure itself. Dependency Injection design pattern allows us to remove hard-coded  dependency and make our application loosely coupled, extensible and mantainable. Below class diagram explains Dependency Injection Traveler: public class Traveler { private Vehicle vehicle; public void setVehicle(Vehicle vehicle) { this.vehicle = vehicle; } public void travel(){ vehicle.start(); vehicle.run(); vehicle.stop(); } } Vehicle: public interface Vehicle { void start(); void run(); void stop(); } public class Bike implements Vehicle{ @Override public void start() { System.out.println("Bike is started"); } @Override public void run() { System.out.println("Bike is runnig"); } @Override public void stop() { System.out.println("Bike is stop...

What is difference between HashMap and Hashtable classs

Synchronization & Thread Safe - All methods in Hashtable are synchronized and thread safe whereas methods in HashMap are not synchronized and it is not thread safe. HashMap is more faster than Hashtable because its methods are not synchronized. Null Key & Null Value - HashMap allows one null key and any number of null values whereas null key and null value is not allowed in Hashtable. Hashtable's put method will throw NullPointerException if null is used as key or value Iterating the values - HashMap uses Iterator to iterate over the values whereas Hashtable uses Enumerator. Super class & Lagacy - Hashtable is subclass of Dictionary class which is now obsolete in JDK1.7. HashMap is sub class of AbstractMap Note :-  It is better of externally synchronize a HashMap or Using concurrentMap implementation. (i.e. ConcurrentHashMap)