重溫Java經典教程(The Java™ Tutorials)第三篇-Java語言-第一章
(Java語言定義的四種類型的變數)The Java programming language defines the following kinds of variables:
- Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the
static
keyword. Non-static fields are also known as instance variablescurrentSpeed
of one bicycle is independent from thecurrentSpeed
of another. - Class Variables (Static Fields) A class variable is any field declared with the
static
static
since conceptually the same number of gears will apply to all instances. The codestatic int numGears = 6;
final
could be added to indicate that the number of gears will never change. - Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example,
int count = 0;
). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. - Parameters You've already seen examples of parameters, both in the
Bicycle
class and in themain
method of the "Hello World!" application. Recall that the signature for themain
method ispublic static void main(String[] args)
. Here, theargs
variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
原始資料型別(省略)
陣列型別
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main
method of the "Hello World!" application. This section discusses arrays in greater detail.
操作運算子和優先順序
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
(典型的i++和++i操作)
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++;
and ++result;
will both end in result
being incremented by one. The only difference is that the prefix version (++result
) evaluates to the incremented value, whereas the postfix version (result++
) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
表示式,語句,塊
Now that you understand variables and operators, it's time to learn about expressions, statements, and blocks. Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.
條件控制語句Control Flow Statements
The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. This section describes the decision-making statements (if-then
, if-then-else
, switch
), the looping statements (for
, while
, do-while
), and the branching statements (break
, continue
, return
) supported by the Java programming language.