Java Tutorial 2

来源:互联网 发布:校园app 软件下载 编辑:程序博客网 时间:2024/06/15 06:54
 
Syntax Notation
["public"] ["abstract""final"]"class" class_name ["extends" object_name]"{"
// properties declarations
// behavior declarations"}"

Lexical Structure
  • Java is case sensitive.
  • Whitespace,tabs, and newline characters are ignored except when part of stringconstants. They can be added as needed for readability.
  • Statements terminate in semicolons! Make sure to always terminate statements with a semicolon.
  • Reserved words (or keywords) have special meanings within the language syntax.
  • Literalsare data constants. They can be either numbers, characters or strings.Examples of numbers are true (boolean), 123 (integer) and 1.2 (floatingpoint). Examples of character literals are 'a' and '/t'. Examples ofstrings are "hello world" and "hi/n/How are You?".
  • Identifiersare names for variables and functions. The first character must be aletter, underscore or dollar sign. Following characters can alsoinclude digits. Letters are A to Z, a to z, and Unicode charactersabove hex 00C0. Java style normally uses an initial capital letter fora class identifier, all uppercase for constants and lowercase formethod and variable identifiers. Note: an identifier must not be fromthe Java reserved word list.
  • Single line comments begin with //
  • Block comments begin with /* and end with */ [preferred]
  • Documentary comments begin with /** and end with **/

Literal Constants

  • Literal constants are values that do not change within a program.

Escape Characters

  • Escape(aka backslash) characters are used inside literal strings to allowprint formatting as well as preventing certain characters from causinginterpretation errors.
  • /b backspace /f formfeed /t horizontal tab
  • /" double quote /n newline /' single quote
  • /r carriage return // backslash /### Latin encoded character
  • /uHHHH Unicode encoded character

Variables

  • Variablesare temporary data holders. Variable names (or identifiers) must beginwith a letter, underscore or dollarsign, use ASCII or Unicodecharacters and underscore only, and are case sensitive.
  • Variables are declared with a datatype.
  • byte x,y,z; /* 08bits long, not assigned, multiple declaration */
  • short numberOfChildren; /* 16bits long */
  • int counter; /* 32bits long */
  • long WorldPopulation; /* 64bits long */
  • float pi; /* 32bit single precision */
  • double avagadroNumber; /* 64bit double precision */
  • boolean signal_flag; /* true or false only */
  • char c; /* 16bit single Unicode character */
  • Variablescan be made constant or read only by prepending the modifier final tothe declaration. Java convention uses all uppercase for final variablenames.

Arrays

  • Arrays allow you to store several related values in the same variable .
  • int i[]; /* one dimension array */
  • char c[][]; /* two dimension array */
  • float [] f; /* geek speak way */
  • Bowl shelfA[]; /* array of objects */
  • String flintstones[] = {"Fred", "Wilma", "Pebbles"}; //init values as well
  • Array memory allocation is assigned explicitly with the new operator and requires known static bounds (ie. number of elements).

Operators, Expressions, Conditions

  • Operators are actions that manipulate, combine or compare variables.
  • Assignment: = += -= /= %=
    Arithmetic: + - * / % (modulus) ++ (increment) -- (decrement)
    String Concatenation: +
    Comparison: == != > >= < <=
    Boolean Comparison: ! & ^ && (&& are short circuit ops)
    Bitwise Comparison: ~ & ^ (xor) << >> >>>
    Bitwise Assignment: &= = ^= (xor) <<= >>= >>>=
    Conditional: (expr1) ? expr2 : expr3
    Object Creation: new (eg int a[] = new int[10];)
    Casting of Type: (var_type)
  • Primativearray objects are created and allocated memory based on their staticarray size and type by using the new reserved word. The number of itemsin an array can then be determined by accessing its length property.For a two dimensional array named M, M.length gives the number ofelements in its first dimension and M[0].length gives the number ofelements in its second dimension.
  • Since Java is a stronglytyped language, required changes in data type must be explicitly donewith a cast operation. For example a = (int) b;.

 

  • Expressions are phrases used to combine values and/or operands using operators to create a new value.
  • Conditionsare phrases that can be evaluated to a boolean value such as acomparison operator between two constants, variables or expressionsused to test a dynamic situation. Examples are x <= 5 and bool_flag!= true.

Statements, Blocks and Scope

  • Statementsare complete program instructions made from constants, variables,expressions and conditions. Statements always end with a semicolon.
  • Execution blocks are the sections of code that start and end with curly brackets.
  • Variablesmaintain their definition (or 'scope') until the end of the executionblock that they are defined in. This is the reason why variabledeclaration and assignment can be a two step process

Assignment Statements

  • Assignmentstatements use an assignment operator to store a value or the result ofan expression in a variable. Memory allocation is done at the time ofassignment.
  • Variables may be assigned an initial value when declared.
  • boolean fileOpenFlag = true;, int finalScore = null; and final float PI = 3.14159;
i wanna run away never say goodbey
原创粉丝点击