Saturday, 19 July 2014

THREAD

Actually Thread is an Operating System concept, this provide efficient performance through "Parallelism or Concurrency". Parallelism /Concurrency meaning doing many work at the same time without waiting for completion of one another. Thread give us multitasking.



Thread provide mechanism  to perform another work without waiting for completion to another work. 
Thread are also called "Light weight Process". Each Thread shall reside in the Process and no one Thread are outside the process. Every Thread have their own Stack memory, heap memory,register  and program counter .

In above there is symbol represent the Thread.
Thread provide synchronization : each thread shared data of other thread in same process.One thread can read, write or change another thread's data.

Types of Thread

There are two types of Thread
  1. User Level Threads -- User managed threads
  2. Kernel Level Threads -- Operating System managed threads acting on kernel, an operating system core.
But in this topic I haven't discuss because it's require 100 page to explain this operating system concept.




Life Cycle of a Thread (Thread States)

A thread have five state and it's have only one state at a time
A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are as follows:
  1. New
  2. Runnable
  3. Running
  4. Waiting (Blocked)
  5. Dead

1) New

When you create the instance of Thread Object.

2) Runnable

When you invoke the start() method thread object ,but scheduling algorithm have not been selected this thread.

3) Running

It's start it's working and get scheduled by scheduling algorithm of operating system 

4) Waiting

This is the state when the thread is still alive, but is currently not eligible to run.

5) Dead

A thread is in dead state when object of thread become out of scope and garbage collector sweep it from system outsources.


Implementation Thread in JAVA

You can implement Thread in Java be two ways
  1. extends Thread class
  2. implement runnable interface
Here are code example

public class ThreadA implements Runnable {
    public void run() {
     //Code
    }
}
 public class ThreadA extends Thread {
    public void run() {
     //Code
    }
}





you can start thread by just invoking start() method to make it into running state.
  
ThreadA myThread=new ThreadA();
myThread.start();


Sunday, 13 July 2014

Symbols Of JAVA

Owner
In 2010 ,Oracle buy Java and now Oracle are the owner of Java

Place
Java is the name of Island in Indonesia 
Inventor
James Gosling are the inventor of Java,
IDE
Integrated Development Environment are use for the Software development.Eclipse are the top most usable and Free source software.
   
Thing
Java are the coffee bean and so many coffee restaurant names on Java
Extension
JAR stand for Java Archive.It is the package of collection of all file that are used to open application by just single click

 J2ME
Android is the Operating system use for mobile phone and tablet. Java Programming Language are totally use to build android application.

Framework
Here are framework used In J2EE Technologies



Friday, 11 July 2014

Java Edition

Using Java; you can build many application regarding different platform, architecture and operating system

So Java is categorized into three edition 
  1. Java SE(Standard Edition)
  2. Java EE(Enterprise Edition)
  3. Java ME(Micro Edition)



What is Java SE (Standard Edition) ?
J2SE is the first step if you want to be become Java Programmer.It is the foundation or base of both Java ME and Java EE because Java EE and Java ME use the core library of Java SE for their development like java.* and javax.* packages uses in J2EE and J2ME. Java SE is a widely used platform for development and deployment for desktop and server environments. Java SE uses the Object Oriented  programming language.Using Java SE you can made desktop application for standalone computer or for LAN as well as Applet have also part of Java Standard Edition.
Java SE have the following areas 
  • Filing Reader or Writer
  • SWING or AWT
  • Classes and Object
  • JDBC
  • Socket Programming
  • Applet
  • Thread
  • Graphics


What is Java EE (Enterprise Edition) ?
J2EE is the Java architecture for developing multiple tier enterprise applications.In other words, if your application are  very large scale, distributed system, then you should consider using JavaEE. Built on top of JavaSE. If you want o build application for WEB then J2EE technologies will be used.  J2EE provide you following and it's part of J2EE
  • For database access (JDBC, JPA,Hibernate Framework)
  • JSP(Front-end Engineering)
  • Servlet(Business Intelligence) 
  • Java Server faces (JSF) Front End Framework
  • Remote method invocation (RMI), 
  • Messaging (JMS)
  • Web services like AJAX (Asynchronous JavaScript and XML) 
  • JNDI(Java Naming and Directory Inteface) 
  • EJB (Enterprise Java Beans) .


What is J2 ME (Micro Edition) ?
J2 ME are being use for small and tiny which limited resources like PDA, Cellular phone ,TV or in embedded devices like microwave oven, security alarm etc.

Here are the target device for J2ME
  • Limited processing power
  • Limited system memory
  • Limited storage capacity
  • Small display
  • Less Battery power
  • Limited connectivity to internet
J2ME was designed with all these above considerations.
Now todays Android and blackberry  OS(Operating system) are the best example of J2ME. It totally implemented using Java.Google, Oracle and some other behind JAVA then it's mean Java are the world top most language.


Here below picture give conclusion of all my discussion.
Hope you got the idea of whole discussion but still if you have ambiguity, miss-clarification or some other thing then comment below I shall response to you. 

Wednesday, 9 July 2014

What is Multilevel Inheritance

First take a look on this picture because I believe 'Picture contain Thousand words" ; so look at this picture


In above the Class A is super class and class B is child class of class A and Class C is child of Class B, but class C also absorb the capability of Class B as well as class class A.

Here is the example of  Java code

     public class A{

     }
     public class B extends A{

     }
     public class C extends B{

     }


There are much more difference between multilevel and multi inheritance, lets take another look 
 


public class Employee{
}


public class OfficeStaff extends Employee{
}


public class Cashier extends OfficeStaff{
}




Tuesday, 8 July 2014

Garbage Collector in Java

Garbage Collector is a part of JVM (Java Virtual Machine). Garbage collector(GC) only deal with heap memory.GC is a program which runs on the JVM which gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.


Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp.

Garbage Collector done it's job without any request and  notification.It continuously reviewing the heap memory that any objects have not been referenced to variable(stack) then it automatically clean the memory from these object or simple words are It frees memory allocated to objects that are not being used by the program any more.
In Java the GC runs automatically, but you can also call it explicitly with System.gc() and try to force a major garbage collection but in a programming language like C, allocating and deallocating memory is a manual process so Java take this headache from programmer and put it into JVM.





  public class GarbageCollector{
   public static void main(String[] args) {
        Object obj1=new Object(); //Create a new object on Heap
        obj1=new Object();//Create another object and hold reference to last one variable
        System.gc();
  }
}//ENd of Class

Now in the above code the first time obj1 hold referenced first created object and then
 it again hold another object reference; but where is the last object referenced that is
 unreachable; now it's time of Garbage collector and it automatically deallocate the 
object memory from  the heap and make available free for the future and save program
from starvation problem. 
We can garbage collector in Java by using this statement
   System.gc();

Note: If anyone don't know about the word "HEAP" or "Stack" then first 
I recommend to you read this topic
http://theprogrammingsintroduction.blogspot.com/2014/07/what-is-stack-and-heap-in-java.html

What is Exception handling in Java

Exception Handling is also known as Defensive Programming.
Exception is an indication of a problem that occurs during a program’s execution.
Exception handling enables you to create applications that can resolve (or handle) exceptions. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered. The features  help you write robust and fault-tolerant programs that can deal with problems and continue executing or terminate gracefully. 
Java exception handling is based in part on the work of Andrew Koenig and Bjarne Stroustrup.

When to Use Exception Handling?
Exception handling is designed to process synchronous errors, which occur when a statement
executes. Common examples we’ll see  are out-of-range array indices, arithmetic overflow (i.e., a value outside the representable range of values), division by zero, invalid method parameters, thread interruption
and unsuccessful memory allocation (due to lack of memory). 
Exception handling is not designed to process problems associated with asynchronous events (e.g., disk I/O completions,network message arrivals, mouse clicks and keystrokes), which occur in parallel with,
and independent of, the program’s flow of control.

Hierarchy of Exception
An small portion of the inheritance hierarchy for class Throwable (a subclass of Object), which is the superclass of class Exception. Only Throwable objects can be used with the exception-handling mechanism. Class Throwable has two subclasses: Exception and Error. 
Class Exception and its subclasses—for instance, RuntimeException (package java.lang) and IOException (package java.io)—represent exceptional situations that can occur in a Java program and that can be caught by the application. Class Error and its subclasses represent abnormal situations that happen in the JVM. 

Types of Exception

  1. Checked Exception
  2. UnChecked Exception

Java compiler enforces a code written with in try/ catch block or throw an Exception object for checked exceptions.All exception types that are direct or indirect sub classes of class RuntimeException (package java.lang) are unchecked exceptions like ArrayIndexOutOfBoundsExceptions and ArithmeticExceptions. 
And other Exception known as checked Exception that are not subclass of RunTimeException class.
You must write Java code for checked exception within try catch block or use throw clause.

Here is an Example through Java Code

  public class ExceptionExample{
         public static void main(String []args){
               try{
                    // Open File and read data
               }
               catch(Exception e){
                         //TO do for to handle Exception
               }
         }
  }


OR throws example are

 public class ExceptionExample{
         public static void main(String []args) throws Exception{
              
                    // Open File and read data
              
              
         }
  }

In above throws example we don't need to write code with in try/catch block.

Hope you got the idea about the Exception handling.I know there are many other concept and techniques related Exception Handling not mentioned in here but reason for that I put consideration in my mind for all audience newbie, Beginner ,Intermediate or Expert.

What is Stack and Heap in Java


Java Memory is divided into two types

  1. Stack 
  2. Heap
Both are in RAM(Random access Machine).

All objects are stored on the heap (including their attributes).
Local variables (including arguments) always contain primitive values or references and are stored on the stack.
Here is another picture below

String s = "Hello";
In this "s" are variable that hold the address/reference of "Hello" String 
Object.
Variable is on Stack and "Hello" reference is on Heap.Java only stores primitives on
 the stack and Objects are created on the heap, and only references
(which in turn are primitives) are passed around on the stack.
Let take a look on Java Code

  public class Employee{
    public static void main(String []args){
          Employee e=new Employee();
    }
  }

In above the "e" is a variable that hold the reference/address of Employee
object and "new Employee()" actually this give the reference of newly created 
object.
Variables created on the stack will go out of scope and automatically deallocate and
whether  Variables on the heap must be destroyed manually and never fall out of scope(Garbage collector/finalize() ).
In a stack of items, items sit one on top of the other in the order they were placed 
there and you can only remove the top one (without toppling the whole thing over). 
so stack follow LIFO(Last In first Out) but  In a heap, there is no particular order to the 
way items are placed.
You can reach in and remove items in any order because there is no clear 'top' item.