Posts

Showing posts from February, 2015

Program to to Merge Two sorted Linked lists in Java

package programs; import ds.linkedlists.*; //Linkedlist and Node are my internal classes which I mentioned in LinkedList Program public class Merge2SortedLL { public static void main(String[] args) { LinkedList l1 = new LinkedList(); LinkedList l2 = new LinkedList(); l1.Enqueue(4); l1.Enqueue(5); l1.Enqueue(7); l1.Enqueue(11); l1.Enqueue(12); l2.Enqueue(3); l2.Enqueue(4); l2.Enqueue(7); l2.Enqueue(18); l2.Enqueue(20); l1.head = Merge(l1,l2); l1.display(); } private static LLNode Merge(LinkedList l1, LinkedList l2) { LLNode t1= l1.head; LLNode t2= l2.head; LLNode head = null; LLNode prev = null; while(t1!=null && t2 != null){ if(head == null){ if(t1.value >t2.value){ prev = t2; t2 = t2.next; } else { prev= t1; t1 = t1.next; } head= prev; } else{ if(t1.value >t2.value){ prev.next = t2; prev = t2; t2 = t2.next;

Access Modifiers

Access control is nothing but giving control/access over that particular class member. This can be in various ways namely 1) Public -when a method or a variable is declared as public,it means all other classes regardless of package they belong to, can use them. 2) Private -Members marked as private cannot be accessed anywhere except the class it is declared. 3) Protected -Members which are marked as protected can be accessed anywhere inside the package and also in the classes which inherit that particular class where it is declare. 4) default -Same as protected except it can only be accessed anywhere inside the package but not by the child packages outside the project.

OOPS Java !!

Object : Whenever JVM encounter new keyword,it creates a new instance of the class with its own state and behavior which is termed as an object. Object Oriented Programming is a methodology to create a program with classes and objects  In Java we do create objects and classes and deal with them,hence it is an object oriented programming language. Because of its primitives , Java is not 100% object oriented programming language. So..which are 100% OOP language? Smalltalk ,S imula and few other are in those list. OOPS Concepts: Abstraction - Hiding the internal functionality and details is known to be abstraction.In Java,it is achieved through abstract classes and interfaces. Inheritance -Acquiring all behavior and properties of the Parent class by the child class. Code reusability and runtime polymorphism is achieved thrigh inheritance. Polymorphism - In Java,polymorphism refers to the ability of a methods to take many forms. It is achieved through overloading and over

What happens when you run a Java Program?

Let suppose you run a Hello World program! HelloWorld.java public class HelloWorld(){     public static void main(String[] args){        System.out.println("Hello World!");     } }  Compile Time : Java Code(.java files) -------Compiler-------> Byte Code(.class files) Before JVM(Java Virtual Machine) runs the program,the program needed to be converted to Byte-Code, which is platform independent.Compiler compiles and generates .class files if the program is error free.These class files are saved in Workspace/classpath specified  Run Time : Byte Code(.class files)  -----Interpreter-----> Executes the instructions Now, JVM is invoked to run the byte-code.It searches for the .class file in the path/workspace and executes it. In detail, JVM >Classloader loads the compiled (.class files)  code, >ByteCode Verifier verifies the bytecode , >Then interpreter comes into picture where it takes over the control of bytecode and convert