java declaration array _java array declaration and creation

When I was writing Java questions today, I was used to writing C++ and found that I forgot the operation of Java arrays, so I sent out the articles I wrote before to review it.

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

The array in the Java program must be initialized before it can be used. The so-called initialization is to allocate memory space for the elements of the array object and specify the initial value for each array element. In Java, the array is static. Once the array is initialized, The length has already been determined and cannot be changed at will.

declare array variable

To use an array in a program, you must first declare an array variable. The following is the syntax for declaring an array variable:

dataType[] arrayRefVar; // preferred method

or

dataType arrayRefVar[]; // same effect, but not preferred method c++ inherited

But the above just declares the array and cannot be used directly, because there is no memory space allocated for it, it must be initialized at this time.

For example, the following code will report an error in Java8

public class test{

public static void main(String[] arg) {

int[] a;

a[0]= 1;

System.out.println(a[0]);

}

}

// The system will report an error that the local variable a may not have been initialized

// Further adding to my personal understanding, the declared array is not sized, there is no way to allocate memory space, all must be initialized

// But the following code is forbidden in Java

public class test{

public static void main(String[] arg) {

int[10] a;

a[0]= 1;

System.out.println(a[0]);

}

}

// The system will report that there is a syntax error on the error marker "10", delete this marker

// can't initialize array like c++

So, we have two ways to initialize the array

Static initialization: The initial value of each array element is determined by the programmer himself, while the length of the array is determined by the system itself, for example:

int[] a = {1,2,3,4,5};

System.out.println(a.length);

// The second line of code can see that the array has the properties of a class, and its variables can be accessed with dot

// The first line of code statically initializes the array integer array a, giving it a fixed length, and the system can allocate space for it

Dynamic initialization: The length of the array is determined by the programmer during initialization, and if there is no value of an array element, the system assigns the initial value, for example:

int[] b=new int[5];

System.out.println(b[0]);

// 很明显,数组就是类

数组一定要初始化吗?所谓的对数组初始化究竟是对什么初始化?是数组变量本身吗?

答案看上去肯定是一定要,但是我在实践中发现却是不一定需要,要想搞清楚这个问题,我们需要理解数组变量和数组对象的区别。而在认识他们之前需要先了解Java的数据类型。Java的数据类型分为两种,基本数据类型和引用数据类型。

基本数据类型有八种:byte,short,int,long,float,double,boolean,char。只需要知道基本数据类型变量包含一个值,这个值的类型与变量相对应。

引用数据类型:引用型变量的值与基本类型变量不同,变量值是指向内存空间的引用(地址)。所指向的内存中保存着变量所表示的一个值或一组值。这和C++中的指针很相似,事实上Java语言中的引用实际上就是指针,是指向一个对象的内存地址。Java所说的不支持指针只是不支持指针的计算,但是指针类型被保留了下来,并称为引用类型。声明引用数据类型后,不可以直接使用,还需要实例化在堆内存中开辟内存空间。

数组变量是一个引用类型的变量,数组变量是指向堆内存当中的数组对象,并不是数组本身。当改变一个数组变量所引用的数组,就可以造成数组长度可变的假象。实际上数组对象本身的长度本没有变化,只是数组变量指向了一个新的数组对象。

所以对于数组变量来说,他并不需要初始化,我们常说的初始化其实是初始化数组对象而非数组变量,有时候我们不进行初始化,而让数组变量指向一个有效的数组对象,数组也可以使用,例如:

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一个。

数组的增强循环(也可用于其他集合)

Java增强循环语句如下:

for(声明语句:表达式){

// 代码

}

// 冒号可以理解为"in"

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等(而不是数组的下标!!)。

表达式: 表达式是要访问的数组名,或者是返回值为数组的方法。

实例

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(",");

}

}

}

输出结果

10,20,30,40,50,

James,Larry,Tom,Lacy,

我们回头理解其执行顺序:

创建名为x的integer变量

将numbers的第一个元素赋值给x

执行大括号内的代码

赋值给下一个元素x

重复执行至所有的元素都被运行

Java的变量类型

Java中有三种变量类型

类变量:独立于方法之外的变量,用static修饰

实例变量:独立于方法之外的变量,不过没有static修饰

类的每个对象特定的变量也是实例对象(顾名思义)

局部变量:类的方法中的变量

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; // 这就是错误的代码,无法编译

}

}

声明与初始化变量类型

实例变量永远会有默认值。如果没有明确的赋值给实例变量,或者没有调用setter,实例变量仍有默认值。

integers 0

floating points 0.0

Booleans false

references null

局部变量没有默认值

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324092566&siteId=291194637