Types in java include basic data types and composite types.
Basic data types: basic data types such as int, char, etc.;
Composite types: pointers and references;
Reference: Give the object another name, reference type reference Another type.
A reference is not an object. On the contrary, it is just another name for an existing object. As shown in Figure 1-1.
References must be initialized
After defining a reference, all operations performed on it are bound to it performed on the object.
Assigning a value to a reference
Actually assigns the value to the object bound to the reference;
Gets the value of the reference
actually obtains the value of the object bound to the reference;
uses the reference object as the initial value
actually obtains the value of the object bound to the reference. The reference to the bound object is used as the initial value.
References can only be bound to objects and cannot be bound to literal values or the calculation results of an expression
The basics of references The data type must be the same as the basic data type of the referenced object
As shown in the following code:
#include <iostream> using namespace std; int main() { int a = 10; //int &r; //错误的,引用必须初始化。 int &ra = a; //ra与变量a绑定 ra = 20; //为引用赋值,实际上是a = 10 cout<<"a = "<<a<<endl; cout<<"ra = "<<ra<<endl; //获取引用的值,实际上是输出a的值 int b = ra; //以引用对象作为初始值,即b = a; cout<<"b = "<<b<<endl; //int &rc = 10; //错误的,引用不能直接与字面值绑定在一起 double da = 3.14; //int &rb = da; //错误的,引用的类型必须与引用对象的类型一致 return 0; }
Recommended tutorial:Getting started with java development
The above is the detailed content of What does a reference in java mean?. For more information, please follow other related articles on the PHP Chinese website!