java 对象数组赋null_内存数据是否变化_深入Java关键字null

The null type has one value, the null reference, represented by the literal null,which is formed from ASCII characters. A null literal is always of the null type.

以下是我的一些想法:

package com.zac.expe1;

public class NullInDepth {

public static void helloThere(){

System.out.println("Hello from null?!");

}

public static void main(String[] args) {

//((NullInDepth)null).helloThere();

NullInDepth nullIn = null;

nullIn.helloThere();

Object nullStr = null;

NullInDepth nullOut = (NullInDepth) nullStr;

System.out.println(nullIn);

}

}

这里Hello from null?!会被打印出来,而后边一句会打印出null,我们知道println一般是调用对象的toString来进行打印,但是null明显不是对象,作为一个不确定引用它只是把自己的literal给打印了出来,因此我猜测null是预先存储在内存中的一个引用,它是有值的,可能根据不同的jvm有不同的实现,但是jvm中会一致地将这个值打印为null。

另外按照我的猜测,null应该是栈里边的一个引用,null type是可以转换为所有其他type的引用,所以我也猜测null type应该是所有其他引用类型的子类型。

静态方法(也许需要是public的)应该是由类名来调用,但是也可以使用 类名(null).静态方法名 的方式调用,具体细节没太弄懂。

Since Java and C# run on virtual machines, it does not matter what is used physically to represent null, and it is not necessarily the same across implementations.

What matters is the behaviour of null, as defined in the language specification (see Dan's and MRFerocius' answers for details). Basically, it is a special value that variables of reference type can hold, and which cannot be dereferenced.

BTW, as a reference point, the Java serialization spec use a single byte value 0x70 to represent a null reference.

"null" has the interesting property that "instanceof" used with null always returns false.

When putting a cast before a literal null, remember this is only a syntax to help the compiler choose between overloaded methods. The cast doesn't actually "do anything" to the null reference. Casting doesn't do anything anyway. If it survives in bytecode at all, it only performs a check. Static types only really matter to the compiler (blah blah reflection blah blah bytecode contains some static types blah blah).

再引用另外一篇文章,虽然根本上就错了,但还是有些内容的:

作为关键字true,false,null都分别代表不同的含义。

"

"位"是内存中作为存储数据的基本单位,而我们又通常说,一个字节是8位,也就是 1byte = 8bit。

因为内存中,"位"是使用0和1来表示的,所以作为关键字,true的值在内存中就表示1,false在内存中就是表示0。

但是这里不要和整数(int)中的0和1相比,他们占用的内存空间是不一样的。一个int型的变量,占用的内存空间是4个字节,也就是4 * 8 = 32位,与true和false占用的内存空间是不同的。

而作为关键字null,解释起来就更是麻烦了。

当一个对象被声明时(Object o;),这个对象会以一个整数的字节数,只在内存堆栈(stack)中开辟一个内存指针。例如,我们使用Object o = new Object();实例化一个对象,那么在内存中的运行则是:在内存堆栈(stack)中开辟一个4个字节的指针,然后在内存堆区(heap)开辟这个对象所要存储的数据并初始化,然后在将之前在stack中的内存指针中赋上在heap中开辟的空间的首地址。

而如果Object o;没有进行实例化,则不可能使用o的引用。这时为了方便使用,则出现了null关键字。null关键字的意义也就是一个用来初始化对象的空引用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值