初始化引用的位置java_java中引用初始化以及null的用法

1. Car car;

只是声明 car 是 Car 类 的一个引用,但尚未被初始化(即,首次赋值)

2. Car car = null;

声明 car 是 Car 类 的一个引用,并且以无名类的引用 null,被初始化(即,首次赋值)。

注:关于 null 的诠释,请看下文。

类内变量,即一个变量作为类成员使用的时候,声明的时候可以不赋值。因为在调用构造方法创建实体的时候,可予以赋值。如果还没有被初始化,java会为其自动分配默认值如下:

Boolean false

Char '/u0000'(null)

byte (byte)0

short (short)0

int 0

long 0L

float 0.0f

double 0.0d

对象的引用 则分配空值 null。

注意:赋空值,如 Car car = null , 也是赋值,也是初始化的一种。

如果一个变量是在一个方法中定义的,java不会给其分配默认值。如果,在以后的代码执行中,需要用到这个变量的数值,就必须由程序员负责为它初始化,否则将得到编译错误的提示,即无法通过编译。可想而知,当调用这个方法的时候,如果这个变量的值尚未确定,待到用它的时候,就不知所措了。不过,在以后的代码执行中,如果用不到它,编译是可以通过的。

null 还有一个用处。如果往后不再使用某个对象,就可以将这个对象的引用赋予 null 值。这样,java 的 垃圾回收器(Garbage collector) 就可以将这个对象的内存,交还给操作系统。

下列代码,具体解释 "Car car = null;" 的意义。(代码中的注释序号,对应以下解释的序号)。

用 一个"可以成为任何引用类型的特殊符号" null,将一个无名类的引用值(指空), 赋予 Car 类型的引用。此刻,虽然尚未调用构造方法创建 Car 类型的实体,但此时指空的引用 car 已经认定是 Car类型的 引用。

众所周知,在编译期间静态方法的行为就已经确定。代码运行之前, 编译好的字节码文档(class文件) 就被 ClassLoader 载入(load) JVM 的 stack,所以,该静态方法(slogan())即可被调用。因此,即便 Car 类型的引用 car 是空值,仍然可以调用 Car 类型的 静态方法 slogan()。

之后,调用 Car 类型的构造方法,在堆里(heap)建立了Car 类型的实体,其引用就是指向这个实体的引用 car。

此时调用 Car 类型的成员方法 toString(),就能输出这个实体的具体信息。

class Car{

String brand;

int price;

public Car(String s, int n){

brand=s;

price=n;

}

static void slogan() {

String s = "领导时代,驾驭未来。路有我!";

System.out.println(s);

}

public String toString(){

String s ="";

s += "商标: " + brand;

s += ", 价格:" + price;

return s;

}

}

public class ND{

public static void main(String[] args){

Car car= null; // 1.

car.slogan(); // 2.

car = new Car("奔驰SUV", 200000); // 3.

System.out.println(car); // 4. 调用 Car 类型的成员方法 toString()

}

}

输出:

领导时代,驾驭未来。路有我!

商标: 奔驰SUV, 价格:200000

4.1. The Kinds of Types and Values

4.1  类型和值的种类

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

Java 语言中 有两种类型: 基本类型 (§4.2) 和 引用类型 (§4.3)。相应地,就有两种数据数值,它们能作为 参数,作为方法的返回值,以及 作为表达式的算子被计算,以变量形式被保存。

Type:

PrimitiveType

ReferenceType

数据类型:

基本类型

引用类型

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

还有一个特殊的 null 类型,也就是没有名字的 表达式 null (§3.10.7, §15.8.1) 的类型。

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

因为 null 类型 没有名字,所以不可能声明为 null 类型的变量,或者转换为 null 类型。

The null reference is the only possible value of an expression of null type.

null 引用 是 null 类型表达式 的 唯一可能的值。

The null reference can always undergo a widening reference conversion to any reference type.

null 引用 可以转换为任意引用类型。

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

实际上,程序员可以不顾及 null 类型,仅认为 null 仅仅是一个可以成为任何引用类型的特殊符号就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值