Core Java学习笔记
星期三, 4月 23rd, 2008最近在看core java ,记录了一些以前没仔细留意过的东西:
1.数组的定义
int[] a = new int[] { 1, 2, 3 };//普通定义
a = new int[] { 1, 2, 3, };// 多个逗号,数组长度还是3
int[] b = { 1 };// 定义变量,声明数组的时候可以不用new
String[][] strs = { { “a” }, { “b” } };
//// strs = { { “a” }, { “b” } };//声明好的变量再赋值的时候,这样就会出错
strs = new String[][]{ { “a” }, { “b” } };//声明好的变量再赋值的时候,必须写上 new
2.static 代码块,
在类中可以定义多个static的代码块,
可以放在类的任何位置(不在方法里),类被加载的时候,从上到下依次执行.
例如
package com.lizongbo;
public class CoreJavaTest {
static {
System.out.println(”bbb”);
}
static {
System.out.println(”cccc”);
}
public static void main(String[] args) throws Exception {
////
}
static {
System.out.println(”dddd”);
}
}
同样,初始化的代码块也可以这样定义。
package com.lizongbo;
public class CoreJavaTest {
static {
System.out.println(”bbb”);
}
static {
System.out.println(”cccc”);
}
{
System.out.println(”eeee”);
}
public CoreJavaTest(){
System.out.println(”init”);
}
public static void main(String[] args) throws Exception {
CoreJavaTest cc=new CoreJavaTest();cc=new CoreJavaTest();
//
}
static {
System.out.println(”dddd”);
}
{
System.out.println(”ffff”);
}
}
执行结果为:
bbb
cccc
dddd
eeee
ffff
init
eeee
ffff
init