Posts

Bitwise right shift operators in Java

C/C++ supports only one right shift operator ">>" which is a positive or unsigned integers In java we have two operator 1)Right shift Signed operator(>>)  In Java, the operator ‘>>’ is signed right shift operator. All integers are signed in Java, and it is fine to use >> for negative numbers. The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler. For example, if binary representation of number is  1 0….100, then right shifting it by 2 using >> will make it  11 …….1. See following Java programs as example ‘>>’ class Test {      public static void main(String args[])  {         int x = - 4 ;         System.out.println(x>> 1 );     ...

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...

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 ...

Static In Java!

Static in dictionary means stable,unchanging or inactive. In java static is generally used in case of variables,Methods,blocks and classes. It is not associated with any instance of the class that is why you can access the static member without creating any instance of the class. 1.Static variables:  When a class has static variables,those are likely to be called class variables.They don't need instances of the class to be created and then values to be initialized seperately.These can be directly accessed by className.variableName. When you have the common data for all the objects/instances. Then you might be allocating an extra memory for the same variable for every instance you create. Ex1: When a company Xyz has its employee data stored in a class.he knew that everyone are of the same company . Here it creates an extra allocation for all the instances. public class Employee(){ String Name; String Company; long Salary; Employee(String Name,long Sa...