Monday, 22 June 2015

RMI


Remote method invocation,


  • An overview of RMI
  • Writing rmi server
  • Writing rmi client
  • Creating and deploying the application.
Overview :

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.

Understanding stub and skeleton :

RMI uses stub and skeleton object for communication with the remote object.
remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects: 

Stub :

The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:

  1. It initiates a connection with remote Virtual Machine (JVM),
  2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
  3. It waits for the result
  4. It reads (unmarshals) the return value or exception, and
  5. It finally, returns the value to the caller.

Skeleton : 

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:

  1. It reads the parameter for the remote method
  2. It invokes the method on the actual remote object, and
  3. It writes and transmits (marshals) the result to the caller.
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.
stub and skeleton in RMI
What is distributed application,
  1. The application need to locate the remote method
  2. It need to provide the communication with the remote objects, and
  3. The application need to load the class definitions for the objects.
The RMI application have all these features, so it is called the distributed application.



Applets

Applets,

  • What are applets
  • Life Cycle methods of applets
  • Examples using Applets
  • Running applets with html file
  • Layout Managers

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application, including the following:
  • An applet is a Java class that extends the java.applet.Applet class.
  • A main() method is not invoked on an applet, and an applet class will not define main().
  • Applets are designed to be embedded within an HTML page.
  • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
  • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
  • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
  • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
  • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Life cycle 

Four methods in the Applet class give you the framework on which you build any serious applet:
  • init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
  • start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
  • stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
  • paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

Reflections in java



Reflections,

  • The reflection API
  • How to use reflections
  • Advantages of reflections
  • Drawbacks of reflections

Java Reflection is a process of examining or modifying the run time behavior of a class at run time.

The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.

The java.lang and java.lang.reflect packages provide classes for java reflection.

Where it is used,(Advantages)
The Reflection API is mainly used in:
  • IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.
The java.lang.Class class performs mainly two tasks:
  • provides methods to get the metadata of a class at run time.
  • provides methods to examine and change the run time behavior of a class.

Commonly used methods of Class class:

Method
Description
1) public String getName()
returns the class name
2) public static Class forName(String className)throws ClassNotFoundException
loads the class and returns the reference of Class class.
3) public Object newInstance()throws InstantiationException,IllegalAccessException
creates new instance.
4) public boolean isInterface()
checks if it is interface.
5) public boolean isArray()
checks if it is array.
6) public boolean isPrimitive()
checks if it is primitive.
7) public Class getSuperclass()
returns the superclass class reference.
8) public Field[] getDeclaredFields()throws SecurityException
returns the total number of fields of this class.
9) public Method[] getDeclaredMethods()throws SecurityException
returns the total number of methods of this class.
10) public Constructor[] getDeclaredConstructors()throws SecurityException
returns the total number of constructors of this class.
11) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException
returns the method class instance.

How to get the object of Class class?

There are 3 ways to get the instance of Class class. They are as follows:
  • forName() method of Class class
  • getClass() method of Object class
  • the .class syntax

1) forName() method of Class class

  • is used to load the class dynamically.
  • returns the instance of Class class.
  • It should be used if you know the fully qualified name of class.This cannot be used for primitive types.
Let's see the simple example of forName() method.
  1. class Simple{}  
  2.   
  3. class Test{  
  4.  public static void main(String args[]){  
  5.   Class c=Class.forName("Simple");  
  6.   System.out.println(c.getName());  
  7.  }  
  8. }  
Output:Simple

2) getClass() method of Object class

It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.
  1. class Simple{}  
  2.   
  3. class Test{  
  4.   void printName(Object obj){  
  5.   Class c=obj.getClass();    
  6.   System.out.println(c.getName());  
  7.   }  
  8.   public static void main(String args[]){  
  9.    Simple s=new Simple();  
  10.    
  11.    Test t=new Test();  
  12.    t.printName(s);  
  13.  }  
  14. }  
  15.    
Output:Simple

3) The .class syntax

If a type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type.It can be used for primitive data type also.
  1. class Test{  
  2.   public static void main(String args[]){  
  3.    Class c = boolean.class;   
  4.    System.out.println(c.getName());  
  5.   
  6.    Class c2 = Test.class;   
  7.    System.out.println(c2.getName());  
  8.  }  
  9. }  
Output:boolean
       Test

Determining the class object

Following methods of Class class is used to determine the class object:
1) public boolean isInterface(): determines if the specified Class object represents an interface type.
2) public boolean isArray(): determines if this Class object represents an array class.
3) public boolean isPrimitive(): determines if the specified Class object represents a primitive type.
Let's see the simple example of reflection api to determine the object type.
  1. class Simple{}  
  2. interface My{}  
  3.   
  4. class Test{  
  5.  public static void main(String args[]){  
  6.   try{  
  7.    Class c=Class.forName("Simple");  
  8.    System.out.println(c.isInterface());  
  9.      
  10.    Class c2=Class.forName("My");  
  11.    System.out.println(c2.isInterface());  
  12.     
  13.   }catch(Exception e){System.out.println(e);}  
  14.   
  15.  }  
  16. }  

Drawbacks,

  • You lose compile-time type safety - it's helpful to have the compiler verify that a method is available at compile time. If you are using reflection, you'll get an error at runtime which might affect end users if you don't test well enough. Even if you do catch the error, it will be be more difficult to debug.
  • It causes bugs when refactoring - if you are accessing a member based on its name (e.g. using a hard-coded string) then this won't get changed by most code refactoring tools and you'll instantly have a bug, which might be quite hard to track down.
  • Performance is slower - reflection at runtime is going to be slower than statically compiled method calls/variable lookups. If you're only doing reflection occasionally then it won't matter, but this can become a performance bottleneck in cases where you are making calls via reflection thousands or millions of times per second. I once got a 10x speedup in some Clojure code simply by eliminating all reflection, so yes, this is a real issue.

Input and Output streams

Input and Output streams

  • Overview of Streams
  • Bytes vs. Characters
  • Converting Byte Streams to Character Streams
  • File Object
  • Binary Input and Output
  • PrintWriter Class
  • Serialization
  • Reading and Writing Objects
  • Basic and Filtered Streams

JDBC


JDBC topics
  • The JDBC Connectivity Model
  • Types of Jdbc Drivers
  • Database Programming (with MSSQL and MYSQL)
  • Connecting to the Database
  • Creating a SQL Query
  • Getting the Results using ResultSet Interface
  • SQL Basic Queries
  • Statement and PreparedStatement
  • Commit and Autocommit, BatchUpdates
  • ResultSetMetaData and DataBaseMetaData
http://www.tutorialspoint.com/jdbc/index.htm

http://www.slideshare.net/vikasjagtap3/jdbc-ppt-44534221


Sunday, 21 June 2015

Java Inner Class



Java Inner class
  • Inner Classes
  • Member Classes
  • Local Classes
  • Anonymous Classes
  • Instance Initializers
  • Static Nested Classes


  • Java inner class or nested class is a class i.e. declared inside the class or interface.
  • We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
  • Additionally, it can access all the members of outer class including private data members and methods



Thursday, 18 June 2015

Test yourself


Test Yourself

How java is platform independent ?

Difference between JDK,JRE and JVM.

What is object ? Write an example

What is Class ? Write an example

What is Inheritance? Write an example

What is Polymorphism ? Write an program for method overloading and method overriding.

What is Abstraction ? Write an example for class.

What is Encapsulation? Write an example for class.

What you mean by access modifiers ?

What is package and how you define user defined packages?

What you mean by variable ? write down the different variables with examples ?

What you mean by data type list some primitive data types ?

What you mean by Array ?

What is single dimensional array ? write an program to access its elements

What is multi dimensional array ? write an program to access its elements

How you get an array length ?

What is the use of super keyword ?

What is the use of this keyword ?

What you mean by interface? write an program

What you mean by abstract class ? write an program

What you mean by exception / Exceptions handling ?

What you mean by checked exceptions ?

What you mean by unchecked exceptions ?

What is the use of try-catch block ?

How you create an multi-level catch ?

What you mean by user defined exceptions or custom exceptions?

What is the difference between throw and throws ?

What is use of finally block ?

What is the difference between final,finally and finalize block ?

What you mean by Damean thread ?

What you mean by threads ?

How do you create an thread ?

How do you set an priority to execute the threads in order ?

Name some interrupting threads methods ?

What you mean by inter thread communication ?

What you mean by synchronization ?

Name some lists,class in collection framework ?

What is an array list ? write an program for us ?

What is an linked list ? write an program for us ?

What is an Hash map ? write an program for us ?

What is an Linked hash map ? write an program for us ?

What is an Tree map ? write an program for us ?

What you mean by enhanced for loop ?

What is an Hash table? write an program for us ?


Programs

Write an class to store students information.

Write an class for inheritance

Write an program to store student information in array list

Write an program to store employee details in hash map and perform the below methods in your class.

  1. Store the employee details with integer keys
  2. Get the 2nd employee from the map collection
  3. Remove the 1st employee from the collection
  4. Get the size of the map
  5. Clear the map and check the size
Write an program for try catch block

Write an interface and its implementation in one class