java initial_【java】initial | 学步园

构造器初始化:

先置0,后赋值

在类的内部,定义变量的先后顺序决定了初始化的顺序

package mete.data;

class Window {

Window(int marker) { System.out.println("Window(" + marker + ")"); }

}

class House {

Window w1 = new Window(1);

House() {

System.out.println("House()");

w3 = new Window(33);

}

Window w2 = new Window(2);

void f() { System.out.println("f()"); }

Window w3 = new Window(3);

}

public class OrderofInitialization {

public static void main(String[] args) {

House h = new House();

h.f();

}

}

output:

66bd52d0c69dc9e7595283165b5b7a75.png

静态数据的初始化:

无论创建多少个对象,静态数据都只占用一份存储区域。static关键字不能应用于局部变量,因此它只能作用于域。如果是基本类型,默认置为标准初值,如果是对象,置为null。

package mete.data;

class Bowl {

Bowl(int marker) {

System.out.println("Bow1(" + marker + ")");

}

void f1(int marker) {

System.out.println("f1(" + marker + ")");

}

}

class Table {

static Bowl bowl1 = new Bowl(1);

Table() {

System.out.println("Table()");

bowl2.f1(1);

}

void f2(int marker) {

System.out.println("f2(" + marker + ")");

}

static Bowl bowl2 = new Bowl(2);

}

class Cupboard {

Bowl bowl3 = new Bowl(3);

static Bowl bowl4 = new Bowl(4);

Cupboard() {

System.out.println("Cupboard()");

bowl4.f1(2);

}

void f3(int marker) {

System.out.println("f3(" + marker + ")");

}

}

public class StaticInitialization {

public static void main(String[] args) {

System.out.println("Creating new Cupboard in main.");

new Cupboard();

System.out.println("Creating new Cupboard in main.");

new Cupboard();

table.f2(1);

cupboard.f3(1);

}

static Table table = new Table();

static Cupboard cupboard = new Cupboard();

}

output:

f2d2b8c102f95f0906b083fbc5c15fdf.png

数组初始化:

数组只是相同类型的、用一个标识符名称封装到一起的一个对象序列或基本类型数据序列,数组是通过方括号下标运算符来定义和使用的。

编译器不允许指定数组的大小,现在拥有的只是对数组的一个引用,而且也没有给数组对象本身分配任何空间。

在java中,可以将一个数组复制给另外一个数组,其实真正做的是复制了一个引用。

创建数组:

package mete.data;

import java.util.Arrays;

import java.util.Random;

public class ArrayNew {

public static void main(String[] args) {

int[] a;

Random rand = new Random(47);

a = new int[rand.nextInt(20)];

System.out.println("length of a is " + a.length);

System.out.println(Arrays.toString(a));

//Arrays.toString 方法属于java.util标准类库,它将产生一位数组的可打印版本。

}

}

output:

398afa773bfbae59c638a24a178e9b9e.png

如果你创建了一个非基本类型的数组,那么你就创建了一个引用数组。

用花括号来创建数组(对比c语言)

package mete.data;

import java.util.Arrays;

public class ArrayInt {

public static void main(String[] args) {

Integer[] a = {1, 2, 3};

System.out.println(Arrays.toString(a));

}

}

output:

d6d07b43f34d5a15eb487f0f8f8d1143.png

package mete.data;

import java.util.Arrays;

import java.util.Random;

public class ArrayClassObj {

public static void main(String[] args) {

Random rand = new Random(47);

Integer[] a = new Integer[rand.nextInt(20)];

System.out.println("length of a is " + a.length);

for(int i = 0 ; i < a.length; i++ )

a[i] = rand.nextInt(500);

System.out.println(Arrays.toString(a));

}

}

output:

8a6ba019f3015ba2c8c63626cabc84ef.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值