java syntax.pdf

Upload: bala-ratnakar-koneru

Post on 04-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Java Syntax.pdf

    1/30

    Basic Java/C SyntaxHarry Erwin, PhDCSE301

  • 8/14/2019 Java Syntax.pdf

    2/30

    Resources Flanagan, D., 2005,Java in a Nutshell, 5th

    edition, OReilly, ISBN 0-596-00773-6

    McLaughlin, B, and Flanagan, D, 2004,Java 1.5Tiger: A Developers Notebook, OReilly, ISBN0-596-00738-8

    Arnold, K., and Gosling, J., 1998, The JavaProgramming Language, Second Edition,Addison-Wesley

  • 8/14/2019 Java Syntax.pdf

    3/30

    Topics

    Punctuation Operators Statements

  • 8/14/2019 Java Syntax.pdf

    4/30

    Purpose This is a review of basic C syntax as it is

    used by Java

    We will review: Punctuation Operators

    Statements

  • 8/14/2019 Java Syntax.pdf

    5/30

    Separators

    () are used in method calls {} are used to bracket blocks [] are used for array element access are used in generics (Java 5)

  • 8/14/2019 Java Syntax.pdf

    6/30

    Block Structure {} Java, like C and C++, is a block structured language.

    Programmes are organised as nested blocks (Russian dolls), eachproviding a local referencing environment. Names are visible fromdeclaration to the end of the containing block. Names in innerblocks hide the same names in containing blocks. {} are used todelimit blocks.

    Memory is divided into a stack and a random access area (like theheap or free store in C or C++). Primitive types, references toreference types, addresses, and method parameters live in the

    stack, which represents the local referencing environment.Reference types live in the random access area.

    When you enter a block or declare a variable, the stack grows.When you leave a block, the stack shrinks.

  • 8/14/2019 Java Syntax.pdf

    7/30

    More Separators

    : is used in the ?: operator.

    ; is a statement terminator. , is used as a separator in various places. . is used for object member access.

    @ is used in documentation comments.

  • 8/14/2019 Java Syntax.pdf

    8/30

    Operators +, -, *, / are used in arithmetic. +, - are also used to give the signs of

    numbers. ++, -- are used to increment and decrement. ~ is bitwise complement.

    ! is boolean NOT.

    new is an object creation operator.

  • 8/14/2019 Java Syntax.pdf

    9/30

    More Operators (type) is used for a type conversion. % gives the integer remainder. + and += are used in String concatenation. > is right shift with sign extension.

    >>> is right shift with zero extension.

  • 8/14/2019 Java Syntax.pdf

    10/30

    Yet More Operators < and and >= are greater than/greater than or

    equal.

    instanceof is used in type comparison. == is equals (identical value or same

    object).

    != is not equal, !(a==b)

  • 8/14/2019 Java Syntax.pdf

    11/30

    Even Yet More

    Operators & is bitwise/boolean AND | is bitwise/boolean OR

    ^ is bitwise/boolean XOR && is conditional AND (fails as soon as an

    element is false)

    || is conditional OR (succeeds as soon as anelement is true)

    ?: conditional (ternary) operator

  • 8/14/2019 Java Syntax.pdf

    12/30

    Had Enough Operators?

    = is assignment

    *=, /=, %=, +=, -=, =, >>>=, &=, ^=, |=are all assignment with the correspondingoperation being performed

    i+=1; is the same as i++; or i = i+1; forexample. x &= y; is the same as x = x&y;

  • 8/14/2019 Java Syntax.pdf

    13/30

    Operator Precedence

    and Associativity Operators have precedence and associativity Precedence describes the order in which

    they are grouped if there are no ()

    Associativity describes how a^b^c isevaluated: (a^b)^c or a^(b^c).

    Use parentheses for clarity--having to memorise

    this stuff is cruel and unusual punishment.

  • 8/14/2019 Java Syntax.pdf

    14/30

    Reserved Words abstract, assert, boolean, break, byte, case,

    catch, char, class, const, continue, default, do,double, else, enum, extends, false, final,

    finally, float, for, goto, if, implements, import,instanceof, int, interface, long, native, new,null, package, private, protected, public,return, short, static, strictfp, super, switch,synchronized, this, throw, throws, transient,true, try, void, volatile, while.

    Whew!

  • 8/14/2019 Java Syntax.pdf

    15/30

  • 8/14/2019 Java Syntax.pdf

    16/30

    Expression Statements

    Expression statements can have side effects.They include:

    assignments increments and decrements method calls

    object creation

    Terminated with a ;

  • 8/14/2019 Java Syntax.pdf

    17/30

  • 8/14/2019 Java Syntax.pdf

    18/30

    Empty Statement

    A single ; Sometimes useful, particularly in loops.

  • 8/14/2019 Java Syntax.pdf

    19/30

    Local Variable

    Declarations Format is type variableName; May also include an initializer:

    type variableName = otherVariable; type variableName = new type(args); String stringName = the string;

    Can appear anywhere. Look up scope orlexical scope. Limited to the local block.

  • 8/14/2019 Java Syntax.pdf

    20/30

    Control Flow

    if-else

    switch while and do-while for

  • 8/14/2019 Java Syntax.pdf

    21/30

    if-else

    Syntax: if (boolean-expression)

    statement1; // true case else // optional, but tricky statement2; // false case

    Be careful about which if the else is boundto. It will be bound to the innermost if thatdoesnt yet have an else statement.

  • 8/14/2019 Java Syntax.pdf

    22/30

    if-if-else

    The following is also legal:

    if (boolean1) statement1;

    else if (boolean2) statement2; etc... // you know what I mean here. else statementN; /* executed if none of

    the other statements are executed. */

  • 8/14/2019 Java Syntax.pdf

    23/30

    switch

    switch (integer expression or enumeration type) {

    case value1: statement1;

    case value2: statement2; break; default: statementDefault; break;

    case value3: statement3; break;

    }

    Note that if case value1 is executed, statements 1 and2 are executed. To prevent that, you use break; orreturn;

  • 8/14/2019 Java Syntax.pdf

    24/30

    while and do-while

    while(boolean expression) statement;

    do statement while(boolean expression); The difference is whether the boolean

    expression is evaluated before or after thestatement is executed in the loop.

  • 8/14/2019 Java Syntax.pdf

    25/30

    for

    for(init; (boolean expression); incr) statement;

    init; is executed once at the beginning,

    (boolean expression); is executed at the beginning of eachloop to see if the loop continues.

    statement; is executed for each loop. continue; skips to incr;

    incr; is executed for each loop

    Infinite loop

    for (;;) statement;

  • 8/14/2019 Java Syntax.pdf

    26/30

    Labeled Statement

    label: statement;

    Used by the break and continue statementsas a destination. break label;

    continue label;

  • 8/14/2019 Java Syntax.pdf

    27/30

    break and continue

    A break; statement exits from any block orstatement

    A break label; exits from the block orstatement named label. A continue; or continue label; skips to the

    end of a loops body.

    There is no goto.

  • 8/14/2019 Java Syntax.pdf

    28/30

    return

    return; or return retval; exits the methodand returns to the calling method. The stackgets unwound for this.

  • 8/14/2019 Java Syntax.pdf

    29/30

  • 8/14/2019 Java Syntax.pdf

    30/30

    Conclusions

    Basic Java syntax is similar to that in C andC++, with some exceptions.

    Review it, because we will expect you toknow it from the beginning. There are some differences that we will

    cover from time to time.

    We will also cover some new stuff in Java 5