core mete rials

Post on 30-May-2018

227 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 8/14/2019 Core Mete Rials

    1/133

    CORE JAVA

    INTRODUCTION:

    Goslings solution to the problems of C++ was a new language called Oak. Oak

    preserved the familiar syntax of C++ but omitted the potentially dangerous features likeexplicit resources references, pointer arithmetic, and operator overloading. Oakincorporated memory management directly into the language, freeing the programmer toconcentrate on the tasks to be performed by the program. To be successful as anembedded systems programming language, Oak needed to be able to respond to real-world events within microseconds. It also needed to be portable; that is, it had to be ableto run on a number of different microprocessor chips and environments. This hardwareindependence would allow a toaster manufacturer to change the chip used to run thetoaster without changing the software. The manufacturer could also use some of thesame code that ran the toaster to run a similar appliance, such as a toaster oven. Thiswould cut down on development and hardware costs, as well as increase reliability.

    As Oak matured, the World Wide Web was growing dramatically, and thedevelopment team at Sun realized Oak was perfectly suited to Internet programming.Thus in 1994, they completed work on a product known as Web Runner, an early Webbrowser written in Oak. Web Runner was later renamed Hot Java, and it demonstratedthe power of Oak as an Internet development tool. Hot java is well versions underdevelopment.

    Finally, in 1995, Oak was renamed java (for marketing and legal reason) andintroduced at Sun World 95, since then, Javas rise in popularity has been meteoric. Evenbefore the first release of the java compiler in January 96, the development communityconsidered java a standard for Internet development.

    JAVA ARCHITECTURE

    To create reliable network based application, java language is used to run securelyover a network and work as wide range of platform.

    As write other programming languages, java uses a compiler to convert humanreadable code into executable program.

    Traditional compilers produce a code that can be executed by specific hardware.For ex. Windows95 c++ compiler creates executable that can be executed by Intelcompatible processors.

    Java compiler generates architecture independent byte code.This byte code can be executed by java virtual machine JVM, which is java

    architecture implemented in s/w rather than h/w.Java byte code files are called class files, because they contain single java class.To execute java byte codes, the JVM uses class reader to files the byte code from

    a disk or a n/w.Each class files is fed to a byte code verifier to check the class whether it is

    formatted correctly.The execution unit of JVM carries out the instructions specified in the byte code.

  • 8/14/2019 Core Mete Rials

    2/133

    This is an interpreter, which is a program that reads the byte codes, interprets thatmeaning and then performs the associated function.

    OOPS

    Oops is a way of thinking about the problem solutions.

    - Instead of taking program in top-down linear fashions, oops attempts to breakthe problem with its layout parts.

    - The solution focuses on them without objects and their relative to otherobjects.

    - In oops, the structure and its related routines and combined or encapsulatedwith a single entity called as class.

    - Routines defined inside a class are called member functions or methods.

    - The variables declared inside a class are called data members.- The class definition in a pattern or template for variables to be created likesthat class data type.

    - Variables which the class data types are called objects. An object is a storagevariable created based on a class.

    - Objects are called to be instances of a class.- Classes defined the variables and routines that are members of all the objects

    of that class.- An object variable is a reference to an object.- Creating a reference to an object and creating the actual object itself are two

    separate steps.- The object reference variable can have two possible values i.e. number or

    object.- To create an object referred by class ref. Variable like an new operator.- Encapsulation also allows having data hiding, which is a way to prevent direct

    access to the data members in the object.- This will force to methods to read data in member variables, rather than

    accessing them directly.Class Inheritance

    - Like technique, we can inherit both data members and methods of an thirstyclass into newly creation class.

    - The class, which is inherited, is called super class and the newly derived classis called as sub class.

    - We can early override the method of the parent class by regularly the methodin the sub class.

    - For this inheritance, we use extends clause such as- class B extends A- {- }

  • 8/14/2019 Core Mete Rials

    3/133

  • 8/14/2019 Core Mete Rials

    4/133

    Ex : class A{int x1;int x2;A (int x1)

    {this .x1 = x1;}A (int x1, int x2){this (x1);this .x2 = x2;}}

    Ex : class B extends A{

    int x3;B (int x1, int x2, int x3){Super (x1, x2);this .x3 = x3;}}

    Accesing super class members outside the class definition

    1. Data references are resolved at compile timeoa .x // As xob .x // Bs x

    (A) ob.x // As xoa = ob; oa.x // As x(B) oa.x // Bs x

    2. Method references are resolved at run timeoa .f1 () // As f1 ()ob .f1 () // Bs f1 ()oa = ob;oa.f1 () // Bs f1 ()

    Constructors and Finalize

    We can define two special kinds of methods in a class i.e.1. Constructors, i.e. the method, that return new instance of the class. If we do

    not define a constructor, we can use the default constructor to create aninstance of a class.

    2. Finalizers i.e. the functions that are called just before an object in garbagecollected.

  • 8/14/2019 Core Mete Rials

    5/133

    - The default constructor is automatically added to the class by the javacompiler.

    - The constructor allocates storage memory for any member variables that aredeclared on Javas built in data types.

    - A contractor is defined in the same way as an ordinary method, but it must

    have the same name as the class and have no return type.- We can define any number of constructors for a class by having differentparameter list and this is called constructor overloading.

    - We can define any no. of methods for a class with the same name anddifferent parameter list and this is called method overloading.

    - Java virtual machine automatically retains the memory used by an objectwhen no variable is referred to that object.

    - The process is known as garbage collection.- Finalizers are used in a situation where a class needs to clean itself before the

    garbage collection.- Finalizers are called just before an object of a class is garbage collector.

    - Finalizers are used to close opened files or connections to unsure that therelated taxes are completed before the object is garbage collected.- Example definition of this finalize method is

    Void finalize (){______}

    Examples :

    class A{

    int x1=10;

    int x2=20;

    int x3=30;

    A(int x1){

    this.x1=x1;

    }

    A(int x1,int x2)

  • 8/14/2019 Core Mete Rials

    6/133

    {

    this(x1);

    this.x2=x2;

    }

    A(int x1,int x2,int x3){

    this(x1,x2);

    this.x3=x3;

    }

    void display(){

    System.out.println(x1);

    System.out.println(x2);

    System.out.println(x3);

    }

    public static void main(String args[]){

    A oa1=new A(11);

    oa1.display();

    A oa2=new A(11,12);

    oa2.display();

    A oa3=new A(11,12,13);

    oa3.display();

    }

    }

  • 8/14/2019 Core Mete Rials

    7/133

    Example 2:

    class A{

    int x1=10;

    int x2=20;

    int x3=30;

    void display(){

    System.out.println(x1);

    System.out.println(x2);

    System.out.println(x3);

    }

    void adisplay(){

    System.out.println(" this is A display");

    display();

    }

    }

    class B extends A

    {

    int x1=40;

    int x2=50;

    int x4=60;

  • 8/14/2019 Core Mete Rials

    8/133

    void display(){

    System.out.println(x1);

    System.out.println(x2);

    System.out.println(x3);

    System.out.println(x4);

    }

    void bdisplay(){

    System.out.println("this is B display");

    display();

    }

    }

    class overrideex2{

    public static void main(String args[]){

    A oa=new A();

    oa.display();

    oa.adisplay();

    System.out.println(oa.x1);

    System.out.println(oa.x2);

    oa=new B();

    oa.display();

    oa.adisplay();

  • 8/14/2019 Core Mete Rials

    9/133

    //oa.bdisplay();

    System.out.println(oa.x1);

    System.out.println(oa.x2);

    System.out.println(oa.x3);

    //System.out.println(oa.x4);

    A oa1=new A();

    oa1.adisplay();

    B ob=new B();

    ob.adisplay();

    ob.bdisplay();

    }

    }

    Example 3:

    class A{

    int ax1=10;

    class B{

  • 8/14/2019 Core Mete Rials

    10/133

  • 8/14/2019 Core Mete Rials

    11/133

    void adisplay(){

    C oc=new C();

    oc.cdisplay();

    B ob=new B();

    ob.bdisplay();

    System.out.println(ax1);

    //System.out.println(bx1);

    }

    }

    class staticdemo2{

    public static void main(String args[]){

    A oa=new A();

    oa.adisplay();

    //B ob=new B();

    //A.B oab=new A.B();

    A.C oc=new A.C();

    oc.cdisplay();

    }

  • 8/14/2019 Core Mete Rials

    12/133

    }

    Example 4:

    abstract class A{

    int ax1=10;

    int ax2=20;

    void set(int p1,int p2