5.6 成员初始化
- 方法的局部变量 未初始化 编译提示
- 类的数据成员 基本类型(boolean char byte short int long float double )未初始化,有初值
Data type Initial value
boolen false
char [] #char 值为0 控制字符 NUL 转义字符\0
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
reference null
5.6.1 指定初始化
类成员变量定义(注意float f = 3.14f)
对象初始化
5.7 构造器初始化
自动初始化,在构造器被调用之前发生
# i 首先置为0,然后是7 所有基本类型和对象引用,自动初始化
public class Counter {
int i;
Counter(){i=7;}
public static void main(String[] args) {
Counter counter = new Counter();
}
}
5.7.1 初始化顺序
即便字段散步于方法定义之间,它们仍会在任何方法(包括构造器)被调用之前得到初始化。
package Chapter5Initial;
import static net.mindview.util.Print.*;
class Window {
Window(int marker) {
print("window(" + marker + ")");
}
}
class House {
Window w1 = new Window(1); //Before constructor
House() {
// Show that we're in the constructor
print("House()");
w3 = new Window(33); // reinitialize w3
}
Window w2 = new Window(2);// After constructor
void f() {
print("f()");
}
Window w3 = new Window(3);// At end
}
public class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f(); // Show that construction is done
}
}
window(1)
window(2)
window(3)
House()
window(33)
f()
package Chapter5Initial;
public class InitialOrderTest {
// 静态字段
public static String staticField = "静态字段";
// 静态代码块
static {
System.out.println(staticField);
System.out.println("静态代码块");
}
// 字段
public String field = "字段";
// 初始化块
{
System.out.println(field);
System.out.println("初始化块");
}
// 构造方法
public InitialOrderTest() {
System.out.println("构造方法");
}
public static void main(String[] args) {
new InitialOrderTest();
System.out.println("");
new InitialOrderTest();
}
}
静态字段
静态代码块
字段
初始化块
构造方法
字段
初始化块
构造方法
5.7.2 静态数据的初始化
初始化顺序:先静态,后 非静态
静态初始化 只在类对象首次加载 进行一次
没有显示使用static关键字,构造方法实际也是静态方法。
5.7.3 显式的静态初始化
static {
}
5.7.4 非静态实例初始化
import static net.mindview.util.Print.print;
class Mug {
Mug(int marker){
print("Mug(" + marker + ")");
}
void f(int marker){
print("f()(" + marker + ")");
}
}
public class Mugs {
// 字段
Mug mug1;
Mug mug2;
// 初始化块,在两个构造方法前执行
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
Mugs(){
print("Mugs()");
}
Mugs(int i){
print("Mugs(int)");
}
public static void main(String[] args) {
print("Inside main()");
new Mugs();
print("new Mugs() completed");
new Mugs(1);
print("new Mugs(1) completed");
}
}
5.8 数组初始化
int[] a1;(推荐)
int a1[];(c++)
赋值 复制引用
public class ArrayNew {
public static void main(String[] args) {
int[] a;
Random rand = new Random();
System.out.println(String.valueOf(rand.nextInt(20)));
System.out.println(String.valueOf(rand.nextInt(20)));
System.out.println(String.valueOf(rand.nextInt(20)));
System.out.println(String.valueOf(rand.nextInt(20)));
a = new int[rand.nextInt(20)];
print("length of a = " + a.length);
print(Arrays.toString(a));
}
}
public class practice16 {
public static void main(String[] args) {
Random random = new Random();
String[] strings = new String[random.nextInt(20)];
for(int i = 0;i< strings.length;i++){
strings[i] = String.valueOf(i);
}
for(int i = 0;i< strings.length;i++){
System.out.println(strings[i]);
}
System.out.println(Arrays.toString(strings));
}
}
public class Practice17 {
Practice17(String arg){
System.out.println(arg);
}
public static void main(String[] args) {
Random random = new Random();
Practice17[] practice17s = new Practice17[random.nextInt(20)];
for (int i = 0; i<practice17s.length;i++){
practice17s[i] = new Practice17(Integer.toString(i));
}
}
}
5.8.1 可变参数列表
class A{}
public class NewVarArgs {
// static void printArray(Object[] args){
// for(Object obj:args)
// System.out.print(obj +" ");
// System.out.println();
//
// }
// public static void main(String[] args) {
// printArray(new Object[]{new Integer(47),new Float(3.14),new Double(11.22)});
// printArray(new Object[]{"one","two","three"});
// printArray(new Object[]{new A(),new A(),new A()}); //Chapter5Initial.A@511d50c0 类名+对象地址
// }
static void printArray(Object...args){
for(Object obj:args)
System.out.print(obj +" ");
System.out.println();
}
public static void main(String[] args) {
printArray(new Integer(47),new Float(3.14),new Double(11.22));
printArray(47,3.14F,11.11);
printArray("one","two","three");
printArray(new A(),new A(),new A());
printArray((Object[])new Integer[]{1,2,3,4});//编译器填充数组
printArray();
}
}
可变参数
public class OptionalTrainlingArguments {
static void f(int required,String... trailing){ //可变参数必须String
System.out.println("required: " + required + "");
for(String s: trailing)
System.out.print(s+" ");
System.out.println();
}
public static void main(String[] args) {
f(1,"one");
f(2,"two","three");
f(0);
}
}
public class VarargType {
static void f(Character... args) {
System.out.println(args.getClass()); //class [Ljava.lang.Character;
System.out.println(" length " + args.length);
}
static void g(int... args) {
System.out.println(args.getClass());
System.out.println(" length " + args.length);
}
public static void main(String[] args) {
f('a');
f();
g(1);
g();
System.out.println("int[] " + new int[0].getClass()); //int[] class [I
}
}
5.9 枚举类型
public enum Spiciness {
NOT,MILD,MEDIUM,HOT,FLAMING //大写,若多单词,下划线隔开
}
public class SimpleEnumUse {
public static void main(String[] args) {
Spiciness howHot = Spiciness.MEDIUM; //创建枚举类型的引用,将其赋值给某个实例
System.out.println(howHot);
}
}
public class EnumOrder {
public static void main(String[] args) {
for (Spiciness s : Spiciness.values()) // static values()方法
System.out.println(s + ".ordinal" + s.ordinal()); // 特定enum常量的声明顺序
}
}
public class Burrito { //n. (墨西哥) 肉馅(或豆馅)玉米粉圆饼
Spiciness degree;
public Burrito(Spiciness degree){this.degree = degree;}
public void describe(){
System.out.print("This burrito is ");
switch (degree){
case NOT:
System.out.println("not spicy at all.");
break;
case MILD:
case MEDIUM:
System.out.println("a little hot.");
break;
case HOT:
case FLAMING:
default:
System.out.println("maybe too hot.");
}
}
public static void main(String[] args) {
Burrito plain = new Burrito(Spiciness.NOT), // 朴素的,plain rice 白饭
greenChile = new Burrito(Spiciness.MEDIUM), //青椒 绿色智利
jalapeno = new Burrito(Spiciness.HOT); //墨西哥胡椒
plain.describe();
greenChile.describe();
jalapeno.describe();
}
}
This burrito is not spicy at all.
This burrito is a little hot.
This burrito is maybe too hot.
public class Practice21 {
static void describe(Money money){ // 静态方法
switch (money){
case FEN:
System.out.println(money + " is Fen.");
break;
case JIAO:
System.out.println(money + " is JIAO.");
break;
case YUAN:
System.out.println(money + " is YUAN.");
break;
case TENYUAN:
System.out.println(money + " is TENYUAN.");
break;
case HUNDRED:
System.out.println(money + " is HUNDRED.");
break;
case THOUSAND:
System.out.println(money + " is THOUSAND.");
break;
}
}
public static void main(String[] args) {
for(Money money: Money.values()) {
System.out.println(money + ".value: " + money.ordinal());
describe(money);
}
}
}