Java基础概念

Variables & Fields 变量与字段

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.

变量 和字段是两种不同的东西。
变量包括局部变量参数,初始化于栈中
字段包括实例字段类字段,初始化于堆中

Naming 名字

命名规则基本与C一致

原子数据类型

byte,short,int,long,float,char
原子数据类型是语言内建支持的,不是从类创建的。
string不是原子类型,但是语言有特殊支持。

Array 数组

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.

Java基础概念


  1. //数组可以这样声明,但不推荐
  2. int[] anArray;
  3. //数组初始化
  4. anArray = new int[10];
  5. //列表初始化
  6. int[] anArray = {
  7. 100, 200, 300,
  8. 400, 500, 600,
  9. 700, 800, 900, 1000
  10. };

多维数组

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:


  1. class MultiDimArrayDemo {
  2. public static void main(String[] args) {
  3. String[][] names = {
  4. {"Mr. ", "Mrs. ", "Ms. "},
  5. {"Smith", "Jones"}
  6. };
  7. // Mr. Smith
  8. System.out.println(names[0][0] + names[1][0]);
  9. // Ms. Jones
  10. System.out.println(names[0][2] + names[1][1]);
  11. }
  12. }

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);

上一篇:使用动态SQL语句实现Sbo的客户物料组销售二维分析


下一篇:【原】iOS:一种直接修改frame的某个属性的方法