2017年6月29日 下午12:09
第一个部分:数组定义
定义的方法:
1. int num[]= new int[10];必须说明长度 int初始化为0
2. int []num;
num=new int[5]
3. int num[]=new int[]{1,2,3,4}
4. int num[]={1,2,4,5,6,7}
String string1[] = {“1”,”2”};
长度方面的错:
1. String str[] = new String[];不能不说明长度
2. int num[10];编译出错 说明长度情况下,必须要有指向的内容
3. String str[] = new String[3]{“1”,”2”,”3”}; 编译错
先定义后初始化(一对三错):
String str[];
str = new String[]{“1”,”2”};对
String str[];
str[] = new String[]{"1","2"};错
String str[];
str = {"1","2"};错
int str[];
str ={1,2};错总结:分开只能按1写
第二部分:局部 and 全局 基本类型 and 对象类型 数组 and 非数组
1 | package package1; |
输出:
1 | 10 |
[“”]自己加的
1 | package package1; |
输出:
1 | 10 |
发现:
1. 他们的输出是一样的,说明局部变量的概念对数组无效

图1:对象声明为局部
图2:对象声明为全局
发现:
1. 对于非数组对象(单个对象),局部变量概念有效
加入基本类型后:
1. 局部中:
基本类型 对象类型
数组(只定义长度) 0 null
非数组(只对象声明) 错 错
3. 全局中:
基本类型 对象类型
数组(只定义长度) 0 null
非数组(只对象声明) 0 null
总结:
对于数组类型: 基本类型是0,对象类型为null
对于非数组:局部中一定错 ,全局中 0 和 null
String 和 Student的区别(单个对象+以初始化)
1. String的默认值是"" ,Student默认值为包名+地址
2. 其他的,Student String 数组是完全一样的 注:注意前提为以初始化