Java has only nine types, and they are all primitive types:
boolean - need I explain?
byte - signed 8-bit integer
char - unsigned 16-bit integer
short - signed 16-bit integer
int - signed 32-bit integer
long - signed 64-bit integer
float - 32-bit float
double - 64-bit float
reference - equivalent to C++ pointer except that they can only refer to null or valid object instances
Classes are definitions for instantiating objects, and objects can only be handled through references. Since references are primitive types and not objects you cannot have references to references.
All types in Java are passed by value to methods.
References are passed by value.
Let me clarify: to change
what a reference refers to means to set it to refer to null, or to set it to refer to a valid object of they type it references; to change
the object being referenced means that the object being referenced is a mutable type with methods to change its state or public member variables.
1 2 3 4
|
Cow ref; //a variable of type reference, which can only reference null or a Cow
ref = null; //changing what the reference refers to
ref = new Cow(); //changing what the reference refers to
ref.setMilk(8); //changing the object being referenced
|
References are passed by value to methods, therefore when you change
what a reference refers to you are changing the local method copy of the reference.
The object being referenced is not affected in this case, nor is the copy of the reference variable outside of the method in the calling code.