Java Language Summary.pdf

download Java Language Summary.pdf

of 16

description

Java Language Summary.pdf

Transcript of Java Language Summary.pdf

  • A P P E N D I X A

    Java LanguageSummary

    A P P E N D I X A

    Java LanguageSummary

    A P P E N D I X A

    Java LanguageSummary

  • This appendix contains a summary of the Java language that you can use as a quick reference.

    Reserved WordsThe following is a list of Javas reserved words, also known as keywords.You cannot use them to name identifiers.

    The boolean literals true and false, and the null literal, while not technicallykeywords, cannot be used as identifiers.

    3

    abstract default if private this

    boolean do implements protected throw

    break double import public throws

    byte else instanceof return transient

    case extends int short try

    catch final interface static void

    char finally long strictfp volatile

    class float native super while

    const for new switch

    continue goto package synchronized

  • Primitive Data TypesThere are eight primitive data types in Java. They are listed here, alongwith the number of bits used for each type. The ranges of possible valuesfor the integral types are provided. Floating point constants that representundefined numbers are also provided here for your reference.

    4 Learn java In a Weekend

    TABLE A.1 PRIMITIVE DATA TYPE LISTING

    Type Number of Bits

    boolean 1

    byte 8

    short 16

    char 16

    int 32

    float 32

    long 64

    double 64

  • *NaN stands for not a number and represents values of undefined operations suchas 0.0 / 0.0.

    APPENDIX A Java Language Summary 5

    TABLE A.2 RANGES FOR INTEGRAL TYPES

    Type Minimum Maximum

    byte -27 27 1

    short -215 215 1

    int -231 231 1

    long -263 263 1

    char 0 216 1

    TABLE A.3 FLOATING POINT CONSTANTS

    Float.NEGATIVE_INFINITY

    Float.POSITIVE_INFINITY

    Float.NaN*

    Doube.NEGATIVE_INFINITY

    Double.POSITIVE_INFINITY

    Double.NaN*

  • CommentsThree types of comments are used in Java (single line, multi-line, andjavadoc).

    Single line comment example:

    // this is a single line comment

    Multi-line comment example:

    /* this is a multi-line

    comment */

    Javadoc comment example:

    /**

    * This Is a javadoc comment

    */

    LiteralsLiterals are used in Java to represent values that are of the primitive,String, or null types in source code. To summarize their syntaxes, any-thing within square brackets ([ and ]) is optional, whereas the bar char-acter (|) separates different options; for example, [+|-] means either +,-, or neither (since they are within square brackets).

    Integer LiteralsInteger literals can be expressed in octal (base 8), decimal (base 10), orhexadecimal (base 16). Octal digits can be any digit from 0 to 7. Decimaldigits can be any digit ranging from 0 to 9. Hexadecimal digits can be anydigit ranging from 0 to 9 and also any letter (case insensitive) from A to F.A=10, B=11, C=12, D=13, E=14, F=15. The syntax for an integer literal is

    [+|-][0[X|x]]number[L|l]

    6 Learn java In a Weekend

  • Floating Point LiteralsA floating point literal can be either a float or a double. The syntax fora floating point number is

    [+|-]number.number[[+|-]Eexponent|[+|-]eexponent][F|D|f|d]

    APPENDIX A Java Language Summary 7

    Example Description

    67 int literal having value 67

    +67 int literal having value 67

    -67 negative int literal having value 67

    012 octal int literal having value 10

    -0X27C hexadecimal int literal having value 636

    1234567890L long literal having value 1,234,567,890

    -0XBEL hexadecimal long literal having value -190

    Example Description

    -10. double literal having value -10.0

    +.01 double literal having value 0.01

    1.23 double literal having value 1.23

    1.23d double literal having value 1.23

    1.23f float literal having value 1.23

    2E4 double literal having value 20,000.0 (2x104)

    -133e-2F float literal having value -1.33 (-133x10-2)

  • Boolean LiteralsBoolean literals must be either true or false.

    Character LiteralsA character literal is a single character, or escape sequence enclosed in singlequotes. The data type of a character literal is always char. A Unicode char-acter escape sequence is in the form \unnnn, where nnnn is the hexadecimalrepresentation of the character. The syntax for a character literal is

    character|escape_sequence

    Exactly one character or escape sequence must appear within the singlequotes.

    8 Learn java In a Weekend

    Example Description

    a char literal a

    $ char literal $

    \u003F char literal ?

    \ char literal (single quote)

    \ char literal (double quote)

    \b char literal for backspace

    \f char literal for formfeed

    \n char literal for new line

    \r char literal for carriage return

    \t char literal for tab

    \\ char literal for backslash (\)

  • String LiteralsA string literal consists of a string of characters and/or escape sequenceswithin double quotes. The data type is always String. The syntax for aString literal is

    [characters&|escape_sequences]

    Null LiteralThe null literal is null.

    OperatorsBelow, arg refers to any variable or value. Some operators take onlycertain types of arguments. For example, ! works only on boolean types.

    APPENDIX A Java Language Summary 9

    Example Description

    An empty string

    Abc String literal Abc

    \Java\ String literal Java

    C:\\My Documents\\myfile.txt String literal C:\MyDocuments\myfile.txt

  • 10 Learn java In a Weekend

    Type Syntax Description

    Unary +arg, -arg Sign (positive or negative)

    ++variable Prefix increment

    variable++ Postfix increment

    --variable Prefix decrement

    variable-- Postfix decrement

    !arg Boolean compliment (Not)

    ~arg Bitwise inversion

    (type)arg Cast

    Arithmetic arg + arg Addition

    arg - arg Subtraction

    arg * arg Multiplication

    arg / arg Division

    arg % arg Modulus

    Shift arg >> arg Left shift

    arg >> arg Right shift

    arg >>> arg Unsigned right shift

  • APPENDIX A Java Language Summary 11

    Type Syntax Description

    Comparison arg < arg Less than

    arg > arg Greater than

    arg = arg Greater than or equal to

    arg == arg Equal to

    arg != arg Not equal to

    arg instanceof class Instance of

    Bitwise arg & arg Bitwise AND

    arg | arg Bitwise OR

    arg ^ arg Bitwise XOR

    Logical arg && arg Logical AND

    arg || arg Logical OR

    Ternary condition ? val_if_true : val_if_false Conditional operator

  • Assignment OperatorsAssignment operators store a value in a variable. This section covers theoperators that include the equal sign (=). (The increment (++) and decre-ment (--) operators perform assignments as well, but are not covered inthis section.) The assignment operator can be just the equal sign or theequal sign followed by an additional operator (note that the combinationof the two constitutes one single operator). The syntax for the assignmentoperator is

    variable =[op] arg

    If = is combined with another operator, the operation is logically equiva-lent to

    variable = variable op arg

    The following are all assignment operators:

    = += -= *= /= &= |= ^= %=

    = >>>=

    LoopsThis section summarizes the syntax used for the three types of loops: for,while, and do-while.

    12 Learn java In a Weekend

    Name Syntax

    for loop for([init, init, ];[condition];[update,update, ]) { body }

    while loop while(condition) { body }

    do loop do { body } while (condition);

  • Break and ContinueThe break and continue statements redirect the flow of loops. breakwill take control out of the loop and continue will return control backto the top of the loop.

    ConditionalsThe conditional statements are if, and switch, as well as the Ternaryoperator described in the section Operators. The syntax for the if con-ditional is

    if (condition) {

    statements_condition_true;

    }

    [else if (other_condition) {

    statements_other_condition_true;

    }, ]

    [else {

    statements_no_condition_true;

    }]

    The square brackets in the previous code indicate that the else if andelse statements are optional and are not part of the syntax.

    The syntax for the switch conditional is

    switch (test) {

    case value: [statements_test_equals_value] [break;]

    [default: [statements_test_equals_no_case_value]

    }

    APPENDIX A Java Language Summary 13

  • Try-Catch Blockstry-catch blocks are used for exception handling. Here is the syntax:

    try {

    statements_that_may_cause_an_exception;

    } catch (exception_type exception_variable_name) {

    [statements_that_execute_if_exception_is_caught]

    } finally {

    [statements_that_execute_whether_or_not_an_exception_occurs]

    }

    Class DefinitionThe syntax for defining a class is

    [package package_name;]

    [[import Imported_class_or_package;] ]

    [access_modifier] class class_name [extends super_class_name][implements implemented_Interface, ] {

    [class_definition]

    }

    Class ModifiersThis section describes the Java keywords used to modify classes.

    14 Learn java In a Weekend

    Modifier Description

    public Access modifier.

    final Cannot be subclassed.

    abstract Must be subclassed (cannot be instantiated).

  • Constructor DefinitionThe first line of the constructor body must be either an explicit call to thesuper-classs constructor using super(), an implied call to the super-classs no-arg constructor, or a call to another constructor within this classusing this(). The syntax for a constructor is as follows:

    [access_modifier] class_name([arg, ]) { [constructor_body] }

    Variable DeclarationThe syntax for declaring a variable is as follows:

    [access_modifier] [modifier, ...] type variable_name [ = initial_value];

    Variable ModifiersVariable modifiers are Java keywords that modify the characteristics of avariable.

    APPENDIX A Java Language Summary 15

    Modifier(s) Description

    public, protected, private Access modifiers.

    static Used to indicate this is a class variable.

    final Used to indicate a constant.

    transient Used to indicate a variable thatcannot be serialized.

    volatile Used to indicate that variable maybe modified asynchronously.

  • Method DefinitionUnless the return type is void, the last executable statement of any logicalpath of execution within the method must be a return statement thatreturns a value of type return_type. The syntax for defining a methodis as follows:

    [access_modifier] [modifier, ] return_type method_name([arg, ])[throws exception, ] { [body] }

    Method ModifiersMethod modifiers are Java keywords that are used to modify the charac-teristics of a method.

    16 Learn java In a Weekend

    Modifier(s) Definition

    public, protected, private Access Modifiers.

    static Indicates this is a class method.

    final Indicates that this method cannotbe overridden.

    abstract Indicates the body must be definedin a subclass.

    native Indicates the method body isdefined outside of Java in a nativelibrary.

    synchronized Indicates that at most only onethread may have control of thismethod at any given time.