Statement and create Java arrays

Today in the brush Java question, write C ++ used to find forgotten manipulate Java arrays, then included the previously written articles sent to study it a bit.

First of all, there are several ways to create an array of ways?

Array Java program must be initialized before you can use, so-called initialization, that is, and specify the initial value for each array element is an element allocates memory array object, but in Java, arrays are static, once the array initialization, length is already determined, can not be altered.

Declare array variables

You must first declare an array variable, you can use the array in your program. Here is the syntax for declaring an array variable:

dataType[] arrayRefVar;   // 首选的方法
 
或
 
dataType arrayRefVar[];  // 效果相同,但不是首选方法 c++沿用

But more than just declare an array, and can not be used directly, because there is no assigned memory space, this time must be initialized.

For example, the following code will be given in Java8

public class test{
    public static void main(String[] arg) { 
        int[] a;
        a[0]= 1;
        System.out.println(a[0]);
    }
}
// 系统将报错 局部变量 a 可能尚未初始化
// 进一步加入我个人的理解,声明的数组没有确定大小,没有办法分配内存空间,所有必须初始化
// 但是以下代码在Java中是禁止的
public class test{
    public static void main(String[] arg) { 
        int[10] a;
        a[0]= 1;
        System.out.println(a[0]);
    }
}
// 系统将报错 标记“10”上有语法错误,删除此标记
// 不能像c++这样初始化数组

So, we have two ways to initialize an array of

  1. Static initialization: determining by the program ape their initial value of each array element, and the length of the array by the system to decide, for example:
int[] a = {1,2,3,4,5};
System.out.println(a.length);
// 第二行代码可以看出,数组拥有类的性质,可以用dot访问其变量
// 第一行代码静态初始化了数组整型数组a,赋予其固定的长度,系统可以为其分配空间
  1. Dynamic Initialization: length of the array is determined by the initialization program apes, and not a value of the array element, assigned by the system to the initial value, for example:
int[] b=new int[5];
System.out.println(b[0]);
// 很明显,数组就是类

Array must initialize it? It called to initialize the array initialization exactly what? It is an array variable itself?

The answer is that we must certainly look, but I found in practice is not necessarily required, in order to understand this problem, we need to understand the array variable and the array object distinction. And before you know they need to understand Java data types. Java data types are divided into two types, basic data types and reference data types .

The basic data types are eight: byte, short, int, long , float, double, boolean, char. Only you need to know the data type of the variable contains a basic value, the value of the variable type, respectively.

Reference data types : reference value variable with a different basic types of variables, the variable value is a reference pointing to the memory space (address). Points to memory holds a value or variable represented by a set of values. It pointers in C ++ and is very similar, in fact, is actually a reference pointer in the Java language, is a pointer to an object memory address. Java does not support pointers just said does not support the calculation of the pointer, but the pointer type is retained, and called a reference type. After declaring the reference data types, it can not be used directly, but also instances of open space in the memory heap memory.

Array variable is a reference type variable, an array variable is an array object pointing to the heap memory of them, not the array itself. When changing the array referenced by an array variable, it can cause the array of variable length artifacts. In fact the length of the array object itself does not change this, but the array variable to point to a new array object.

So for the array variable, he does not need to be initialized, in fact, we often say that the initialization is to initialize an array of objects rather than an array of variables, sometimes we are not initialized, and let the array variable points to a valid object array, the array can also be used ,E.g:

int[] a = {0,1,2,3,4};
int[] b;    
b = a;  
System.out.println(b[1]);
// 输出结果为 1
b[1] = 99;  
System.out.println(a[1]);
// 输出结果为 99
我自己的理解是,Java的数组就是一个对象。初始化数组的最好办法就是new一个。
  • Enhanced circulation of the array (other sets can also be used)

    Enhanced Java loop as follows:

    for(声明语句:表达式){
      // 代码
    }
    // 冒号可以理解为"in"

    Disclaimer statement : declare a new local variable, and the type of the variable must match the type of array elements. Loop whose scope is defined in the block, and its value is equal to the value of array elements in this case ( instead of an array subscript !! ).

    Expression : Expression is the name of the array to be accessed, or method that returns the value of the array.

    Examples

    public class Test {
       public static void main(String args[]){
          int [] numbers = {10, 20, 30, 40, 50};
    
          for(int x : numbers ){
             System.out.print( x );
             System.out.print(",");
          }
          System.out.print("\n");
          String [] names ={"James", "Larry", "Tom", "Lacy"};
          for( String name : names ) {
             System.out.print( name );
             System.out.print(",");
          }
       }
    }

    Output

    10,20,30,40,50,
    James,Larry,Tom,Lacy,

    We look back and understand that the order of execution:

    1. Create a integer variable named x
    2. The first element of numbers assigned to x
    3. Execute the code within the curly braces
    4. Assigned to the next element x
    5. Repeat until all the elements are running

Java variable types

  • There are three types of variables in Java
    • Class variables: independent variables other than the method using a modified static
    • Examples of variables: independent of variables other than the method, but no static modification
      • Each object of a particular class of variables are instance objects (as the name implies)
    • Local variables: Class Variable in Method
public class variable{
    static int a = 0; // 类变量
    
    String str = "Java is easy to learn."; // 实例变量 实例变量声明在类内而不是方法中
    
    public void method{
        int b = 1; // 局部变量 局部变量是声明在方法中的
        // 局部变量在使用前必须初始化
        int x; 
        int z = x + 1; // 这就是错误的代码,无法编译
    }
}

Declare and initialize a variable type

  • Instance variables will always be the default. If there is no clear assignment to instance variables, or no calls setter, instance variables are still the default.
    • integers 0
    • floating points 0.0
    • Booleans false
    • references null
  • Local variables no default

Guess you like

Origin www.cnblogs.com/scyq/p/11656076.html