Home > Java > Javagetting Started > body text

What does a reference in java mean?

王林
Release: 2019-11-20 16:48:34
Original
7747 people have browsed it

What does a reference in java mean?

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.

What does a reference in java mean?

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template