java 显式初始化_初始化、显式初始化、隐式初始化。这几个区别是什么?

简单整理一下。

C(ISO/IEC 9899:201x)

5.1.2

1 All objects with static storage duration shall be initialized (set to their

initial values) before program startup.

这大概是C标准中对初始化一词的解释。

6.2.43 An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.事实上由于这些对象的生存期贯穿整个程序,初始化就只能进行一次,原因望文生义就好。(不过似乎也没哪种对象能初始化多次)

6.7.9 初始化的相关内容

8 An initializer specifies the initial value stored in an object.又强调一遍。

10 If an object that has automatic storage duration is not initialized explicitly,似乎没有指明什么叫explicitly initialize,姑且理解为“定义处没有带有初始化器”吧。

10 If an object that has static or thread storage duration is not initialized

explicitly, then:— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules,

and any padding is initialized to zero bits;

— if it is a union, the first named member is initialized (recursively) according to these

rules, and any padding is initialized to zero bits;这也许就是所谓“隐式初始化”干的活。。至于“初始化为0”就是无稽之谈了。谁也没保证空指针二进制布局全为0。(然而标准的另一个地方似乎有些微妙地否定了我这种说法)

此外,赋值和初始化没有一毛钱的关系。单纯是长得像。

下面是C++(N4296)。相比来讲C++的初始化要更杂乱也更精细一些,但是它居然分在若干个章节里。。

3.6.2

2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5)

before any other initialization takes place.这说明一次完整的初始化过程可能会发生多次初始化。。

同时这一节定义了若干术语,比如constant initialization,static initialization,dynamic initialization。

3.8

1 The lifetime of an object of type T begins when:

— storage with the proper alignment and size for type T is obtained, and

— if the object has non-vacuous initialization, its initialization is complete.这指明了(non-vacuous)初始化的意义:它开启了对象的生存期。此外,也说明了初始化并不是确定对象所占的存储。

6.7

2 Variables with automatic storage duration (3.7.3) are initialized each time theirdeclaration-statementis

executed.注意variable在C++中是有明确定义的术语。(C中则没有)

8.5

1 A declarator can specify an initial value for the identifier being declared.这个说法和C中的差不多。

这里也定义了若干类型的初始化:zero-initialize default-initialize value-initialize direct-initialize。此节很长,把大多数的初始化都包括了进去。

同样的,这里也使用到了explicitly initialize却没有定义。只好同样理解成“具有初始化器的初始化”了。

就这样。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值