Lectre 1- Java Intro

Embed Size (px)

Citation preview

  • 8/3/2019 Lectre 1- Java Intro

    1/24

    1

    Java Programming

    Java Basics

  • 8/3/2019 Lectre 1- Java Intro

    2/24

    2

    Contents

    Background Java Virtual Machine

    Application & Applet

    Class & Object

    3 Principles of Object Oriented Programming

    Start up Java

    Variables & Assign

    String & Character

    Arithmetic Operator & Expression

    Comment Array

    Keywords

  • 8/3/2019 Lectre 1- Java Intro

    3/24

    3

    Background / History

    Sun Microsystems Inc.

    James Gosling

    1990, Green Project

    1991, Develop the Language for Household Products => To

    General Developing Language

    Java

    James Gosling, Arthur Van Hoff, Andy Bechtolsheim

    Coffee Materials in Indonesia

    Write once, Run everywhere

    Spread Rapidly through WWW and Internet

  • 8/3/2019 Lectre 1- Java Intro

    4/24

    4

    Byte Code and Java Virtual Machine

    Existing Development Environment

    Source

    Code

    Compiler(Pentium)

    Compiler(PowerPC)

    Compiler(SPARC)

    Binary

    File

    Pentium

    PowerPC

    SPARC

    Binary

    File

    Binary

    File%cc Hello.c o Hello

    % Hello

    Binary CodeRun

  • 8/3/2019 Lectre 1- Java Intro

    5/24

    5

    Byte Code and Java Virtual Machine

    Java Development Environment

    Java Compiler

    (Pentium)

    Java Compiler

    (PowerPC)

    Java Compiler

    (SPARC)

    Java

    Interpreter

    Pentium

    PowerPC

    SPARC

    Java

    Interpreter

    Java

    Interpreter

    Java

    Code

    Java

    ByteCode

    (Independent on

    Platform)

    %javac Hello.java

    Hello.class created

    % java Hello

    Byte CodeRun JVM

  • 8/3/2019 Lectre 1- Java Intro

    6/24

    6

    Java Program

    Java Application Program

    Application

    Program written in general programming

    language

    Applet

    Program running in Web Browser Environment

    Can be viewed by appletviewer or Web browser

    with JVM

  • 8/3/2019 Lectre 1- Java Intro

    7/247

    Class and Object

    Object

    Memory Space to Define State and Operation

    Instance of Class

    Class Template of Creating Object

  • 8/3/2019 Lectre 1- Java Intro

    8/248

    3 Principles of Object Oriented Programming

    Encapsulation

    Control Access the data and method in program code

    Inheritance

    Inherit the data and methods of parent class

    Polymorphism

    Shape

    Ellipse Rectangle Triangle

    getArea()

    getArea() getArea()getArea()

    Shape Obj = new Ellipse();

    Obj.getArea();

    Which Area?

  • 8/3/2019 Lectre 1- Java Intro

    9/249

    Java Class Library

    java.applet : Applet related java.awt : Abstract Window Toolkit

    java.awt.event : Event process from awt component

    java.awt.image : Image processing

    java.beans : JavaBeans Component java.io : File or Network I/O Support

    java.lang : Java Language Support

    java.net : Network related functions

    java.util : Utility function

  • 8/3/2019 Lectre 1- Java Intro

    10/2410

    Java 2 SDK

    Java 2 Software Development Kit (Java 2 SDK)

    Java Application or Applet Development and

    Running Environment

    Java 2 Runtime Environment (JRE) Java Running Environment

    Java 2 Standard Edition

    Java 2 Micro Edition Java 2 Enterprise Edition

  • 8/3/2019 Lectre 1- Java Intro

    11/24

    11

    Java Program Structure

    In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements

    These terms will be explored in detail throughout the course A Java application always contains a method calledmain

  • 8/3/2019 Lectre 1- Java Intro

    12/24

    12

    Java Program Structure

    public class MyProgram{

    }

    // comments about the class

    class header

    class body

    Comments can be placed almost anywhere

  • 8/3/2019 Lectre 1- Java Intro

    13/24

    13

    Java Program Structure

    public class MyProgram{

    }

    // comments about the class

    public static void main (String[] args){

    }

    // comments about the method

    method headermethod body

  • 8/3/2019 Lectre 1- Java Intro

    14/2414

    Start Java Application

    Hello.java

    class Hello {

    public static void main(String args[]) {

    System.out.println(Hello World);

    }

    % javac Hello.java

    % java Hello

    Compile

    Run

    hello.c

    void main() {

    printf(Hello World\n);

    }

    % cc hello.c o hello

    % hello

    Hello WorldResult

    Java C

    Class Name

    Main method

    Main Function

  • 8/3/2019 Lectre 1- Java Intro

    15/24

    15

    Variable and Assign

    Variable

    Type

    char 16bits Unicode character data

    boolean Boolean Variable

    byte 8 bits signed integer short 16 bits signed integer

    int 32 bits signed integer

    long 64 bits signed integer

    float 32 bits signed floating point number

    double 64 bits signed floating point number

  • 8/3/2019 Lectre 1- Java Intro

    16/24

    16

    Variable and Assign

    int num = 100;

    long m = 21234234L

    double type : .5 0.8 9e-2 -9.3e-5float type : 1.5e-3f;

    Variable Declaration

    type varName;

    Value assign

    varName = value;

    float x, y, x;

  • 8/3/2019 Lectre 1- Java Intro

    17/24

    17

    String and Character

    String : sequence of character

    String s = Enter an integer value: ;

    Char c = A;

    Concatenation Operator +

    String s = Lincoln said: + \ Four score and seven years ago\ ;

    Result :

    Lincoln said: Four score and seven years ago

    Unicode

  • 8/3/2019 Lectre 1- Java Intro

    18/24

    18

    Arithmetic Operator

    Operators

    + - * / %

    += -= *= /= %=

    ++ --

    5 / 2

    2 Why isnt it 2.5 ?5 % 2 14 / 2 24 % 2 0

    count * num + 88 / val19 % count

    j += 6; j = j + 6;

  • 8/3/2019 Lectre 1- Java Intro

    19/24

    19

    Comment

    Single Line Comment

    int i = 10 ; //i is counter

    Multiple Line Comment

    /*

    Some comments*/

    Documentation Comment

    /**

    Documentation Comment

    */

    Using javadoc Tool,

    make document

  • 8/3/2019 Lectre 1- Java Intro

    20/24

    20

    Arrays ( One Dimensional)

    type VarName[]

    Definition of One Dimensional Array

    int ia[];

    varName = new type[size]

    Assign Range to One Dimensional Array

    ia = new int[10];

    type varName[] = new type[size]

    Declaration of One Dimensional Array with Range

    int ia[] = new int[10];

  • 8/3/2019 Lectre 1- Java Intro

    21/24

    21

    Arrays ( One Dimensional)

    varName.length

    Number of elements in Array

    Type varName[] = {e0, , en};

    Initialization of One Dimensional Array

    int j[] = {0, 1, 2, 3, 4, 5};

    int k[];

    K = j;

    Example 1.15

  • 8/3/2019 Lectre 1- Java Intro

    22/24

    22

    Arrays ( Multi-Dimensional)

    type VarName[][];

    Definition of Two Dimensional Array

    float fa[][];

    varName = new type[size1][size2];

    Assign Range to Two Dimensional Array

    fa = new float[2][3];

    type varName[][] = new type[size1][size2];

    Declaration of Two Dimensional Array with Range

    float fa[] = new float[2][3];

  • 8/3/2019 Lectre 1- Java Intro

    23/24

    23

    Arrays ( Two Dimensional)

    varName.length

    Number of elements in Multi-Dimensional Array

    Type varName[][] = {{e00,

    , e0n}, {e10,

    ,e1y}, {e20,

    , e2z}};

    Initialization of Multi-Dimensional Array

    Example 1.16

    varName[index].length

    Number of elements in Each elements of Multi-Dimensional Array

    Number of Raw

  • 8/3/2019 Lectre 1- Java Intro

    24/24

    24

    Java Keywords

    47 Java Keywordsabstract double int super

    boolean else interface switch

    break extends long synchronized

    byte final native this

    case finally new throwcatch float package throws

    char for private transient*

    class goto* protected try

    const* if public void

    continue implements return volatile

    default import short while

    do instanceof static