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.

Thursday, December 24, 2009

Short Notes on OOPS in java

Short Notes on OOPS:

Abstraction:

It denotes the essential characterstics of an object which differentiate from other object and thus provide crisply defined conceptual boundaries relative to the perspective viewer.

Encapsulation:

It is the mechanism that binds together the code and the data it manipulates and keeps both safe from outside misuse and interference.

Inheritance:

One object acquired the properties of another Object.

Polymorphism:

It is an ability of an object to take on many forms.

Ex: Compile time polymorphism – method over loading

Run time polymorphism – method overriding


Wednesday, December 23, 2009

Interview questions(like diff bettween two) in java


S.No

Abstract

Interface

1

Single inheritance

Multiple inheritance

2

Non abstarct mtd also

Only abstarct mtds

3

Non-static and non-final variables also.

Variables must be static and final(implicitly)

4

Non public members also

Only public mbrs

5

Using extends keyword

Using implements keyword

6

It can invoke if main exists

Pure abtract

7

Faster

Flexible



S.No

Constructor

Method

1

Member function of class.

Ordinary member function.

2

Same as class name.

Own name

3

Invoke using new operator

Invoke using dot operator.

4

No-return type.

It have.



S.No

ArrayList

Vector

1

Its a growable array.

Synchronized ArrayList.

2

Not thread safe

Thread safe.




S.No

HashMap

HashTable

1

Un ordered and un sorted map.

Not thread safe.

Synchronized HashMap. Thread safe.

2

It allows one null key and multiple null values

It wont allow no null key/values.



S.No

AWT

SWING

1

Heay Weight Component.

Light Weight Component.

2

Component use native methods.

Use the methods that r written in java.

3

It have their own viewport which sends the output to the screen.

It does not write itself to the screen, but redirect it to the component it builds

4

Platform dependent.

Platform independent.

5

Old

Its advanced AWT.

1.Its provide additional components like

Jtable,JTabbedPane

2. can add icons and tooltips.

3. built on double buffering.

6

Support Event Delegation Model

MVC architecture,

7

Static Look and feel.

Dynamic Look and feel.


S.No

InvokeAndWait

InvokeLater

1

Synchronous.

Asynchronous.

2

It blocks until runnable task is complete.

It posts an actionevent to the event queue and returns immediately.



S.No

Font

FontMetrics

1

It is used to render text data onto the screen

It provides access to attributes of Font Objects, thing such as charwidth, charheight ,ascents and descent of a font Object.




S.No

String

StringBuffer

1

Immutable(cant change content )

Mutable(can change content using append mtd)

2

Not thread safe.

Thread safe.



S.No

Jlist

JcomboBox

1

Its dont have Editor Component.

It have Editor Component.

2

It allows single,single interval and multiple interval selection.

It allow only single selection.



S.No

Encapsulation

Abstraction

1

It is the mechanism that binds together the code and the data it manipulates and keeps both safe from outside information and misuse.

It denotes the essential characterstics of an object which differentiate from other object.

2

Hiding irrevelents details of an object.

Showing essential things of an object.


Collections in java

Collection:

It is an object that groups multiple elements on a single unit. It is used to store,

retrive, manipulate and communicate the data.

List :

It care about index. ordered based on the index . we can specify the index of elements or it stored default by end .

ArrayList:

Its a growable array. Fast Iteration and Random Access.

Vector:

Synchronized ArrayList . Random access.

Linked List:

Like Array list ordered by index position and also doubly linked.

Fast Insertion and Deletion.


SET:

Its care about uniqueness-its does not allow duplicate.

Hashset:

un ordered and un sorted . Efficient implementation of hasCode() . While iterate we can't predict this order.

LinkedHashSet:

ordered Hashset. Doubly linked. While iterate we can get which order we r inserted.

TreeSet:

Sorted set . sorted by natutral order.

Map:

Its care about unique identifiers. U map a unique id to a specific value, where key and value both r objects.

HashMap:

Un ordered and un sorted. One null key and multiple null values allowed.

Hashtable:

Synchronized HashMap. No null key/values allowed.

LinkedHashMap:

Ordered hashmap by insertion.doubly linked.

TreeMap:

Sorted Map.


Thursday, December 10, 2009

JDBC

What is JDBC?

Java Database Connectivity is a technique of connect java front end to back end database and allowing the retrieval and manipulation of data in the database using java.

---------------------------------------------------------------------------------------------
How do you connect to the Database?

The process of using JDBC to connect to the database is as follows:

1. Register the driver:

Class.forName(”driverName”);
for example, sun.jdbc.odbc.JdbcOdbcDriver


2. Making the connection using DriverManager Class’s
getConnection method :

Connection con = DriverManager.getConnection(”url,”myLogin”,myPassword”);
For example url may be jdbc:odbc:dsn_name.

3. Creating the JDBC Statement and Retreiving:

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery(query);

4. Handling SQL Exception

---------------------------------------------------------------------------------------------

What are the drivers and when it will use?

JDBC-ODBC Bridge Driver for Java and Database (Java Application)

JDBC-Net pure java driver for Applet and Database(Applet application)


Native-API partly Java driver for Native class and Database
(for network application)

Native Protocol pure java driver for vendor specfic application

(EJB application)