Introduction to JAVA1

download Introduction to JAVA1

of 21

Transcript of Introduction to JAVA1

  • 8/7/2019 Introduction to JAVA1

    1/21

    JAVA MIND BENDER JAVA

    48

    A.B.COLLEGE PATHANKOT

    Introduction to JAVA

    Introduction:

    Java is a general purpose, object-oriented programming language

    introduced by Sun Microsystems of USA in 1991.Originally called Oak

    by James Gosling, one of the inventors of the language. The java team

    which included Patrick Naughton discovered that the existing languages

    like c and c++ had limitations in terms of both reliability and portability.

    They modelled their new language java on C and C++ but removed a

    number of features of C and C++ that were considered as sources of

    problems and thus made Java is really simple, reliable, portable, simple,

    compact, interactive and powerful language. The most striking feature of

    the language is that it is a platform neutral Language. Java is the first

    language that is not tied to any particular hardware or operating system.

    Programs developed in java can be executed anywhere on any system.

    JAVA Features:

    1.Compiled and Interpreted: - Java combines both theseapproaches thus making java a two stage systems. First, java compiler

    translates source code into what is known as bytecode instructions.

    Second, java interpreter generates machine code that can be directly

    executed by the machine that is running the java programs.

  • 8/7/2019 Introduction to JAVA1

    2/21

    JAVA MIND BENDER JAVA

    49

    A.B.COLLEGE PATHANKOT

    2.Platform-independent and portable: - The most significantcontribution of java over other language is its portability. Java

    programs can be easily moved from one computer system to another,

    anywhere and anytime.

    3.Object-Oriented: - Java is a true object-oriented language. Almosteverything in java is an object. All program code and data reside

    within objects and classes. The object model in java is simple and

    easy to extend.

    4.Robust and secure: -Java is a robust language. It provides manysafeguards to ensure reliable code. It has strict compile time and run

    time checking for data types. Security becomes an important issue for

    a language that is used for programming on internet.

    5.Distributed: - Java is designed as a distributed language. It has theability to share both data and programs. This enables multiple

    programmers at multiple remote locations to collaborate and work

    together on a single project.

    6.Multithreaded: - Multithreaded means handling multiple taskssimultaneously. Java supports multithreaded programs. This means

    we need not wait for the application to finish one task before

    beginning another.

    7.Dynamic and Extensible: - Java is a dynamic language. Java iscapable of dynamically linking in new class libraries, methods and

  • 8/7/2019 Introduction to JAVA1

    3/21

    JAVA MIND BENDER JAVA

    50

    A.B.COLLEGE PATHANKOT

    objects. Java programs support functions in other languages such as C

    and C++. These are native methods.

    Object-Oriented Programming (OOP):

    Object-Oriented Programming (OOP) is an approach to perform

    organization and development, which attempts to eliminate some of

    the pitfalls of conventional programming methods by incorporating

    the best of structured programming feature with several new

    concepts. Object-Oriented Programming is an approach that

    provides a way of modularizing programs by creating partitioned

    memory area for both data and function that can be used as

    templates for creating copies of such modules on demand. OOP

    treats data as a critical element in the program development and does

    not allow it to flow freely around the system.

  • 8/7/2019 Introduction to JAVA1

    4/21

    JAVA MIND BENDER JAVA

    51

    A.B.COLLEGE PATHANKOT

    Basic Concepts ofOOP:

    y Objects and Classes: - Objects are the basics runtimeentities in an object-oriented system. They may represent a

    person, a place, a bank account, a table of data or any item that

    the program may handle. They may also represent user-defined

    data types such as vectors and lists. Any programming problem is

    analyzed in terms of objects and the nature of communication

    between them.

    A class may be thought of as a data types and an object as a variable

    of that data type. Once a class has been defined, we can create any

    number of objects belonging to that class. A class is thus a collection of

    objects of similar type.

    yEncapsulation: - The wrapping up of data and methods

    into a single unit (called class) is known as encapsulation. The

    data is not accessible to the outside world and only those

    methods, which are wrapped in the class, can access it.

    Encapsulation makes it possible for objects to be treated like

    black boxes, each performing a specific task.

    y Inheritance: -Inheritance is the process by which objects ofone class acquire the properties of objects of another class. The

    concepts of inheritance provide the idea of reusability. This

  • 8/7/2019 Introduction to JAVA1

    5/21

    JAVA MIND BENDER JAVA

    52

    A.B.COLLEGE PATHANKOT

    means that we can add additional features to an existing class

    without modifying it.

    y Polymorphism:- Polymorphism means the ability to takemore than one form. It plays an important role in allowing objects

    having different internal structures to share the same external

    interface. It is extensively used in implementing inheritance.

    The JAVA Abstract Window Toolkit (AWT)Package:

    Java applets are run in a web browser this makes them different from a

    standalone java program. Both a Web Browser and JDKs Applet

    Viewer can be used to run a java Applet. To display data within a GUI,

    an applet must use java classes specifically designed for displaying

    information in a GUI environment. The java GUI classes are containedinjava.awt package.

    Javas AWT package provides the following: -

    y A full set of user-interface (UI) widgets and other components,including windows, menus, buttons etc.

    y Support for UI containers, which can contain other embeddedcontainers or UI widgets.

    y An event system for managing system and user events amongparts of the AWT.

  • 8/7/2019 Introduction to JAVA1

    6/21

    JAVA MIND BENDER JAVA

    53

    A.B.COLLEGE PATHANKOT

    The Major Components in the AWT: -

    yContainers: - Containers are generic AWT componentsthat can contain other containers. The most common form of

    container is a panel, which represent a container that can be

    displayed on screen. Applets are a form of panel.

    y Canvases: - A canvas is a simple drawing surface.Canvases are good for painting images or performing other

    graphics operations.

    y UI components:- These components can include buttons,lists, check boxes, text fields etc.

    y Window construction components: - Thesecomponents include window, frames and menu bars.

    The partial AWT hierarchy is shown in the diagrams:

  • 8/7/2019 Introduction to JAVA1

    7/21

    JAVA MIND BENDER JAVA

    54

    A.B.COLLEGE PATHANKOT

    Components

    Canvas Text components ButtonContainers

    Panel Window

    TextField

    Applet

    Frame Dialog

  • 8/7/2019 Introduction to JAVA1

    8/21

    JAVA MIND BENDER JAVA

    55

    A.B.COLLEGE PATHANKOT

    BASICUSERINTERFACE (UI)COMPONENTS:-

    y Label: -

    The simplest form of UI component is the label, which is

    effectively, a text string that can be used to label other UI

    Components. In general a label can be defined as an uneditable

    text String that acts as a description for other AWT components.

    Syntax:

    Label l1=new Label (Label Name);

    add (l1);

    y Buttons: -Buttons are simple UI components that trigger some action in the

    interface when they are pressed.

    Syntax:

    Button b1=new Button (Button Name);

    add (b1);

  • 8/7/2019 Introduction to JAVA1

    9/21

    JAVA MIND BENDER JAVA

    56

    A.B.COLLEGE PATHANKOT

    y CheckBoxes: -Check boxes are user-interface components that have two States:

    on and off. Unlike buttons, check boxes usually dont trigger direct

    actions in a UI, but instead are used to indicate optional features of

    some other action. Check boxes can be used in two ways:

    y Nonexclusive- Given a series of check boxes, any of them can beselected.

    y Exclusive- Given a series of check boxes, only one check boxfrom the series can be selected at a time.

    Syntax:

    Checkbox chk=new Checkbox (Shoes);

    add (chk);

    yRadio

    Button

    :

    -A Radio button is hollow round. Radio Buttons only one in a series

    can be selected at a time.

    Syntax:

    CheckboxGroup chkgrp=new CheckboxGroup ();

    Checkbox chkbx=new Checkbox (item name, chkgrp, false);

    add (chkbx);

  • 8/7/2019 Introduction to JAVA1

    10/21

    JAVA MIND BENDER JAVA

    57

    A.B.COLLEGE PATHANKOT

    y Choice Menus or Choice Lists:-Choice menus are pop-up list of items from which one can select

    an item. Choice menus can have only one item selected at a time.

    Syntax:

    Choice Ch=new Choice ();

    Ch.add (apples);

    Ch.add (orange);

    add (Ch);

    y Text Fields: -Text fields provide an area where one can enter and edit a singleline of text. Text fields are generally used for getting text input

    from a user.

    Syntax:

    TextField txt=new TextField (10);

    add (txt);

    y Text Areas: -Text Areas are editable text fields that can handle more than one

    line of text input. Text Areas are created from the TextArea class.

  • 8/7/2019 Introduction to JAVA1

    11/21

    JAVA MIND BENDER JAVA

    58

    A.B.COLLEGE PATHANKOT

    Text areas have horizontal and vertical scrollbars that enable the

    user to scroll through the text contained in the component.

    Syntax:

    TextArea taDetails=new TextArea (letter, 10, 45);

    add (taDetails);

    y Scrolling Lists: -Scrolling lists are similar to Choice menus or Choice lists with two

    significant differences:

    y A scrollbar list can be set up so that more than one item can beselected from the list at a time.

    yScrolling lists do not pop-up when selected. Instead, multipleitems are displayed in a manner similar to a textArea. If the list

    contains more items they can be displayed, the scrollbar is used to

    move through the entire list.

    Syntax:

    List lt=new List (5, true);

    lt. add (Raj);

    lt. add (Jyoti);

    lt. add (Aruna);

    lt. add (Sonia);

  • 8/7/2019 Introduction to JAVA1

    12/21

    JAVA MIND BENDER JAVA

    59

    A.B.COLLEGE PATHANKOT

    lt. add (Kamal );

    lt. add (Preet);

    add (lt);

    y Scrollbars: -Scrollbars are components that enable a value to be selected by

    sliding a box between two arrows. Several components have built-

    in scrollbar functionality, including text areas and scrolling lists.

    The Scrollbar class is used for creating scrollbars. A scrollbar can

    be horizontal or vertical.

    Syntax:

    Scrollbar bar = new Scrollbar (Scrollbar. VERTICAL, 10, 0, 1, 100);

    add (bar);

  • 8/7/2019 Introduction to JAVA1

    13/21

    JAVA MIND BENDER JAVA

    60

    A.B.COLLEGE PATHANKOT

    LAYOUTS:

    A layout refers to arranging and placing of the components in a

    container, much like arranging the furniture in a house java has an

    interface called the Layout Manager which belongs to the java.awt

    package. This is used for placing objects in a container for display on

    screen without specifying the row, column position. The AWT package

    includes classes of the following layout manager:

    y Flow Layout:-The flow layout class lays out components in amanner similar to the way words are laid out in a page, from left

    to right until theres no more room, then onto the next row.

    Syntax:

    FlowLayout flow=new FlowLayout (FlowLayout.LEFT);

    setLayout (flow);

    y Grid Layout: -The grid layout manager arranges componentsinto a grid of rows and columns.

    Syntax:

    GridLayout grid=new GridLayout (10, 3);

  • 8/7/2019 Introduction to JAVA1

    14/21

    JAVA MIND BENDER JAVA

    61

    A.B.COLLEGE PATHANKOT

    setLayout (grid);

    y Border Layout:- Border layouts, which created by using theBorderLayout class, divide a container into five sections: north,

    south, east, west, center.

    Syntax:

    BorderLayout border=new BorderLayout (10, 3);

    setLayout (border);

    add (North,btnNorth);

    add (South,btnSouth);

    add (East,btnEast);

    add (West,btnWest);

    add (Center,btnCenter);

    y Card Layout: -A card layout is a group of containers orcomponents that are displayed one at a time. Each container in a

    group is called a card.

    Syntax:

    CardLayout card=new cardLayout (10, 3);

    setLayout (card);

  • 8/7/2019 Introduction to JAVA1

    15/21

    JAVA MIND BENDER JAVA

    62

    A.B.COLLEGE PATHANKOT

    Event-Driven Programming:-

    A window based application, or an applet, is quite different. The

    operation of the program is driven by what is done with the GUI.

    Selecting menu items of buttons using the mouse, or through the

    keyboard, causes particular action within the program. The action using

    the GUI for a window based program or an applet clicking a menu item

    or a button, moving the mouse and so on are first identified by the

    operating system. The signals that a program receives from the operating

    system as a result of the actions are called events. A window based

    program is called an event driven program, because the sequences of

    events are created as a result of the interaction with the GUI drives and

    determines what happens in the program.

    Delegating the Event:-

    Suppose the user clicks a button in the GUI for a program. The button is

    the source of this event. The event generated as a result of the mouse

    click is associated with the Button object in the program that represents

    the button on the screen. An event always has a source object-in this

    case the button object. When the button is clicked, it will create a new

    object that represents and identifies this event in this case an object of

    type Action Event. This object will contain information about the event

    and its source. Any event that is passed to a java program will be

    represented by a particular event object and this object will be passed as

  • 8/7/2019 Introduction to JAVA1

    16/21

    JAVA MIND BENDER JAVA

    63

    A.B.COLLEGE PATHANKOT

    an argument to the method that is to handle the event. In this case it is

    actionPerformed ().

    Javas Event Types:

    11 concrete event types, which are contained in the java.awt.event

    package. All the event classes extend from two abstract superclasses:

    java.util.EventObject and java.awt. There are AWTEvent. The event

    inheritance hierarchy is as follow:

    java.util.EventObject

    java.awt.AwtEvent

    Adjustment Component Item Event Text EventAction Event

    Container Event Focus Event Input Event Paint Event Window Event

    Mouse Event Key Event

  • 8/7/2019 Introduction to JAVA1

    17/21

    JAVA MIND BENDER JAVA

    64

    A.B.COLLEGE PATHANKOT

    JDBC:

    JDBC has been part of the Java Standard Edition since the release of

    JDK 1.1. The JDBC classes are contained in the Java package java.sql.

    Starting with version 3.0. JDBC allows multiple implementations to

    exist and be used by the same application. The API provides a

    mechanism for loading the correct Java packages and registering them

    with the JDBC Driver Manager. The Driver Manager is used as a

    connection factory for creating JDBC connections.JDBC connections

    support creating and executing statements. These statements may be

    update statements such as SQL CREATE, INSERT, UPDATE and

    DELETE or they may be query statements using the SELECT statement.

    Additionally, stored procedures may be invoked through a statement.

    Statements are one of the following types:

    y Statement The statement is sent to the database server eachand every time.

  • 8/7/2019 Introduction to JAVA1

    18/21

    JAVA MIND BENDER JAVA

    65

    A.B.COLLEGE PATHANKOT

    y PreparedStatement The statement is cached and then theexecution path is pre determined on the database server

    allowing it to be executed multiple times in an efficient manner.

    Update statements such as INSERT, UPDATE and DELETE indicates

    how many rows were affected in the database. These statements do not

    return any other information.Query statements return a JDBC row result

    set. The row result set is used to walk over the result set. Individual

    columns in a row are retrieved either by name or by column number.

    There may be any number of rows in the result set. The row result set

    has metadata that describes the names of the columns and their types.

    The ConnectivityModel Being Used: -

    The java application is the UI. This in turn communicates with a driver

    manager using underlying java code. The Driver Manager is a class that

    Java

    ApplicationThe User

    Interface

    32-bit

    ODBCAccess

    Driver

    The AccessDatabase And

    Its Table

    Javas Driver Manager, Responsible for

    setting

    Up the JDBC ODBC bridge

  • 8/7/2019 Introduction to JAVA1

    19/21

    JAVA MIND BENDER JAVA

    66

    A.B.COLLEGE PATHANKOT

    belongs to the java.sql package. The Driver manager in turn

    communicates with the 32-bit ODBC driver. The 32-bit ODBC driver in

    turn communicates with the Access database and its tables.

  • 8/7/2019 Introduction to JAVA1

    20/21

    JAVA MIND BENDER JAVA

    67

    A.B.COLLEGE PATHANKOT

    Using javas Driver Manager to connect to a Database:

    Choosing an appropriate driver:-

    The method Class.forName(String) is used to load the JDBC driver

    class. The line below causes the JDBC driver fromsome jdbc vendorto

    be loaded into the application.

    Class.forName( "sun.jdbc.odbc.jdbcOdbcDriver" );

    The java DriverManager The ODBCSystem DSN The AccessDatabase, table

    With Data in it

    Creates a ConnectionObject whichCommunicates with the ODBC driver and in turn spawns a

    Result Set object in hold the record set returned by the querymade to the Database table.

    The Result Set ObjectThis holds the records retrieved from the Access Database, table.

    Data from this result set object can be bound to controls on a java

    form, i.e. the UI

  • 8/7/2019 Introduction to JAVA1

    21/21

    JAVA MIND BENDER JAVA

    68

    Creating a connection using the Driver Manager class:-

    Now when a connection is needed, one of the

    DriverManager.getConnection() methods is used to create a JDBC

    connection.

    Connection conn = DriverManager.getConnection(url);