一、变量传值
Java
- 类对象 // 传引用
- 原始数据类型 // 传值
- String自身不带修改函数,每次操作都生成一个String对象。可以认为是传值。
C++
- 基本数据类型变量传值 //传值
- 一般指针传值 // 与int型相似,传值
- 类对象变量传值 // 相当于用某个对象来初使化新建对象
- 类指针对象变量传值 // 与int 类型相似,传值
二、函数传值
Java | 拷贝复制传值 | 基本数据类型:拷贝复制变量的值,不能改变外部的地址和值。 类对象:拷贝复制传递对象的引用,不会改外部的地址,但能能过这个引用,改蛮对象的数据值。 |
C++ | 拷贝复制传值 | 基本数据类型:拷贝复制变量的值,不能改变外部的地址和值。 类对象:拷贝复制传值,在函数内部不能修改传入数据对象的数据。 指针或引用:能在函数内部修改传入对象或变量的数据。不能修改外部的地址,除非在函数内部有新建指针变量并将其返回给输入指针。如:int *set(int *p){return new int(10);} int a = 11;int *pa = &a; pa = set(pa);。这一点JAVA中的引用也是如此。 |
Java 与 C++ 函数参数传值方式均为拷贝复制传值。两者区别只在于传入的东西的“表像”的不一样。
三、JAVA中的引用与C++的区别
JAVA中的引用,对类对象有此说法,对基础类型变量无此说法。
对类对象的引用,例如:设有类Test,则
Test t1 = new Test();
Test t2 = t1;
// t2为t1的一个引用(t1,t2具有相同的地址)
变量t1,t2相当于C/C++中的指针,它们指向同一个内存(具有相同的地址)
C++中,引用与指针是不同的概念。对对象及基础数据类型都有定义,设有类Test,如
Int a = 10;
Int &b = a;//创建a的一个引用。
Test ta = Test();
Test tb = ta;
Test &tc = ta;
// b 为a的一个引用,两者的地址相同。无论引用对象是类对象、数据类型对象还是指针。
顺便说一下
int a = 0;
int b = a;//先创建int型变量b中,在将a的值赋给b。
Test ta = Test();//创建Test 类对象ta
Test tb = ta;// 创建Test类对象,再用ta来初使化tb。故ta与tb的地址不同,数据相同
// 此句与Testtb;tb = ta;等效
Test &tc = tb;// 创建类Test 对象tb的引用 tc。tc 与tb有相同的地址。指向同一片内存空间
Test *pa = new Test();// 创建类指针对象 pa,并默认初使化
Test *pb =pa; // 创建Test类指针对象并将 地址pa赋给pb。
Test *&pc = pa; // 创建 pa的一个引用pc ,从而pc与pa具有相同地址
// 故,pa,pb,pc指向同一内存空间
四、测试代码及运行结果
Java引用,函数传值代码
publicclass JavaCodeTest
{
public JavaCodeTest()
{
// TODO自动生成的构造函数存根
}
public static void main(String[] args)
{
// TODO自动生成的方法存根
Test a = new Test(001, "name001");
Test b = new Test(002, "name002");
print("a: " + a + " ");
a.print();
print("b: " + b + " ");
b.print();
print("Swapping......\n");
swap(a, b);
print("a: " + a + " ");
a.print();
print("b: " + b + " ");
b.print();
print("\n");
}
public static void swap(Test a, Test b)
{
Test tem = new Test();
tem.copy(a);
a.copy(b);
b.copy(tem);
}
public static void print(String str)
{
System.out.print(str);
}
}
classTest extends Object
{
int id;
String name;
Test()
{
}
Test(int id)
{
this.id = id;
}
Test(String name)
{
this.name = name;
}
Test(int id, String name)
{
this.id = id;
this.name = name;
}
Test(String name, int id)
{
this.id = id;
this.name = name;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public void print()
{
System.out.println("id = " + id + ",name = " +name);
}
public void copy(Test obj)
{
this.id = obj.id;
this.name = obj.name;
}
@Override
public Test clone()
{
return (new Test(this.id, this.name));
}
}//输出结果
a:test.TestJava.Test@2a139a55 id = 1,name = name001
b:test.TestJava.Test@15db9742 id = 2,name = name002
Swapping......
a:test.TestJava.Test@2a139a55 id = 2,name = name002
b:test.TestJava.Test@15db9742 id = 1,name = name001
//引用测试
/*
* C++/C引用测试
*/
#include<iostream>
usingnamespace std;
classTest
{
public:
int val;
Test();
Test(int val);
~Test();
int getVal();
void setVal(int val);
};
Test::Test(){this->val= 0;};
Test::Test(intval){this->val = val;};
Test::~Test(){};
intTest::getVal(){return this->val;}
voidTest::setVal(int val){this->val = val;}
intmain()
{
int a = 11;
int b = a;
int &c = a;
cout << "Value:";
cout << "a = "<< a<<" b = "<< b<<" c = "<< c<<endl;
cout << "address:" <<"&a:" << &a <<" &b" << &b<<" &c" <<&c << endl;
Test ta = Test(12);
Test tb = ta;
Test &tc = ta;
Test td = Test();
cout << endl <<"ObjectTest:" << endl;
cout <<"Value:";
cout << "ta.val = "<< ta.val << " tb.val= "
<< tb.val <<" tc.val = " << tc.val<< endl;
cout << "Address:" <<&ta << " "<< &tb << " " << &tc << endl;
cout << "Amendta"<<endl;
ta.setVal(13);
cout << "ta.val = "<< ta.val << " tb.val= "
<< tb.val <<" tc.val = " << tc.val<< endl;
cout << "Address:" <<&ta << " "<< &tb << " " << &tc << endl;
cout << "Amendtb"<<endl;
tb.setVal(15);
cout << "ta.val = "<< ta.val << " tb.val= "
<< tb.val <<" tc.val = " << tc.val<< endl;
cout << "Address:" <<&ta << " "<< &tb << " " << &tc << endl;
cout << "Amendtc"<<endl;
tc.setVal(21);
cout << "ta.val = "<< ta.val << " tb.val= "
<< tb.val <<" tc.val = " << tc.val<< endl;
cout << "Address:" <<&ta << " "<< &tb << " " << &tc << endl;
cout << "Init td with Address:" << &td << endl;
td = ta;
cout << "After td = ta ......tdget Address : " << &td<< endl;
cout << "The val in td :"<< td.val << endl;
cout << endl <<"ObjectPointer Test:" << endl;
Test *pa = new Test(11);
Test *pb = pa;
Test *&pc = pa;
Test *pd = new Test();
cout << "Value: " <<endl;
cout << "pa : " <<pa->val << "\tpb : " << pb->val << "\tpc: " << pc->val << endl;
cout << "Address : "<<endl << pa << " " << pb << " " << pc << " " << endl;
cout << "Amend pa :"<< endl;
pa->setVal(14);
cout << "pa : " <<pa->val << "\tpb : " << pb->val << "\tpc: " << pc->val << endl;
cout << "Amend pb : "<< endl;
pb->setVal(17);
cout << "pa : " <<pa->val << "\tpb : " << pb->val << "\tpc: " << pc->val << endl;
cout << "Amend pc;" << endl;
pc->setVal(18);
cout << "pa : " <<pa->val << "\tpb : " << pb->val << "\tpc: " << pc->val << endl;
cout << "Init pd with Address :" << pd << endl;
pd = pa;
cout << "After : pd = pa......pdget Adress: " << pd << endl;
cout << "The Value of pd :"<< pd->val << endl;
return 0;
}
//输出结果
Value:a =11 b = 11 c = 11
address:&a:0x70fe04 &b0x70fe00 &c0x70fe04
ObjectTest:
Value:ta.val= 12 tb.val = 12 tc.val = 12
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend ta
ta.val =13 tb.val = 12 tc.val = 13
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend tb
ta.val =13 tb.val = 15 tc.val = 13
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend tc
ta.val =21 tb.val = 15 tc.val = 21
Address:0x70fdf0 0x70fde0 0x70fdf0
Init tdwith Address :0x70fdd0
After td= ta ......td get Address : 0x70fdd0
The valin td :21
ObjectPointer Test:
Value:
pa :11 pb :11 pc : 11
Address :
0x1f14a0 0x1f14a0 0x1f14a0
Amend pa:
pa :14 pb :14 pc : 14
Amend pb:
pa :17 pb :17 pc : 17
Amend pc;
pa :18 pb :18 pc : 18
Init pdwith Address : 0x1f14c0
After :pd = pa......pd get Adress: 0x1f14a0
The Valueof pd :18
//输出结果
Value:a =11 b = 11 c = 11
address:&a:0x70fe04 &b0x70fe00 &c0x70fe04
ObjectTest:
Value:ta.val= 12 tb.val = 12 tc.val = 12
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend ta
ta.val =13 tb.val = 12 tc.val = 13
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend tb
ta.val =13 tb.val = 15 tc.val = 13
Address:0x70fdf0 0x70fde0 0x70fdf0
Amend tc
ta.val =21 tb.val = 15 tc.val = 21
Address:0x70fdf0 0x70fde0 0x70fdf0
Init tdwith Address :0x70fdd0
After td= ta ......td get Address : 0x70fdd0
The valin td :21
ObjectPointer Test:
Value:
pa :11 pb :11 pc : 11
Address :
0x1f14a0 0x1f14a0 0x1f14a0
Amend pa:
pa :14 pb :14 pc : 14
Amend pb:
pa :17 pb :17 pc : 17
Amend pc;
pa :18 pb :18 pc : 18
Init pdwith Address : 0x1f14c0
After :pd = pa......pd get Adress: 0x1f14a0
The Valueof pd :18
//返回指针
/*
* return 地址
*/
#include <iostream>
using namespace std;
int *set(int *p)
{
return new int(110);
}
int main()
{
int a = 11;
int *pa = &a;
cout << "pa 的地址是:" << pa << endl;
cout << "*pa = " << *pa << endl << endl;
pa = set(pa);
cout << " set ....." << endl;
cout << "pa 的地址是:" << pa << endl;
cout << "*pa = " << *pa << endl ;
return 0;
}
// 输出结果
pa 的地址是:0x70fe34
*pa = 11
set .....
pa 的地址是:0x7f14a0
*pa = 110