Uncategorized

Core Java Questions

What is JVM? Java Virtual Machine is an abstract machine which enable to run a Java program. Like a real computing machine, it has an instruction set and manipulates various memory areas at run time. The Java Virtual Machine knows nothing of the Java programming language, only of a particular binary format, the class file format. What is a .class file? A .class file contains Java Virtual Machine instructions or byte codes and a symbol table, as well as other additional information. What is the different in Thread and Process? – A program in execution is often referred as process. A thread is a subset of a process. – A process consists of multiple threads. A thread is the smallest part of the process that can execute concurrently with other parts(threads) of the process. – A process is sometime referred as task. A thread is often referred as lightweight process….

Generics Wild Cards

There could be various scenarios where user needs to perform one operation over all objects belongs to similar type. Following example shows the limitation of the scenario: class Group { List group; public Group() { group = new ArrayList(); } public E get(int index){ return this.group.get(index); } public void add(E e){ this.group.add(e); } public List getAll(){ return group; } } class Relation{ private String name; public Relation(String name){ this.name = name; } public void sendInvitation(String event){ System.out.println(“Invitation is sent to “+name+” for the event on “+event); } } class Friend extends Relation{ public Friend(String name) { super(name); } } class Colleague extends Relation{ public Colleague(String name) { super(name); } } public static void main(String[] args) { Group friendsGroup = new Group(); friendsGroup.add(new Friend(“Ajit”)); friendsGroup.add(new Friend(“Raj”)); friendsGroup.add(new Colleague(“Mohit”)); … } Here user has two different groups where one is personal friends group and others is colleagues from office. Now he has…

Generic Methods

Generics can be applied at method level. Generic methods are really helpful when there are some collections operation has to be performed e.g. delete, add, update etc… public void add(T obj, List list){ … list.add(obj); … } public T getData(int index){ this.list.get(index); } To be able to create Generic methods, you must need to define Generic types at Class level. Below example will give you clear picture of generic methods class Group { List group; public Group() { group = new ArrayList(); } public E get(int index){ return this.group.get(index); } public void add(E e){ this.group.add(e); } } class Friends{ … } class Colleagues{ … } public static void main(String[] args) { Group friendsGroup = new Group(); friendsGroup.add(new Friends()); Friends emp = friendsGroup.get(0); System.out.println(emp); } Generic methods are usually implemented where there is a collection handing objects (CRUD) within the class. You won’t be able to invoke any instance method from…

Generics

Why Generics? To minimize Runtime issues during development of a Project. These issues are hidden and takes good amount of time to find and fix the issues Type-safe Programming which improves accuracy in coding. Different ways of Using Generics Generic Class or Interface Multi-type Parameters Generic Methods Wild Cards usage Generic Class or Interface Purpose of creating Generic class or Interface is to maintain specific parameterized object and manipulate it within Class’s boundary. Syntax of Generic class is: class GenericClass<T1, T2… Tn> { … } In above systax T1, T2 are the types of parameters which can be used anywhere within the class. You can specify ‘n’ number of parameters to define a Generic type class. Here is an example of non-generic and generic type class Generic Class Non-Generic Class public class Foo<O> { private O o; public Foo(O o){ this.o = o; } public void set(O o) { this.o…

Java 5 Features

Oracle (earlier Sun Microsystem) had introduced many new features in Java 5 Release. In this blog I will highlight the most popular and important feature which are widely used in the development. New Features: Generics Enhanced ‘for’ Loop Autoboxing/Unboxing Typesafe Enums Varargs Static Import Annotations Concurrency Utilities Collection Framework Scanner ProcessBuilder