Building
. Thus,
house
is a
variable that can refer to an object, but it is not an object, itself. The next line creates a new
Building
object and assigns a reference to it to
house
. Now,
house
is linked with an object.
The fact that class objects are accessed through a reference explains why classes are
called
reference types.
The key difference between value types and reference types is what
a variable of each type means. For a value type variable, the variable, itself, contains the
value. For example, given
int x;
x = 10;
x
contains the value 10 because
x
is a variable of type
int
, which is a value type. However, in
the case of
Building house = new Building();
house
does not, itself, contain the object. Instead, it contains a reference to the object.
Do'stlaringiz bilan baham: |