computer programming (tmk 3102) lecture notes 2

Upload: mamat88

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    1/37

    Chapt er 2: In t roduc t ion t o JAVA

    Session Plan (Week 2):To understand:Java Framework

    Java runtime environmentTerm API, IDE and JDK

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    2/37

    TMK 3102-Chap 1: Introduction toJAVA

    2

    Hist ory of J AVA James Gosling and Sun Microsystems

    Early name for Java - Oak

    Java, May 20, 1995, Sun World

    HotJava

    The first Java-enabled Web browser

    Early History Website:

    http://java.sun.com/features/1998/05/birthday.html

    http://java.sun.com/features/1998/05/birthday.htmlhttp://java.sun.com/features/1998/05/birthday.html
  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    3/37

    TMK 3102-Chap 1: Introduction toJAVA

    3

    Why J ava?The answer is that Java enables users to deploy applications

    on the Internet for servers, desktop computers, and small

    hand-held devices. The future of computing will be

    profoundly influenced by the Internet, and Java promisesto remain a big part of that future.

    Java is a general purpose programming language.

    Java is the Internet programming language.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    4/37

    TMK 3102-Chap 1: Introduction toJAVA

    4

    J ava, Web, and Beyond Java can be used to develop Web

    applications.

    Java Applets Java Servlets and JavaServer Pages

    (JSP)

    Java can also be used to developapplications for hand-held devicessuch as Palm and cell phones

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    5/37

    TMK 3102-Chap 1: Introduction toJAVA

    5

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java is partially modeledon C++, but greatlysimplified and improved.

    Some people refer to Javaas "C++--" because it islike C++ but with morefunctionality and fewer

    negative aspects.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    6/37

    TMK 3102-Chap 1: Introduction toJAVA

    6

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java is inherently object-oriented.Although many object-oriented languagesbegan strictly as procedural languages,Java was designed from the start to beobject-oriented. Object-oriented

    programming (OOP) is a popularprogramming approach that is replacingtraditional procedural programmingtechniques.

    One of the central issues in software

    development is how to reuse code.Object-oriented programming providesgreat flexibility, modularity, clarity, andreusability through encapsulation,inheritance, and polymorphism.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    7/37

    TMK 3102-Chap 1: Introduction toJAVA

    7

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Distributed computing involvesseveral computers working togetheron a network. Java is designed tomake distributed computing easy.Since networking capability isinherently integrated into Java,writing network programs is likesending and receiving data to andfrom a file.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    8/37

    TMK 3102-Chap 1: Introduction toJAVA

    8

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    You need an interpreter to run Javaprograms. The programs arecompiled into the Java VirtualMachine code called bytecode. Thebytecode is machine-independentand can run on any machine thathas a Java interpreter, which is partof the Java Virtual Machine (JVM).

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    9/37

    TMK 3102-Chap 1: Introduction toJAVA

    9

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java compilers can detect manyproblems that would first show up atexecution time in other languages.

    Java has eliminated certain types oferror-prone programming constructsfound in other languages.

    Java has a runtime exception-

    handling feature to provideprogramming support forrobustness.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    10/37

    TMK 3102-Chap 1: Introduction toJAVA

    10

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java implements several securitymechanisms to protect your systemagainst harm caused by strayprograms.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    11/37

    TMK 3102-Chap 1: Introduction toJAVA

    11

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Write once, run anywhere

    With a Java Virtual Machine (JVM),you can write one program that will

    run on any platform.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    12/37

    TMK 3102-Chap 1: Introduction toJAVA

    12

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Because Java is architectureneutral, Java programs areportable. They can be run on anyplatform without being recompiled.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    13/37

    TMK 3102-Chap 1: Introduction toJAVA

    13

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java can execute very fast with thesupport of multithreading, clusteringand load balancing.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    14/37

    TMK 3102-Chap 1: Introduction toJAVA

    14

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Multithread programming issmoothly integrated in Java,whereas in other languages youhave to call procedures specific to

    the operating system to enablemultithreading.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    15/37

    TMK 3102-Chap 1: Introduction toJAVA

    15

    Charac t er is t i c s o f Java

    Java Is Simple

    Java Is Object-Oriented

    Java Is Distributed

    Java Is Interpreted Java Is Robust

    Java Is Secure

    Java Is Architecture-Neutral

    Java Is Portable

    Java's Performance

    Java Is Multithreaded

    Java Is Dynamic

    Java was designed to adapt to anevolving environment. New codecan be loaded on the fly withoutrecompilation. There is no need for

    developers to create, and for usersto install, major new softwareversions. New features can beincorporated transparently asneeded.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    16/37

    TMK 3102-Chap 1: Introduction toJAVA

    16

    J DK

    Java Development Kit (JDK)

    JDK for

    Standard Edition (SE) Enterprise Edition (EE) Mobile Edition (ME)

    Use for producing JAVA program

    Can be downloaded fromhttp://www.java.sun.com

    http://www.java.sun.com/http://www.java.sun.com/
  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    17/37

    TMK 3102-Chap 1: Introduction toJAVA

    17

    J DK Edi t ions

    Java Standard Edition (J2SE or Java SE) J2SE/Java SE can be used to develop client-side

    standalone applications or applets.

    Java Enterprise Edition (J2EE or Java EE) J2EE/Java EE can be used to develop server-side

    applications such as Java servlets and JavaServerPages.

    Java Micro Edition (J2ME or Java ME). J2ME/Java ME can be used to develop applications

    for mobile devices such as cell phones.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    18/37

    TMK 3102-Chap 1: Introduction toJAVA

    18

    Quest ions

    Why are we using JAVA?

    List the characteristics of JAVA

    Programming Language What is JVM? What it is for?

    What is JDK? What is for?

    Give example of JDKs.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    19/37

    TMK 3102-Chap 1: Introduction toJAVA

    19

    Firs t J ava Program

    //This program prints Welcome to Java!

    public class Welcome {public static void main(String[] args) {

    System.out.println("Welcome to Java!");

    }

    }

    Example 1.1

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    20/37

    TMK 3102-Chap 1: Introduction toJAVA

    20

    Creat e & Edi t J ava Sourc e Code

    To use Notepad, type

    notepad Welcome.java from the DOS prompt.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    21/37

    TMK 3102-Chap 1: Introduction toJAVA

    21

    Creat e & Edi t J ava Sourc e Code

    To use WordPad, type

    write Welcome.java

    from the DOS prompt.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    22/37

    TMK 3102-Chap 1: Introduction toJAVA

    22

    Creat ing, Com pi l ing, and Running Program s

    Source Code

    Create/Modify Source Code

    Compile Source Code

    i.e., javac Welcome.java

    Bytecode

    Run Byteode

    i.e., java Welcome

    Result

    If compilation errors

    If runtime errors or incorrect result

    package chapter1;

    public class Welcome {

    public static void main(String[] args) {System.out.println("Welcome to Java!");}

    }

    Method Welcome()

    0 aload_0

    Method void main(java.lang.String[])

    0 getstatic #2 3 ldc #3

    5 invokevirtual #4

    Saved on the disk

    stored on the disk

    Source code (developed by the programmer)

    Byte code (generated by the compiler for JVM

    to read and interpret, not for you to understand)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    23/37

    TMK 3102-Chap 1: Introduction toJAVA

    23

    Com pi l ing and Running J ava

    f rom t he Com m and Window

    Set path to JDK bin directory

    set path=c:\Program Files\java\jdk1.5.0\bin Set classpath to include the current directory

    set classpath=. Compilejavac Welcome.java

    Runjava Welcome

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    24/37

    TMK 3102-Chap 1: Introduction toJAVA

    24

    Anat om y of a J ava Program

    Comments Package Reserved words Modifiers Statements Blocks

    Classes Methods The main method

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    25/37

    TMK 3102-Chap 1: Introduction toJAVA

    25

    Comments

    In Java, comments are preceded by (1) two slashes (//) in a line(2) enclosed between /* and */ in one or multiple lines. Whenthe compiler sees //, it ignores all text after // in the sameline. When it sees /*, it scans for the next */ and ignores anytext between /* and */.

    //This program prints Welcome to Java!

    package chapter1;

    public class Welcome {

    public static void main(String[] args) {

    System.out.println("Welcome to Java!");

    }

    }

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    26/37

    TMK 3102-Chap 1: Introduction toJAVA

    26

    Package

    The second line in the program (package chapter1;)specifies a package name, chapter1, for the classWelcome. Forte compiles the source code inWelcome.java, generates Welcome.class, and stores

    Welcome.class in the chapter1 folder.//This program prints Welcome to Java!

    package chapter1;

    public class Welcome {

    public static void main(String[] args) {

    System.out.println("Welcome to Java!");

    }

    }

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    27/37

    TMK 3102-Chap 1: Introduction toJAVA

    27

    Reserved Words

    Reserved words or keywords are words that have a specificmeaning to the compiler and cannot be used for otherpurposes in the program. For example, when the compilersees the word class, it understands that the word after classis the name for the class. Other reserved words in exampleprogram are public, static, and void.

    //This program prints Welcome to Java!

    package chapter1;

    public class Welcome {

    public static void main(String[] args) {System.out.println("Welcome to Java!");

    }

    }

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    28/37

    TMK 3102-Chap 1: Introduction toJAVA

    28

    J ava K eyw ords

    do new abstract break synchronized

    if default assert private implements

    int goto this throw protected

    try return import public instanceof

    byte else throws double transient

    case void boolean short package

    for final interface static extends

    char long finally strictfp volatile

    class float native super while

    const catch continue switch

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    29/37

    TMK 3102-Chap 1: Introduction toJAVA

    29

    Modi f iers

    Java uses certain reserved words calledmodifiers that specify the properties of thedata, methods, and classes and how they

    can be used. Examples of modifiers arepublic and static. Other modifiers areprivate, final, abstract, and protected. Apublic datum, method, or class can beaccessed by other programs. A private

    datum or method cannot be accessed byother programs.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    30/37

    TMK 3102-Chap 1: Introduction toJAVA

    30

    Sta tements

    A statement represents an action or asequence of actions. The statementSystem.out.println("Welcome to

    Java!") in the example program is astatement to display the greeting"Welcome to Java!

    Every statement in Java ends with asemicolon (;).

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    31/37

    TMK 3102-Chap 1: Introduction toJAVA

    31

    Blocks

    A pair of braces in a program forms a block thatgroups components of a program

    public class Test {

    public static void main(String[] args) {

    System.out.println("Welcome to Java!");

    }

    }

    Class blockMethod block

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    32/37

    TMK 3102-Chap 1: Introduction toJAVA

    32

    Classes

    The class is the essential Java construct. Aclass is a template or blueprint for objects.

    To program in Java, you must understandclasses and be able to write and use them.The mystery of the class will continue to beunveiled throughout subject. For now,

    though, understand that a program isdefined by using one or more classes.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    33/37

    TMK 3102-Chap 1: Introduction toJAVA

    33

    Methods

    What is System.out.println? It is a method: acollection of statements that performs a sequence ofoperations to display a message on the console. Itcan be used even without fully understanding thedetails of how it works. It is used by invoking astatement with a string argument. The stringargument is enclosed within parentheses. In thiscase, the argument is "Welcome to Java!" You can

    call the same println method with a differentargument to print a different message.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    34/37

    TMK 3102-Chap 1: Introduction toJAVA

    34

    m ain Method

    The main method provides the control of program flow.The Java interpreter executes the application byinvoking the main method.

    The main method looks like this:

    public static void main(String[] args) {

    // Statements;}

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    35/37

    TMK 3102-Chap 1: Introduction toJAVA

    35

    J ava Program St ruc t ure

    import and package section;

    Class Sectionpublic class classIdentifier {

    }

    Global Declaration

    Main method sectionpublic static void main (String[] args) {

    }

    Local Declaration

    Java Statement

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    36/37

    TMK 3102-Chap 1: Introduction toJAVA

    36

    Quest ions

    List the anatomy of JAVA program.

    Give example of JAVA keywords.

    What is the meaning of modifiers? Give example of modifiers.

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 2

    37/37

    TMK 3102 Chap 1: Introduction to 37

    Thank you.