Monday, February 22, 2010

Short Notes on Java Archive(JAR) file

JAR:

The Java Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications

Advantages:

  • Security, A jar file can be digitally signed enabling users to verify the signature and then grant the program security privileges.

  • Decreased download time, since the archive is compressed it takes less time to download than it would to download each individual file.

  • Package Versioning, A Jar file may contain vendor and version information about the files it contains.

  • Portability, all Java Runtime Environments know how to handle Jar files.

Example:

  1. Making javafile and compile that

    package amir;
    public class Hello
    {
    public static void main(String arg[])
    {
    System.out.println("Hello its form Hello.java inside amir package");
    }
    }

javac Hello.java

then amir directory have amir->Hello.java

->Hello.class

  1. Creating mainclass file:
    mainclass contains below line
    Main-Class: amir.Hello

  2. Making jar files:
    jar cmf mainclass myfirst.jar amir/Hello.class
    then u will get myfirst.jar

  3. Viewing jar files:
    jar tf myfirst.jar
    then it will show as below
    META-INF/
    META-INF/MANIFEST.MF
    amir/Hello.class

  4. Running jar file:
    java -jar myfirst.jar
    output:
    Hello its form Hello.java inside amir package

Monday, February 15, 2010

Java Basics-1

Class(Logical construct):
Template or Blueprint of an object.
or
Structure and Behaviour of an object.

Object(Physical reality):
Instance of a class .

Overloading:
Same method name and method signature to be vary.
Method signature -> number of arguements
-> order of arguements
-> diff data types
Overriding:
Method name and signature to be same.

abstract class:
It is a class that cannot instantiate. It can be extended or subclassed. It may or may not contain abstarct method. abstract method is a method to have only method definition not implementation(its ends with semi-colen without curly brackets). It is used to single inheritance.

interface:
It is a refernce type similar to a class. It contains common methods and constants to a group of classes.
All interface to be pure abstract. It is used to multiple inheritance.

final:
In front of variable - to be constant.
In front of method - cannot override.
In front of class - cannot inherit.

public -> can acess within class / same package / other package.
private->can acess within class.
protected-> can acces within class / same package / parent-child relationship.
default-> can acces within class / same package .

Thursday, February 11, 2010

Short notes on Thread

Thread:

A Thread is a path of execution of a program. A thread is a sequence of instructions that executed in a define unique flow of control.

SingleThread: A process is made up of one thread.

MultiThread: A process is made up of two or more thread.

Multitasking: It is a ability to do two or more tasks at a same time.


Create a Thread

-> extends Thread class
-> implements Runnable interface (its to be more useful because in big projects super class to be reserved one)

Process based:

It enables a computer to create more process concurrently. Each process having separate memory address location. So switches from one process to another , processor have face more overloads.

Thread based:

A program cotains more threads , it can do more tasks simultaneusly.

A processor switches from one thread to another with fewer overloads. It is also called as Light process.

Advantage:

  1. Improved performance

  2. Minimized system resource usage.

  3. Simultaneusly to use multiple application.

  4. Program structure simplification.

DisAdvantage:

Race condition: When two or more threads try to access the same variable atleast one thread should to write that variable before to access. Its arised because of lack of synchronization.

DeadLock: When two threads are waiting for each other for complete their operation before complete their individual actions.

Lock-starvation: While execution of thread postponed because of that low priority. Java runtime environment executes the thread based on priority. Because CPU executes one thread at a time. Thread priority value 1-10.

Life Cycle:

1.New: While thread initialized it will enter to init state. After we should call start() method, Otherwise it will cause IllegalThreadStateException.

2.Runnable: Once start method called then it will enter to runnable state. Start method allocate each thread resource and sheduldes it ,then it will call run() method.

3.NotRunnable: 3 ways

-> sleep() - wait for specified time.

-> wait() - wait until notify.

-> Blocked by another thread – Blocked thread while I/O opeartion.

4.Dead: if run method completed or thread object to be asign to null then its enter to this state.

Synchronization: Used to avoid racecondition or deadLock effects . Synchronization means serializing the threads. It allows to execute one thread at a time.