简体   繁体   English

初始化未初始化的实例变量时会发生什么?

[英]What really happens when an uninitialized instance variable is initialized?

When I declare an instance variable, such as: 当我声明一个实例变量时,例如:

BSTNode node;

I know that node is now null, since the instance variable is not initialized. 我知道该node现在为空,因为实例变量未初始化。 But is some memory allocated to store the value of null , and is node now a reference to that location? 但是是否分配了一些内存来存储null的值,并且node现在是对该位置的引用吗?

I tried using 我尝试使用

System.out.println(node);

Hoping that I would see the address of the reference, but just saw "null" printed out. 希望我能看到引用的地址,但只看到打印出"null" Why do I not see an address? 为什么看不到地址?

Instance variables are initialized to their default values, if the code doesn't explicitly initialize them. 如果代码未显式初始化实例变量,则实例变量将初始化为其默认值。 All reference variables are initialized to null , per JLS Section 4.12.5 : 根据JLS第4.12.5节 ,所有参考变量均初始化为null

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10): 每个类变量,实例变量或数组组件在创建时都会用默认值初始化(第15.9节,第15.10节):

... ...

For all reference types (§4.3), the default value is null. 对于所有引用类型(第4.3节),默认值为null。

When a null is passed to System.out.println , the string literal "null" is outputted. 当将null传递给System.out.println ,将输出字符串文字"null" The Javadocs for println defer to the print method to cover what happens when a null is passed: 用于println的Javadocs println print方法来说明传递null时发生的情况:

If the argument is null then the string "null" is printed. 如果参数为null,则输出字符串“ null”。

There is no address because there is no object yet. 因为没有对象,所以没有地址。

Yes, memory is allocated and a value assigned no matter the initialization. 是的,无论初始化如何,都会分配内存并分配一个值。

There is no such thing as "uninitialized": there's explicitly initialized, or initialized with the default value. 没有“未初始化”这样的东西:存在显式初始化或使用默认值初始化的东西。

Java has no pointers like C/C++. Java没有像C / C ++这样的指针。 You will never See the memory pointer where you object is located. 您将永远不会看到对象所在的内存指针。 And if you would get it, it might even chance as garbage collection might move it. 而且,如果您能得到它,那么它甚至有可能因为垃圾回收而移动。

If you print an object, it is made a string calling toString(). 如果打印对象,则将其制成调用toString()的字符串。

"Is node now a reference to that location?" node现在是对该位置的引用吗?”

How is null represented in the memory? 内存中的null如何表示?

That is implementation specific, and you won't be able to see the representation of null in a pure Java program. 那是特定于实现的,您将无法在纯Java程序中看到null的表示。 (But null is represented as a zero machine address / pointer in most if not all Java implementations.) (但是,在大多数(如果不是全部)Java实现中,null表示为零机器地址/指针。)

There is already a very detailed answer to this question in Stackoverflow: You should check it out! Stackoverflow中已经对这个问题有非常详细的答案:您应该检查一下!

Also watch this video by Tony Hoare, "Null References: The Billion Dollar Mistake" . 也请观看Tony Hoare的视频“空引用:十亿美元的错误”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 变量未初始化时会发生什么? - What happens when a variable is uninitialized? 即使之前初始化了一个同名的实例变量,当我在方法中再次初始化时会发生什么 - what happens when i initialize again in method even though previously initialized a instance variable with same name Java 中声明的未初始化变量会发生什么? - What happens to a declared, uninitialized variable in Java? 在java中传递对象时会发生什么? - what really happens when passing objects in java? 在Java中加载类时真正发生了什么? - What really happens when loading a class in java? 初始化对象时,实例变量是否始终未初始化? - When Initializing object, instance variable not initialized always? 什么是未初始化的引用变量,它不为null - What is a uninitialized reference variable when it's not null 在Java中,当一个接口“扩展”另一个接口时会发生什么? - In Java, what really happens when an interface “extends” another interface? Java多线程-访问“锁定”对象时真正发生了什么? - Java Multithreading - What Really Happens When Accessing A “Locked” Object? 当我们在rxjava中使用观察者时,真正发生了什么? - what really happens when we are using observer in rxjava?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM