Java Tutorial 2

 
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 string constants. 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.
  • Literals are data constants. They can be either numbers, characters or strings. Examples of numbers are true (boolean), 123 (integer) and 1.2 (floating point). Examples of character literals are 'a' and '/t'. Examples of strings are "hello world" and "hi/n/How are You?".
  • Identifiers are names for variables and functions. The first character must be a letter, underscore or dollar sign. Following characters can also include digits. Letters are A to Z, a to z, and Unicode characters above hex 00C0. Java style normally uses an initial capital letter for a class identifier, all uppercase for constants and lowercase for method and variable identifiers. Note: an identifier must not be from the 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 allow print formatting as well as preventing certain characters from causing interpretation 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

  • Variables are temporary data holders. Variable names (or identifiers) must begin with a letter, underscore or dollarsign, use ASCII or Unicode characters 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 */
  • Variables can be made constant or read only by prepending the modifier final to the declaration. Java convention uses all uppercase for final variable names.

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)
  • Primative array objects are created and allocated memory based on their static array size and type by using the new reserved word. The number of items in an array can then be determined by accessing its length property. For a two dimensional array named M, M.length gives the number of elements in its first dimension and M[0].length gives the number of elements in its second dimension.
  • Since Java is a strongly typed language, required changes in data type must be explicitly done with 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.
  • Conditions are phrases that can be evaluated to a boolean value such as a comparison operator between two constants, variables or expressions used to test a dynamic situation. Examples are x <= 5 and bool_flag != true.

Statements, Blocks and Scope

  • Statements are 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.
  • Variables maintain their definition (or 'scope') until the end of the execution block that they are defined in. This is the reason why variable declaration and assignment can be a two step process

Assignment Statements

  • Assignment statements use an assignment operator to store a value or the result of an expression in a variable. Memory allocation is done at the time of assignment.
  • 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值