Posts

Showing posts from August, 2016

SOAP - REST

Image

Final Finally Finalize

class FinalExample{ public static void main(String[] args){ final int x=100; x=200;//Compile Time Error }} class FinallyExample{ public static void main(String[] args){ try{ int x=300; }catch(Exception e){System.out.println(e);} finally{System.out.println("finally block is executed");} }} class FinalizeExample{ public void finalize(){System.out.println("finalize called");} public static void main(String[] args){ FinalizeExample f1=new FinalizeExample(); FinalizeExample f2=new FinalizeExample(); f1=null; f2=null; System.gc(); }} Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed. Finally is used to place important code, it will be executed whether exception is handled or not. Finalize is used to perform clean up processing just before object is garbage collected. Final is a keyword. Finally is a block. Finalize is a method.

Static Block

How to count number of Instance of a class

public class Bicycle { ... public static int INSTANCES = 0; ...   //constructor     public Bicycle(int gear, int speed, int seatHeight, String color) {         gear = 0;         speed = 0;         seatHeight = 0;         color ="Unknown";         Bicycle.INSTANCES++;     }     protected void finalize() {         INSTANCES--;     } } a. System.gc(); b. Runtime.getRuntime.gc();

Object Equality

Every Java object inherits a set of base methods from java.lang.Object Creational methods Object()   - Default no-argument constructor clone()     - Returns a new instance of the class Synchronizing methods notify() - Sends a signal to a waiting thread (on the current instance) notifyAll() - Sends a signal to all waiting threads (on the current instance) wait() - Forces the current thread to wait for a signal (on the current instance) Equality methods equals(Object) - Returns true if this instance is equal to the argument hashCode() - Returns a hash code based on the instance data Other methods toString() -Returns a string representation of the object finalize() - Performs garbage-collection duties getClass() - Returns the Class object associated with the instance public class Test{ public static void main(String args[]){ System.out.println(new N()==(new N())); //false System.out.println(new N().equals(new N())); //false } } class N{} public class Point {   private s...

Javap java.lang.Objecct

Compiled from "Object.java" public class java.lang.Object {   public java.lang.Object();   public final native java.lang.Class getClass();   public native int hashCode();   public boolean equals(java.lang.Object);   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;   public java.lang.String toString();   public final native void notify();   public final native void notifyAll();   public final native void wait(long) throws java.lang.InterruptedException;   public final void wait(long, int) throws java.lang.InterruptedException;   public final void wait() throws java.lang.InterruptedException;   protected void finalize() throws java.lang.Throwable;   static {}; } Compiled from "Collection.java" public interface java.util.Collection extends java.lang.Iterable {   public abstract int size();   public abstract boolean isEmpty();   public abstract boolean contains(java.lang.O...

Hashcode and Frequency API

import java.lang.Math; import java.util.*; public class HelloWorld {   public static void main(String[] args)   {    Set set = new HashSet ();     Num n = new Num(2);       Num n1 = new Num(2);   /*  set.add(n);     set.add(n);     System.out.println(set.size()); //1*/         set.add(n);     set.add(n1);     System.out.println(set.size()); //2       System.out.println(n.hashCode()); //1829164700   System.out.println(n1.hashCode()); //2018699554       System.out.println(Collections.frequency(set,n)); //1       // create array list object        List arrlist = new ArrayList ();        // populate the list    arrlist.add("A");    arrlist.add("B");    arrlist.add("C");    arrlist.add("C");    arrlist.add("C");        ...

Java Best Tutorial

http://www.java2novice.com/ Set uniqueSet = new HashSet (list); System.out.println("\nSorted Map"); Map treeMap = new TreeMap (map);   public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); }   }

Collection API

Collection -Interface List,Map,Map.Entry,Compartor,Comparable -Interface Collections,Arrays- class

Call By Referance in JAVA

 public static void main ( String[] args ) {       Number a = new Number();       a.x=3;       System.out.println("Value of a.x before calling increment() is "+a.x);       increment(a);       System.out.println("Value of a.x after calling increment() is "+a.x);    }    public static void increment(Number n) {       System.out.println("Value of n.x before incrementing x is "+n.x);       n.x=n.x+1;       System.out.println("Value of n.x after incrementing x is "+n.x);    } Value of a.x before calling increment() is 3 Value of n.x before incrementing x is 3 Value of n.x after incrementing x is 4 Value of a.x after calling increment() is 4

Sort a list by multiple attibute - chained comparator

sorting a list of employees by their job title, then by age, and then by salary chained comparator -  CompareToBuilder class of the Apache Commons Lang library. public class Employee {     private String name;     private String jobTitle;     private int age;     private int salary;     public Employee(String name, String jobTitle, int age, int salary) {         this.name = name;         this.jobTitle = jobTitle;         this.age = age;         this.salary = salary;     }       // getters and setters       public String toString() {         return String.format("%s\t%s\t%d\t%d", name, jobTitle, age, salary);     } } import java.util.Arrays; import java.util.Comparator; import java.util.List; /**  * This is a chained comparator that is used to sort a list by multiple  * attrib...

Difference between abstraction and encapsulation

Image