javamodule01

Upload: buttercup-gal

Post on 08-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 JavaModule01

    1/61

    Introduction

  • 8/7/2019 JavaModule01

    2/61

  • 8/7/2019 JavaModule01

    3/61

    Code:

    #include

    void main()

    {

    printf("Hello World");

    }

    Output:

    Hello World

  • 8/7/2019 JavaModule01

    4/61

    Code:

    #include

    void main()

    {

    cout

  • 8/7/2019 JavaModule01

    5/61

    Compilation refers to the processing

    of source code files (.c, .cc, or .cpp)

    and the creation of an 'object' file.

    Linking refers to the creation of asingle executable file from multiple

    object files.

  • 8/7/2019 JavaModule01

    6/61

    #include

    class Hello

    {

    public:

    void displayHello()

    {

    cout

  • 8/7/2019 JavaModule01

    7/61

    Code:

    class Hello

    {

    public static void main(String args[])

    {

    System.out.println(Hello World.");

    }

    }

    Output:

    Hello World

  • 8/7/2019 JavaModule01

    8/61

    Download and install the latest Java

    SDK (S/W Dev. Kit) from

    http://java.sun.com.Set the ENVIRONMENT

    set path=c:\j2sdk1.4.1_01\bin

    [Command Prompt (cmd)]

    Start > Settings > Control Panel > System> Advanced > System Variables > Path

    (c:\j2sdk1.4.1_01\bin)

  • 8/7/2019 JavaModule01

    9/61

    To compile the Hello program,

    execute the compiler, javac,

    specifying the name of the sourcefile on the command line, as shown

    here:

    Z:\Java> javac HelloWorld.java

  • 8/7/2019 JavaModule01

    10/61

    To run the program, you must use

    the Java interpreter, called java. To

    do so, pass the class name Hello as acommand-line argument, as shown

    here:

    Z:\Java> java Hello

  • 8/7/2019 JavaModule01

    11/61

  • 8/7/2019 JavaModule01

    12/61

    In 1990, Sun Microsystems begana project called Green for

    developing a software for theconvergence of digitallycontrolled consumer devices andcomputers.

    Patrick Naughton, MikeSheridan, and James Gosling Project Members.

  • 8/7/2019 JavaModule01

    13/61

    The Green software ran on an

    entirely new, processor-

    independent language.Gosling called the new language

    "Oak," after the tree outside his

    window.In 1995 Oak was renamed to

    Java.

  • 8/7/2019 JavaModule01

    14/61

    Bytecode is a highly optimized set of

    instructions designed to be executed

    by the Java run-time system, which

    is called the Java Virtual Machine

    (JVM).

    JVM is an interpreter for bytecode.

    JIT compiler is part of the JVM, it

    compiles bytecode into executable

    code in real time, on a piece-by-

    piece, demand basis.

  • 8/7/2019 JavaModule01

    15/61

    15

  • 8/7/2019 JavaModule01

    16/61

    16

  • 8/7/2019 JavaModule01

    17/61

    You write

    Java codeusing an

    editor

    javac MyProg.java

    java MyProg

    Java code:

    MyProg.java

    Bytecode:

    MyProg.class

    Text Editor

    Output

    You save

    the file

    with a .java

    extension

    You run theJava

    compiler

    'javac'

    You execute

    the bytecode

    with the

    command 'java'

    This createsa file of

    bytecode

    with a.class

    extension

    17

  • 8/7/2019 JavaModule01

    18/61

    Java can be used to create two

    types of programs: applications

    and applets.An application is a programthat runs on your computer,

    under the operating system ofthat computer.

    18

  • 8/7/2019 JavaModule01

    19/61

    An applet is an applicationdesigned to be transmitted over

    the Internet and executed by aJava-compatible Web browser.

    An applet is actually a tiny Javaprogram, dynamically

    downloaded across the network,just like an image, sound file, orvideo clip.

    19

  • 8/7/2019 JavaModule01

    20/61

    You write

    Java codeusing an

    editor

    javac MyApp.java

    appletviewer

    MyApp.html

    Java code:

    MyApp.java

    Bytecode:

    MyApp.class

    Text Editor

    Window

    You save the

    file with a.java

    extension

    You run the Java

    compiler 'javac'

    You can view the applet

    with the command

    'appletviewer'

    This creates a file

    of bytecode with a.class extension

    Web page:MyApp.html

    Text Editor

    Web

    Browser

    You write a

    web page inhtml using

    an editor

    You can view the web

    page from a webbrowser

    You save the

    file with a

    .html

    extension

    20

  • 8/7/2019 JavaModule01

    21/61

  • 8/7/2019 JavaModule01

    22/61

    Simple

    Secure

    PortableObject-oriented

    Robust (The degree towhich a system or

    component can functioncorrectly in the presence of

    invalid inputs or stressful

    environmental conditions.)

    Multithreaded

    Architecture-

    neutral

    Interpreted

    High performance

    Distributed

    Dynamic

  • 8/7/2019 JavaModule01

    23/61

    OOP Principles

    Encapsulation

    Inheritance

    Polymorphism Two Paradigms - code and data

    Process - oriented programming - code

    acting on data

    Object - oriented programming - datacontrolling access to code

    23

  • 8/7/2019 JavaModule01

    24/61

    Whitespace: space, tab, newline

    Identifiers: case-sensitive (class names,

    variables)

    Comment

    Single line: //

    Multi line: begin with /* and end with */

    Documentation: begin with /** and end with */

    Literals - a constant value

    Separators - () {} [] ; , .

    24

  • 8/7/2019 JavaModule01

    25/61

  • 8/7/2019 JavaModule01

    26/61

  • 8/7/2019 JavaModule01

    27/61

    Integers: This group includes byte, short,int, and long, which are for whole valuedsigned numbers.

    F

    loating-point numbers :This group includesfloat and double, which represent numberswith fractional precision.

    Characters This group includes char, whichrepresents symbols in a character set, like

    letters and numbers. Boolean: This group includes boolean, which

    is a special type for representing true/falsevalues.

    27

  • 8/7/2019 JavaModule01

    28/61

    28

    Name Width

    long 64

    int 32

    short 16

    byte 8

  • 8/7/2019 JavaModule01

    29/61

    29

    Name Width

    double 64

    float 32

  • 8/7/2019 JavaModule01

    30/61

    In Java char is a 16-bit type

    The range of a char is 0 to 65,536

    Java uses Unicode to representcharacters

    Unicode defines a fullyinternational character set that can

    represent all ofthe characters

    found in all human languages.

    30

  • 8/7/2019 JavaModule01

    31/61

    Escape Sequence Description

    \ddd Octal character (ddd)

    \uxxxx Hexadecimal UNICODE character(xxxx)

    \ Single quote

    \ Double quote

    \\ Backslash

    \r Carriage return

    \n New line (also known as line feed) \f Form feed

    \t Tab

    \b Backspace

    31

  • 8/7/2019 JavaModule01

    32/61

  • 8/7/2019 JavaModule01

    33/61

    class Scope {

    public static void main(String args[]) {

    int x; // known to all code within main

    x = 10;

    if(x == 10) { // start new scopeint y = 20; // known only to this block

    // x and y both known here.

    System.out.println("x and y: " + x + " " + y);

    x = y * 2;

    }

    // y = 100; // Error! y not known here

    // x is still known here.

    System.out.println("x is " + x);

    }

    }

    33

  • 8/7/2019 JavaModule01

    34/61

    When one type of data is assigned to another

    type of variable, an automatic typeconversion will take place if the following

    two conditions are met: The two types are compatible.

    The destination type is larger than the

    source type.

    When these two conditions are met, awidening conversion takes place.

    34

  • 8/7/2019 JavaModule01

    35/61

    To create a conversion between two

    incompatible types, you must use a cast. A

    cast is simply an explicit type conversion. It

    has the general form:(target-type) valueHere, target-type specifies the desired type

    to convert the specified value to.

    int a;

    byte b;

    b = (byte) a;

    35

  • 8/7/2019 JavaModule01

    36/61

    byte b = 50;

    b = b * 2; // Error! Cannot assign an int to a

    byte!

    The intermediate term b * 2 easilyexceeds the range of either of its byte

    operands. To handle this kind of problem,

    Java automatically promotes each byte or

    short operand to int when evaluating anexpression.

    byte b = 50;

    b = (byte)(b * 2);

    36

  • 8/7/2019 JavaModule01

    37/61

    One-Dimensional Arrays

    type var-name[ ];

    int month_days[];

    array-var = new type[size];

    month_days = new int[12];

    int month_days[] = new int[12];

    37

  • 8/7/2019 JavaModule01

    38/61

    Multidimensional Arrays

    int twoD[][] = new int[4][5];

    int twoD[][] = new int[4][ ];

    twoD[0] = new int[1];

    twoD[1] = new int[2]; twoD[2] = new int[3];

    twoD[3] = new int[4];

    38

  • 8/7/2019 JavaModule01

    39/61

    double m[][] = {

    { 0, 1, 2, 3 },

    { 0, 1, 2, 3 },{ 0, 1, 2, 3 },

    { 0, 1, 2, 3 }

    };

    39

  • 8/7/2019 JavaModule01

    40/61

    type[ ] var-name;

    int al[] = new int[3];int[] a2 = new int[3];

    40

  • 8/7/2019 JavaModule01

    41/61

  • 8/7/2019 JavaModule01

    42/61

  • 8/7/2019 JavaModule01

    43/61

    ++ Increment

    += Addition assignment

    = Subtraction assignment

    *= Multiplication assignment

    /= Division assignment

    %= Modulus assignment

    Decrement

    43

  • 8/7/2019 JavaModule01

    44/61

    ~ Bitwise unary NOT

    & Bitwise AND

    | Bitwise OR

    ^ Bitwise exclusive OR

    >> Shift right

    >>> Shift right zero fill

  • 8/7/2019 JavaModule01

    45/61

    &= Bitwise AND assignment

    |= Bitwise OR assignment

    ^= Bitwise exclusive OR assignment

    >>= Shift right assignment

    >>>= Shift right zero fill

    assignment

  • 8/7/2019 JavaModule01

    46/61

    == Equal to

    != Not equal to

    > Greater than

    < Less than

    >= Greater than or equal to

  • 8/7/2019 JavaModule01

    47/61

    & Logical AND

    | Logical OR

    ^ Logical XOR (exclusive OR)

    || Short-circuit OR

    && Short-circuit AND

    ! Logical unary NOT

    47

  • 8/7/2019 JavaModule01

    48/61

    &= AND assignment

    |= OR assignment

    ^= XOR assignment

    == Equal to

    != Not equal to

    ?: Ternary if-then-else

    48

  • 8/7/2019 JavaModule01

    49/61

  • 8/7/2019 JavaModule01

    50/61

  • 8/7/2019 JavaModule01

    51/61

    If

    if (condition) statement1;

    else statement2;

    Nested ifs A nestedif is an if statement that is the target

    of another if or else.

    51

  • 8/7/2019 JavaModule01

    52/61

    if-else-if Ladder

    if(condition)

    statement;

    else if(condition)statement;

    else if(condition)

    statement;

    ...else

    statement;

    52

  • 8/7/2019 JavaModule01

    53/61

    switch (expression) {

    case value1:

    // statement sequence

    break;

    case value2:// statement sequence

    break;

    ...

    case valueN:

    // statement sequencebreak;

    default:

    // default statement sequence

    }

    53

  • 8/7/2019 JavaModule01

    54/61

    switch(count) {

    case 1:

    switch(target) { // nested switch

    case 0:

    System.out.println("target is zero");

    break;

    case 1: // no conflicts with outer switch

    System.out.println("target is one");

    break;}

    break;

    case 2: // ...

    54

  • 8/7/2019 JavaModule01

    55/61

    The switch differs from the if in that switch

    can only test for equality, whereas ifcanevaluate any type of Boolean expression.

    That is, the switch looks only for a matchbetween the value of the expression and one

    of its case constants.

    55

  • 8/7/2019 JavaModule01

    56/61

    No two case constants in the same switch

    can have identical values. Of course, aswitch statement enclosed by an outer

    switch can have case constants in common. A switch statement is usually more

    efficient than a set of nested ifs.

    56

  • 8/7/2019 JavaModule01

    57/61

    while loop

    while(condition) {

    // body of loop

    } do-while loop

    do {

    // body of loop

    } while (condition)

    57

  • 8/7/2019 JavaModule01

    58/61

    for

    for(initialization; condition; iteration) {

    // body

    }Declaring Loop Control Variables Inside the

    for Loop

    Using the Comma

    Nested Loops

    58

  • 8/7/2019 JavaModule01

    59/61

    Java supports three jump statements: break,continue, and return.

    59

  • 8/7/2019 JavaModule01

    60/61

    class Break {

    public static void main(String args[]) {

    boolean t = true;

    first: {

    second: {

    third: {System.out.println("Before the break.");

    if(t) break second; // break out of second block

    System.out.println("This won't execute");

    }

    System.out.println("This won't execute");

    }

    System.out.println("This is after second block.");

    }

    }

    }

    60

  • 8/7/2019 JavaModule01

    61/61

    Thank You !!!