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 variables because their values are unique to each instance of a class (to each object, in other words); the currentspeed of one bicycle is independent from the currentspeed of another.
執行個體變量,每個執行個體不同。
class variables (static fields) a class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. a field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. the code static int numgears = 6; would create such a static field. additionally, the keyword final could be added to indicate that the number of gears will never change.
類變量由static定義,靜态變量,全局靜态存儲,類共享。
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 the main method of the “hello world!” application. recall that the signature for the main method is public static void main(string[] args). here, the args 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.
參數,參數是方法簽名中定義的變量,存儲于堆棧。
the java programming language uses both “fields” and “variables” as part of its terminology. instance variables (non-static fields) are unique to each instance of a class. class variables (static fields) are fields declared with the static modifier; there is exactly one copy of a class variable, regardless of how many times the class has been instantiated. local variables store temporary state inside a method. parameters are variables that provide extra information to a method; both local variables and parameters are always classified as “variables” (not “fields”). when naming your fields or variables, there are rules and conventions that you should (or must) follow.
變量 和字段是兩種不同的東西。
變量包括局部變量和參數,初始化于棧中
字段包括執行個體字段和類字段,初始化于堆中
命名規則基本與c一緻
byte,short,int,long,float,char
原子資料類型是語言内建支援的,不是從類建立的。
string不是原子類型,但是語言有特殊支援。
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.
<code>//數組可以這樣聲明,但不推薦</code>
<code>int[] anarray;</code>
<code>//數組初始化</code>
<code>anarray = new int[10];</code>
<code>//清單初始化</code>
<code>int[] anarray = {</code>
<code>100, 200, 300,</code>
<code>400, 500, 600,</code>
<code>700, 800, 900, 1000</code>
<code>};</code>
you can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as string[][] names. each element, therefore, must be accessed by a corresponding number of index values.
in the java programming language, a multidimensional array is an array whose components are themselves arrays. this is unlike arrays in c or fortran. a consequence of this is that the rows are allowed to vary in length, as shown in the following multidimarraydemo program:
<code>class multidimarraydemo {</code>
<code>public static void main(string[] args) {</code>
<code>string[][] names = {</code>
<code>{"mr. ", "mrs. ", "ms. "},</code>
<code>{"smith", "jones"}</code>
<code>// mr. smith</code>
<code>system.out.println(names[0][0] + names[1][0]);</code>
<code>// ms. jones</code>
<code>system.out.println(names[0][2] + names[1][1]);</code>
<code>}</code>
the output from this program is:
mr. smith
ms. jones
finally, you can use the built-in length property to determine the size of any array. the following code prints the array’s size to standard output:
system.out.println(anarray.length);