首先一些被初始化对象的概念:
类变量(类静态成员): 类被加载时产生;它的生存期是类的生存期; 自动初始化(类被加载)
成员变量(类成员): 在使用new创建该类实例时产生;它的生存期与该类实例对象的生存期相同; 自动初始化(构造方法被调用)
局部变量(类的方法中的变量): 该方法被调用时产生; 在该方法退出后结束; 必须显式初始化
1 方法中的变量,不会被自动初始化。比如main方法中的变量index,定义如int index; 它不会被自动初始化。
2 当用new定义一些变量,会自动初始化,如 int [] d = new int[100]; 它自动初始化为0。
各数据类型的数组自动初始值如下:
byte 0
short 0
int 0
long 0l
float 0.0f
double 0.0d
char '\u0000' 即数值0,而非字符'0',因为它是0~~65535的序列。字符'0'位置在48上,即'\u0030'
boolean false
reference null 如String
3 类成员如果没有被定义会自动获取一个初始值。首先运行声明的类成员,如果有定义,则取定义值,随后调用构造函数,如果构造函数中对已定义变量重赋值,则取重赋的值。
关于对象的创建
为新对象分配内存空间必须执行new Xxx()语句调用。
new语句执行以下操作:
a 为新对象分配内存空间,并根据声明的数据类型初始化为默认值。
b 若有赋值,赋值。
c 执行构造函数
d 变量被分配为一个到内存堆中的新对象的引用。
4 Stack类型自动初始化为空。空显示为[],而不是null。Boolean是引用类型和String类似吧,初始值为null;而boolean是基本类型,初始值为false
5 数组定义
可以如下定义
int iArray[] = null;
这里,iArray[]可以使用了,已经被初始化了。
如:
public class test10 {
public static void main(String args[])
{
int iArray[] ;
iArray[5] = 56; //这里提示iArray没有初始化
}
}
显示:编译错误
比较:
public class test10 {
public static void main(String args[])
{
int iArray[] = null;
iArray[5] = 56;
}
}
显示:编译正常
6 静态块和类成员定义的优先级
静态块内应是在类载入的时候就进行操作。要比成员变量要早,因此,首先int i = 10;其次 i = 20;
如下:
public class Test
{
int i = 20;
static
{
int i = 10;
}
public static void main(String [] args)
{
Test a = new Test();
System.out.println(a.i);
}
}
运行结果:20 其中static中的i属于代码块。
7 关于局部变量和类成员之间的关系: 如果局部变量和类成员同名,那么该方法中认局部变量。
举例:
class Test
{
static int myArg ;
public static void main(String[] args)
{
int myArg; //如果此移行没有写,即可通过编译了。
System.out.pringln(myArg);
}
}
通不过编译。
8 对于Boolean的初始赋值不能赋true,false.
如:
选出正确:
a Object o = new Button("A");
b Boolean flag = true;
c Panel p = new Frame();
d Frame f = new Panel();
e Panel p = new Applet();
答案:a,e
另:注意一下Panel,Frame等。
What will happen when you attempt to compile and run the following code snippet?
Boolean b = new Boolean("TRUE"); //不区分大小写
if(b.booleanValue())
{
System.out.println("Yes : " + b);
}
else
{
System.out.println("No : " + b);
}
A.The code will not compile.
B.It will print – Yes: true
C.It will print – Yes: TRUE
D.It will print – No: false
E.It will print – No: FALSE
选择:B
Boolean的构造器构造时,看参数String,如果不是null,是boolean中的true,忽略大小写,则为 true,否则为false。
new Boolean("TRUE") :true.
new Boolean("anything") :false.
The internal toString() representation of this object produces the boolean value string in lower case, hence it prints "Yes : true" instead of "Yes : TRUE".
9 关于类中的常量必须初始化,不然会产生编译错误。而static会自动初始化。类中只有final必须主动初始化。
比如:
public class test14
{
final int x; //这里把final int改为int编译正常
public test14()
{
System.out.println(x+10);
}
public static void main(String args[])
{
test14 mt = new test14();
}
}
显示:编译错误
public class FinalTest {
final int p;
final int q=3;
FinalTest(){
p=1;
}
FinalTest(int i){
p=i;
//q=i; //(*)如果去掉注释会有编译错误
}
}
显示:编译正常
如果定义的常量未赋初值则可以在构造方法中给他赋值,但是如果定义时已经给常量赋值,如q,则不能再为他赋值,如(*)
10 类初始化的顺序是,首先是static对象,接着是非static对象。如果是同一种,则初始化顺序由在类中的定义顺序决定。
比如:
class Test
{
Test(int i)
{
System.out.println("Test("+i+")");
}
}
public class Q12
{
static Test t1 = new Test(1);
Test t2 = new Test(2);
static Test t3 = new Test(3);
public static void main(String[] args)
{
Q12 Q = new Q12();
}
}
显示:Test(1) Test(3) Test(2)
11 Byte的初始方法
1) Byte b = new Byte(123); // Byte b = new Byte(123);
2) Byte b = new Byte("123");
3) Byte b = new Byte(); b = 123; //错误
4) Byte b = new Byte((int)123.4);
5) Byte b = new Byte(0x123); //编译错误,必须把0x123强制转成byte类型
有两个构造方法
Byte(byte value)
Byte(String s)
Which two create an instance of an array? (Choose two)
D.Object oa=new float[20];
Answer: D
Which two cause a compiler error? (Choose two)
D.boolean results[]=new boolean[3]{true,false,true};
Answer: D
错在[]中有3。
byte[] array1,array2[]
byte array3[][]
byte[][] array4
if each has been initialized, which statement will cause a compile error?
A.array2=array1 B.array2=array3 C.array2=array4
Answer:A