Lecture1_Basics of OOP and Java

  • Upload
    dev

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

  • 8/6/2019 Lecture1_Basics of OOP and Java

    1/30

    HLCL

    Jinesh kumar singh(Cabin D-208).

  • 8/6/2019 Lecture1_Basics of OOP and Java

    2/30

    About Course The objective of this course is to expose the

    students to concepts of OOP with Java. T1: An Intro. To OOP with Java. C Thomas Wu

    , . . R1: The complete Ref. JAVA J2SE,Herbert

    Schildt, 5 th edition. TMH, 2005. R2: Prog. With java, E.Balagurusamy, 2 nd

    edition.TMH 2006

  • 8/6/2019 Lecture1_Basics of OOP and Java

    3/30

    Test 1:15% Test2(Mid Sem):20% Test3(Online LAB Exam):15% Compre:40% Quiz(After Mid Sem):10%

  • 8/6/2019 Lecture1_Basics of OOP and Java

    4/30

    Introduction to Java History: Java is based on the two lang. C and

    C++. C lang.(by Dennis Ritchie) is the first high level

    .oriented lang. C++ lang.(by Bjarne Stroustrup) is the

    extension of C(C + OOP).C++ is partiallyObject oriented lang.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    5/30

    Java is developed by James Gosling, patrick

    naughton, chris warth, Ed frank and Mikesheriden at Sun Microsystems Inc. in 1991. Itsname was Oak at that time. Later in 1995 it isrenamed as Java.

    The primary motivation of java was platformindependence lang. that can be used to createsoftware (embedded s/w) for consumerelectronics devices.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    6/30

    Later it is observed that java can be used for

    internet also(due to platform-independency).Today most browsers are integrated with javaruntime environment(JRE).The key point of java

    for internet are Applet(client-side), Servlets(server-side) Security(control access within Java execution

    environment only, not to other part of Comp.) Portability(Same applet code work on all system.)

  • 8/6/2019 Lecture1_Basics of OOP and Java

    7/30

    Bytecode: The security and portability feature of

    java comes from bytecodes. The javacompiler(javac) generate output for a virtualmachine(JVM) not for the real machine / real

    .bytecodes and produce output. JVM differs platform to platform but all JVM

    understand same bytecode. Thus as long as JVMis installed all java prog. will run irrespective of h/w platform.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    8/30

    Java Features

    a) Simple h)Interpretedb) Secure i)High performance

    c or a e s r u ed) Object oriented k)Dynamice) Robust

    f) Multithreadedg) Architecture neutral

  • 8/6/2019 Lecture1_Basics of OOP and Java

    9/30

    The latest version of java is Java SE 6(internally

    1.6). J2SE:Java 2 standard edition( JRE, Packages)

    , ,Framework, API etc)

    J2ME:Java 2 micro edition(for mobile and

    portable devices)

  • 8/6/2019 Lecture1_Basics of OOP and Java

    10/30

    Some basic Java tools

    javac: Compiler for Java java: The launcher for Java applications. javadoc: API documentation generator.

    .appletviewer: Run and debug applets

    jar: Create and manage Java Arc h ive (JAR) files. jdb: The Java Debugger. javah: C header and stub generator. javap: Class file disassembler

  • 8/6/2019 Lecture1_Basics of OOP and Java

    11/30

    CH-2 OOP and Java

  • 8/6/2019 Lecture1_Basics of OOP and Java

    12/30

    OOP and Java The two approaches of programming

    a) Structured/process-oriented: Like in C. Followtop down approach.

    ec r en e : e n ava. o ow o omup approach.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    13/30

    OOP The principle of OOP are

    a) Abstraction: Hiding complexity.(object in java, Car asa object not as a collection of various parts likeengine, break, gear etc).

    keep safe from outside interference.(A class in java).c) Inheritance: Reuse of existing class/code.(More

    feature are added in subclass then base class).

    d) Polymorphism: Different behavior for differentinputs.(One interface shared by multiple methods).

  • 8/6/2019 Lecture1_Basics of OOP and Java

    14/30

    A simple java Prog. /*

    This is a simple Java program. Call this file "Example.java". */

    // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } }

  • 8/6/2019 Lecture1_Basics of OOP and Java

    15/30

    Save the file with name Example.java(as the classname is Example which contains the main method.)

    Compile the program with>javac Example.java

    will create a separate class file for each class. A classfile is nothing but the bytecodes for JVM). Execute the program with

    >java ExampleThis will run the code and give the output message.This is a simple Java program.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    16/30

    If the program contain more then one class the

    file name will be the class name which containmain() method. By doing this we will get the class file name

    (Example.class) same as the class name itcontain.( class Example). When we execute java Example, then we are

    actually specifying the class name that we wantto execute. So the interpreter will search for a fileclassname.class(Example.class)

  • 8/6/2019 Lecture1_Basics of OOP and Java

    17/30

    Comments: // single line comment

    /* Multilinecomment */

    ocumen a oncomments for javadoc */

  • 8/6/2019 Lecture1_Basics of OOP and Java

    18/30

    class Example declares a class with name

    Example. public static void main(String args[]) declaresmain method.

    ,visibility of class member. When a class memberis preceded by public, then that member may beaccessed by code outside the class in which it is

    declared. In this case, main( ) must be declared aspublic, since it must be called by code outside of its class when the program is started.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    19/30

    The keyword static allows main( ) to be called

    without having to instantiate a particular instanceof the class. This is necessary since main( ) iscalled by the Java interpreter before any objects

    . The keyword void simply tells the compiler that

    main( ) does not return a value

    Java is case-sensitive. Thus, Main is different frommain.(with Main() the program will compile buton execution give error main() not found).

  • 8/6/2019 Lecture1_Basics of OOP and Java

    20/30

    In main( ), there is only one parameter. String args[ ]declares a parameter named args, which is an array of instances of the class String. ( Arrays are collections of similar objects.)

    System.out.println("This is a simple Java program.");

    This line outputs the string This is a simple Javaprogram. followed by a new line on the screen.Output is actually accomplished by the built-in println() method. System is a predefined class that provides

    access to the system, and out is the output stream thatis connected to the console. (System class, out objectand println() method.)

  • 8/6/2019 Lecture1_Basics of OOP and Java

    21/30

    Question What if the program contain more then two

    main methods.(Error/compile, how to execute.

    Is it necessary to keep the file name same asclass name. If no then how we will execute the

    program.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    22/30

    class A{public static void main(String args[]) {System.out.println("Hi Main A");

    }}

    public static void main(String args[]) {System.out.println("Hi Main B");

    }} Save with file name ex1.java, compile it. It will generate two files

    A.class and B.class. When you write java A o/p is Hi Main A andwhen you write java B o/p is Hi Main B.

  • 8/6/2019 Lecture1_Basics of OOP and Java

    23/30

    class Example2 {

    public static void main(String args[]) {int num; // this declares a variable called numnum = 100; // this assigns num the value 100

    " ". .num = num * 2;System.out.print("The value of num * 2 is ");System.out.println(num);

    }}

  • 8/6/2019 Lecture1_Basics of OOP and Java

    24/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    25/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    26/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    27/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    28/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    29/30

  • 8/6/2019 Lecture1_Basics of OOP and Java

    30/30