I have been studying Java for a few months and am now starting to learn C.
我已经学习Java几个月了,现在开始学习C语言。
I am a little confused, I was under the impression that passing an object by reference and passing a pointer to that object were the same thing: I thought the difference was that in Java all passing of objects is done with pointers automatically, where as in C one has to actually sprinkle little asterisks and ampersands here and there. Recently, in conversation, I was assured that there was a difference!
我有点困惑,我印象中通过引用和指针传递一个对象,对象是同一件事:我想所不同的是,在Java中所有的对象都是用指针自动,而实际上在C语言中一个撒小星号和与符号。最近,在谈话中,我确信这是不同的!
What is the difference between passing by reference and passing a pointer?
通过引用传递和传递指针之间的区别是什么?
10 个解决方案
#1
39
Neither Java nor C has pass-by-reference. They are both strictly pass-by-value.
Java和C都没有引用传递。它们都是严格按价值传递的。
Pass-by-reference semantics mean that when you change the value of the parameter in the method, the caller will see that change in the argument.
引用传递语义意味着,当您更改方法中参数的值时,调用者将在参数中看到该更改。
Now, you may be thinking: "But that's the case in Java! If I change an object during the method, the caller sees that change." The object isn't the parameter. The parameter is just the variable - and if you change the value of that variable, the caller won't see that. For example:
现在,您可能会想:“但在Java中就是这样!”如果我在方法中改变一个对象,调用者会看到这个变化。对象不是参数。参数就是变量,如果你改变变量的值,调用者就不会看到这个。例如:
public void foo(Object x)
{
x = null;
}
public void caller()
{
Object y = new Object();
foo(y);
// y is still not null!
}
If the parameter were really passed by reference, y
would be null afterwards. Instead, the value of y
is just a reference, and that reference is passed by value. It's confusing because the word "reference" is in both terms, but they're different things.
如果参数确实是通过引用传递的,那么之后y将为空。相反,y的值只是一个引用,该引用通过值传递。这很令人困惑,因为“引用”这个词在两个词中都有,但它们是不同的东西。
You may want to look at my article about C# parameter passing to see what would be possible if Java did have pass-by-reference semantics, as C# does when (and only when) you use the ref
keyword.
您可能想看看我关于c#参数传递的文章,看看如果Java确实具有引用传递语义,会有什么可能,就像c#在使用ref关键字时(且仅在使用时)所做的那样。
You may also want to look at the comments to my Stack Overflow answer related to the topic.
您可能还想查看与主题相关的堆栈溢出答案的注释。
#2
7
I thought the difference was that in Java all passing of objects is done with pointers automatically, where as in C one has to actually sprinkle little asterisks and ampersands here and there.
我认为不同之处在于,在Java中,所有对象的传递都是通过指针自动完成的,就像在C语言中一样,在这里和那里都要实际地在上面撒一些星号和符号。
Conceptional, that's quite right. If we are pedantic (and that's a good thing), we can even say objects are not passed at all in Java. What is passed is only ever the "pointer", which in Java is called the reference. All indirection is done automatically. So when you do "objref->foo" instead of "objref.foo" in C and can't use the dot because you work with a pointer, in Java you can still use the dot because it doesn't know anything else to access members anyway. In C, you can pass the pointer (and here, it is actually called pointer) and you can pass the object itself, in which case a copy is passed. In C, you access the object that a pointer refers to by indirection using the star or "->". In Java, the only way objects are accessed anyway are using the dot (objref.member).
概念上的,这是相当正确的。如果我们是pedantic(这是一件好事),我们甚至可以说,在Java中根本没有传递对象。传递的只是“指针”,在Java中称为引用。所有间接操作都是自动完成的。所以当你用"objref->foo"而不是"objref "在C中,不能使用点,因为你使用一个指针,在Java中,你仍然可以使用点,因为它不知道任何其他的访问成员。在C中,你可以传递指针(在这里,它实际上被称为指针),你可以传递对象本身,在这种情况下,会传递一个副本。在C语言中,使用星号或“->”间接访问指针所指的对象。在Java中,访问对象的唯一方式是使用dot (object .member)。
If we are pedantic again (now more important again), neither in Java nor in C there is "pass by reference". What we pass in Java is the reference/pointer to the object and in C we either pass a copy of the object (obvious case) or we pass again just a copy of a pointer to the object. So in both cases - Java and C - what we pass are the addresses of the objects. Not talking about primitive java types, which are copied in both Java and C - even though you can pass their address in C too, there is no difference between an aggregate type (i.e a struct) and a "primitive type" in C in that regard.
如果我们再次使用pedantic(现在更重要的是),那么在Java和C中都不存在“通过引用传递”。我们在Java中传递的是指向对象的引用/指针,在C中,我们要么传递对象的副本(明显的例子),要么我们再次传递一个指向对象的指针的副本。在这两种情况下——Java和C——我们传递的是对象的地址。不要讨论在java和C中复制的原始java类型——即使您也可以用C传递它们的地址,聚合类型(i)之间也没有区别。e a struct)和C中的“原始类型”。
#3
3
The differences are subtle. At an assembly level, in both cases the address of the object is passed to the function. However, in the pointer case the parameter is an independent pointer which can be used to modify the target object (*pch = 'x') or can be used to access a different object (pch = "newstr"). In the reference case, the parameter is automatically indirected to the target object.
这些差异并不明显。在程序集级别,在这两种情况下,对象的地址都传递给函数。但是,在指针情况下,参数是一个独立的指针,可以用来修改目标对象(*pch = 'x'),也可以用来访问不同的对象(pch = "newstr")。在引用的情况下,该参数将自动被定向到目标对象。
#4
2
I believe a pointer is a variable that contains a reference. With & you can obtain a memory direction ( reference ), and with * you can access to the content of a memory direction. I suggest the book The C programming Language .
我认为指针是一个包含引用的变量。使用&可以获取内存方向(引用),使用*可以访问内存方向的内容。我推荐这本书的C编程语言。
Regards
问候
#5
2
True pass-by-reference means that you can assign a new value to the reference, and this new value will be visible in the calling context just like inside called method.
真正的引用传递意味着您可以为引用分配一个新值,这个新值将在调用上下文中可见,就像在调用方法内部一样。
In Java, this is not possible, because object references are passed by value.
在Java中,这是不可能的,因为对象引用是按值传递的。
#6
2
A few things
几件事
- C has no references (in spite of C++)
- C没有引用(尽管c++)
- Java has no pointers
- Java没有指针
- The difference between pointers and references is, that pointers are practically memory addresses you can calculate with. References can not be calculated with
- 指针和引用的区别在于,指针实际上是可以计算的内存地址。引用无法计算
- Java and C pass by value (When passing an object in Java, the reference is copied to the function)
- Java和C通过值传递(在Java中传递对象时,引用被复制到函数中)
#7
1
If you are studying C (rather than C++), then there are no references.
如果您正在学习C(而不是c++),那么没有引用。
The term "pass by reference" in C simply means that you are passing a pointer as the address in which the value will be stored, so that the function can change its value. Otherwise, you're passing by value which means that a copy of the variable is generated on the stack and modifications have no impact.
在C中“通过引用传递引用”一词仅仅意味着您正在传递一个指针,作为将值存储在其中的地址,以便函数可以更改其值。否则,您将传递值,这意味着将在堆栈上生成变量的副本,而修改没有影响。
In many ways this is similar to what you see in Java, except that in Java you don't have to explicitly turn things into a pointer or dereference them. In other words, when you pass a pointer, the address is copied on the stack, so if your function changes the pointer (rather than the data it points to), the changes disappear when you are done with the function. Similarly, when you pass an object reference in Java, you can change the contents of the object (e.g., by calling functions), but changing the varialbe to point at anothr object will have no effect once you leave.
在许多方面,这与您在Java中看到的类似,但在Java中,您不必显式地将事物转换为指针或取消引用。换句话说,当您传递一个指针时,地址将被复制到堆栈上,因此如果您的函数改变指针(而不是它指向的数据),那么当您完成这个函数时,更改将消失。类似地,当您在Java中传递对象引用时,您可以更改对象的内容(例如,通过调用函数),但是一旦您离开,将变量更改为指向anothr对象的值将不起作用。
If you were using C++ (which looks like C), then you can pass by reference, in which case you don't need to deal with pointers, and changes to the variable in the function actually change the external value directly (except that you can't make it point at something else).
如果您使用的是c++(看起来像C),那么您可以通过引用进行传递,在这种情况下,您不需要处理指针,并且对函数中的变量的更改实际上直接更改了外部值(除了您不能让它指向其他东西)。
#8
1
There's a recent article by Eric Lippert about this. He's a programmer on the C# compiler, but whatever he says there has enough generality to be extended to other GC languages such as Java:
Eric Lippert最近发表了一篇关于这一点的文章。他是c#编译器的程序员,但是不管他说什么,有足够的通用性可以扩展到其他GC语言,比如Java:
http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
Quote from the article:
引用这篇文章:
Pointers are strictly "more powerful" than references; anything you can do with references you can do with pointers. [...]
指针绝对比引用“更强大”;你能做的任何事都可以用指针来做。[…]
Pointers are typically implemented as addresses. An address is a number which is an offset into the "array of bytes" that is the entire virtual address space of the process. [...]
指针通常被实现为地址。地址是一个数字,它是“字节数组”的偏移量,这是进程的整个虚拟地址空间。[…]
For all these reasons we do not describe references as addresses in the specification. The spec just says that a variable of reference type "stores a reference" to an object, and leaves it completely vague as to how that might be implemented. Similarly, a pointer variable stores "the address" of an object, which again, is left pretty vague. Nowhere do we say that references are the same as addresses.
由于所有这些原因,我们不将引用描述为规范中的地址。规范只是说引用类型的变量“存储”对象的引用,而对于如何实现引用,则完全不明确。类似地,一个指针变量存储一个对象的“地址”,同样地,这个地址也很模糊。我们没有说过引用和地址是一样的。
So, in C# a reference is some vague thing that lets you reference an object. You cannot do anything with a reference except dereference it, and compare it with another reference for equality. And in C# a pointer is identified as an address.
所以,在c#中,引用是一种模糊的东西,可以让你引用一个对象。除了删除引用之外,您不能对引用做任何事情,并将它与另一个引用进行比较以获得相等性。在c#中,指针被标识为一个地址。
By contrast with a reference, you can do much more with a pointer that contains an address. Addresses can be manipulated mathematically; you can subtract one from another, you can add integers to them, and so on. Their legal operations indicate that they are "fancy numbers" that index into the "array" that is the virtual address space of the process.
与引用相比,您可以使用包含地址的指针做更多的事情。地址可以用数学方法处理;你可以从另一个中减去一个,你可以给它们加上整数,等等。它们的合法操作表明,它们是“花哨的数字”,索引到“数组”,即流程的虚拟地址空间。
#9
1
Those readers familiar with C/C++ have probably noticed that object references appear to be similar to pointers. This suspicion is, essentially, correct. An object reference is similar to a memory pointer. The main difference—and the key to Java’s safety—is that you cannot manipulate references as you can actual pointers. Thus, you cannot cause an object reference to point to an arbitrary memory location or manipulate it like an integer.
熟悉C/ c++的读者可能已经注意到对象引用似乎与指针相似。这种怀疑基本上是正确的。对象引用类似于内存指针。主要的不同之处——以及Java安全的关键——是您不能操纵引用,因为您可以实际的指针。因此,不能使对象引用指向任意内存位置,也不能像处理整数那样操作它。
- java not supported & operator then how can u get object address in java.
- 不支持java和操作符,如何在java中获取对象地址。
- In java reference is done by object for example
- 例如,在java引用中是由对象完成的
MyClass object1 = new MyClass();
MyClass object1 =新MyClass();
MyClass object2 = object1;
MyClass object2 =中的object1;
i.e. you might think that object1 and object2 refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, object1 and object2 will both refer to the same object. if you change in any one effect other.
例如,您可能认为object1和object2指的是独立的、不同的对象。然而,这是错误的。相反,在执行此片段之后,object1和object2都将引用相同的对象。如果你改变了一个效果,另一个效果。
*you cann't change the address of object after initialize i.e MyClass obj =new MyClass(); as reference in c++ , with object you can change only object instance value
*你不能在初始化i后更改对象的地址。e MyClass obj =new MyClass();在c++中,使用对象可以只更改对象实例值。
Note: java provide a special feature object1 has been set to null, but object2 still points to the original object.
注意:java提供了一个特殊的特性object1被设置为null,但是object2仍然指向原始对象。
#10
0
i m not sure about java but in C# or VB.net you can pass by value or by refrence
我不确定java,但是在c#或VB.net中,你可以通过值或折射传递
example passing by refrence
例子经过具有一定
static void Main(string[] args)
{
int x = 1;
Foo(ref x);
Console.WriteLine(x.ToString());//this will print 2
}
private static void Foo(ref int x)
{
x++;
}
notice that x is equal to 1 but when you call the method Foo and passing x by refrence when x is increased by one in Foo the original value is increased too
注意,x = 1,但当你调用方法Foo,当x在Foo中增加1时,它通过x,它的原始值也增加了。
#1
39
Neither Java nor C has pass-by-reference. They are both strictly pass-by-value.
Java和C都没有引用传递。它们都是严格按价值传递的。
Pass-by-reference semantics mean that when you change the value of the parameter in the method, the caller will see that change in the argument.
引用传递语义意味着,当您更改方法中参数的值时,调用者将在参数中看到该更改。
Now, you may be thinking: "But that's the case in Java! If I change an object during the method, the caller sees that change." The object isn't the parameter. The parameter is just the variable - and if you change the value of that variable, the caller won't see that. For example:
现在,您可能会想:“但在Java中就是这样!”如果我在方法中改变一个对象,调用者会看到这个变化。对象不是参数。参数就是变量,如果你改变变量的值,调用者就不会看到这个。例如:
public void foo(Object x)
{
x = null;
}
public void caller()
{
Object y = new Object();
foo(y);
// y is still not null!
}
If the parameter were really passed by reference, y
would be null afterwards. Instead, the value of y
is just a reference, and that reference is passed by value. It's confusing because the word "reference" is in both terms, but they're different things.
如果参数确实是通过引用传递的,那么之后y将为空。相反,y的值只是一个引用,该引用通过值传递。这很令人困惑,因为“引用”这个词在两个词中都有,但它们是不同的东西。
You may want to look at my article about C# parameter passing to see what would be possible if Java did have pass-by-reference semantics, as C# does when (and only when) you use the ref
keyword.
您可能想看看我关于c#参数传递的文章,看看如果Java确实具有引用传递语义,会有什么可能,就像c#在使用ref关键字时(且仅在使用时)所做的那样。
You may also want to look at the comments to my Stack Overflow answer related to the topic.
您可能还想查看与主题相关的堆栈溢出答案的注释。
#2
7
I thought the difference was that in Java all passing of objects is done with pointers automatically, where as in C one has to actually sprinkle little asterisks and ampersands here and there.
我认为不同之处在于,在Java中,所有对象的传递都是通过指针自动完成的,就像在C语言中一样,在这里和那里都要实际地在上面撒一些星号和符号。
Conceptional, that's quite right. If we are pedantic (and that's a good thing), we can even say objects are not passed at all in Java. What is passed is only ever the "pointer", which in Java is called the reference. All indirection is done automatically. So when you do "objref->foo" instead of "objref.foo" in C and can't use the dot because you work with a pointer, in Java you can still use the dot because it doesn't know anything else to access members anyway. In C, you can pass the pointer (and here, it is actually called pointer) and you can pass the object itself, in which case a copy is passed. In C, you access the object that a pointer refers to by indirection using the star or "->". In Java, the only way objects are accessed anyway are using the dot (objref.member).
概念上的,这是相当正确的。如果我们是pedantic(这是一件好事),我们甚至可以说,在Java中根本没有传递对象。传递的只是“指针”,在Java中称为引用。所有间接操作都是自动完成的。所以当你用"objref->foo"而不是"objref "在C中,不能使用点,因为你使用一个指针,在Java中,你仍然可以使用点,因为它不知道任何其他的访问成员。在C中,你可以传递指针(在这里,它实际上被称为指针),你可以传递对象本身,在这种情况下,会传递一个副本。在C语言中,使用星号或“->”间接访问指针所指的对象。在Java中,访问对象的唯一方式是使用dot (object .member)。
If we are pedantic again (now more important again), neither in Java nor in C there is "pass by reference". What we pass in Java is the reference/pointer to the object and in C we either pass a copy of the object (obvious case) or we pass again just a copy of a pointer to the object. So in both cases - Java and C - what we pass are the addresses of the objects. Not talking about primitive java types, which are copied in both Java and C - even though you can pass their address in C too, there is no difference between an aggregate type (i.e a struct) and a "primitive type" in C in that regard.
如果我们再次使用pedantic(现在更重要的是),那么在Java和C中都不存在“通过引用传递”。我们在Java中传递的是指向对象的引用/指针,在C中,我们要么传递对象的副本(明显的例子),要么我们再次传递一个指向对象的指针的副本。在这两种情况下——Java和C——我们传递的是对象的地址。不要讨论在java和C中复制的原始java类型——即使您也可以用C传递它们的地址,聚合类型(i)之间也没有区别。e a struct)和C中的“原始类型”。
#3
3
The differences are subtle. At an assembly level, in both cases the address of the object is passed to the function. However, in the pointer case the parameter is an independent pointer which can be used to modify the target object (*pch = 'x') or can be used to access a different object (pch = "newstr"). In the reference case, the parameter is automatically indirected to the target object.
这些差异并不明显。在程序集级别,在这两种情况下,对象的地址都传递给函数。但是,在指针情况下,参数是一个独立的指针,可以用来修改目标对象(*pch = 'x'),也可以用来访问不同的对象(pch = "newstr")。在引用的情况下,该参数将自动被定向到目标对象。
#4
2
I believe a pointer is a variable that contains a reference. With & you can obtain a memory direction ( reference ), and with * you can access to the content of a memory direction. I suggest the book The C programming Language .
我认为指针是一个包含引用的变量。使用&可以获取内存方向(引用),使用*可以访问内存方向的内容。我推荐这本书的C编程语言。
Regards
问候
#5
2
True pass-by-reference means that you can assign a new value to the reference, and this new value will be visible in the calling context just like inside called method.
真正的引用传递意味着您可以为引用分配一个新值,这个新值将在调用上下文中可见,就像在调用方法内部一样。
In Java, this is not possible, because object references are passed by value.
在Java中,这是不可能的,因为对象引用是按值传递的。
#6
2
A few things
几件事
- C has no references (in spite of C++)
- C没有引用(尽管c++)
- Java has no pointers
- Java没有指针
- The difference between pointers and references is, that pointers are practically memory addresses you can calculate with. References can not be calculated with
- 指针和引用的区别在于,指针实际上是可以计算的内存地址。引用无法计算
- Java and C pass by value (When passing an object in Java, the reference is copied to the function)
- Java和C通过值传递(在Java中传递对象时,引用被复制到函数中)
#7
1
If you are studying C (rather than C++), then there are no references.
如果您正在学习C(而不是c++),那么没有引用。
The term "pass by reference" in C simply means that you are passing a pointer as the address in which the value will be stored, so that the function can change its value. Otherwise, you're passing by value which means that a copy of the variable is generated on the stack and modifications have no impact.
在C中“通过引用传递引用”一词仅仅意味着您正在传递一个指针,作为将值存储在其中的地址,以便函数可以更改其值。否则,您将传递值,这意味着将在堆栈上生成变量的副本,而修改没有影响。
In many ways this is similar to what you see in Java, except that in Java you don't have to explicitly turn things into a pointer or dereference them. In other words, when you pass a pointer, the address is copied on the stack, so if your function changes the pointer (rather than the data it points to), the changes disappear when you are done with the function. Similarly, when you pass an object reference in Java, you can change the contents of the object (e.g., by calling functions), but changing the varialbe to point at anothr object will have no effect once you leave.
在许多方面,这与您在Java中看到的类似,但在Java中,您不必显式地将事物转换为指针或取消引用。换句话说,当您传递一个指针时,地址将被复制到堆栈上,因此如果您的函数改变指针(而不是它指向的数据),那么当您完成这个函数时,更改将消失。类似地,当您在Java中传递对象引用时,您可以更改对象的内容(例如,通过调用函数),但是一旦您离开,将变量更改为指向anothr对象的值将不起作用。
If you were using C++ (which looks like C), then you can pass by reference, in which case you don't need to deal with pointers, and changes to the variable in the function actually change the external value directly (except that you can't make it point at something else).
如果您使用的是c++(看起来像C),那么您可以通过引用进行传递,在这种情况下,您不需要处理指针,并且对函数中的变量的更改实际上直接更改了外部值(除了您不能让它指向其他东西)。
#8
1
There's a recent article by Eric Lippert about this. He's a programmer on the C# compiler, but whatever he says there has enough generality to be extended to other GC languages such as Java:
Eric Lippert最近发表了一篇关于这一点的文章。他是c#编译器的程序员,但是不管他说什么,有足够的通用性可以扩展到其他GC语言,比如Java:
http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
Quote from the article:
引用这篇文章:
Pointers are strictly "more powerful" than references; anything you can do with references you can do with pointers. [...]
指针绝对比引用“更强大”;你能做的任何事都可以用指针来做。[…]
Pointers are typically implemented as addresses. An address is a number which is an offset into the "array of bytes" that is the entire virtual address space of the process. [...]
指针通常被实现为地址。地址是一个数字,它是“字节数组”的偏移量,这是进程的整个虚拟地址空间。[…]
For all these reasons we do not describe references as addresses in the specification. The spec just says that a variable of reference type "stores a reference" to an object, and leaves it completely vague as to how that might be implemented. Similarly, a pointer variable stores "the address" of an object, which again, is left pretty vague. Nowhere do we say that references are the same as addresses.
由于所有这些原因,我们不将引用描述为规范中的地址。规范只是说引用类型的变量“存储”对象的引用,而对于如何实现引用,则完全不明确。类似地,一个指针变量存储一个对象的“地址”,同样地,这个地址也很模糊。我们没有说过引用和地址是一样的。
So, in C# a reference is some vague thing that lets you reference an object. You cannot do anything with a reference except dereference it, and compare it with another reference for equality. And in C# a pointer is identified as an address.
所以,在c#中,引用是一种模糊的东西,可以让你引用一个对象。除了删除引用之外,您不能对引用做任何事情,并将它与另一个引用进行比较以获得相等性。在c#中,指针被标识为一个地址。
By contrast with a reference, you can do much more with a pointer that contains an address. Addresses can be manipulated mathematically; you can subtract one from another, you can add integers to them, and so on. Their legal operations indicate that they are "fancy numbers" that index into the "array" that is the virtual address space of the process.
与引用相比,您可以使用包含地址的指针做更多的事情。地址可以用数学方法处理;你可以从另一个中减去一个,你可以给它们加上整数,等等。它们的合法操作表明,它们是“花哨的数字”,索引到“数组”,即流程的虚拟地址空间。
#9
1
Those readers familiar with C/C++ have probably noticed that object references appear to be similar to pointers. This suspicion is, essentially, correct. An object reference is similar to a memory pointer. The main difference—and the key to Java’s safety—is that you cannot manipulate references as you can actual pointers. Thus, you cannot cause an object reference to point to an arbitrary memory location or manipulate it like an integer.
熟悉C/ c++的读者可能已经注意到对象引用似乎与指针相似。这种怀疑基本上是正确的。对象引用类似于内存指针。主要的不同之处——以及Java安全的关键——是您不能操纵引用,因为您可以实际的指针。因此,不能使对象引用指向任意内存位置,也不能像处理整数那样操作它。
- java not supported & operator then how can u get object address in java.
- 不支持java和操作符,如何在java中获取对象地址。
- In java reference is done by object for example
- 例如,在java引用中是由对象完成的
MyClass object1 = new MyClass();
MyClass object1 =新MyClass();
MyClass object2 = object1;
MyClass object2 =中的object1;
i.e. you might think that object1 and object2 refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, object1 and object2 will both refer to the same object. if you change in any one effect other.
例如,您可能认为object1和object2指的是独立的、不同的对象。然而,这是错误的。相反,在执行此片段之后,object1和object2都将引用相同的对象。如果你改变了一个效果,另一个效果。
*you cann't change the address of object after initialize i.e MyClass obj =new MyClass(); as reference in c++ , with object you can change only object instance value
*你不能在初始化i后更改对象的地址。e MyClass obj =new MyClass();在c++中,使用对象可以只更改对象实例值。
Note: java provide a special feature object1 has been set to null, but object2 still points to the original object.
注意:java提供了一个特殊的特性object1被设置为null,但是object2仍然指向原始对象。
#10
0
i m not sure about java but in C# or VB.net you can pass by value or by refrence
我不确定java,但是在c#或VB.net中,你可以通过值或折射传递
example passing by refrence
例子经过具有一定
static void Main(string[] args)
{
int x = 1;
Foo(ref x);
Console.WriteLine(x.ToString());//this will print 2
}
private static void Foo(ref int x)
{
x++;
}
notice that x is equal to 1 but when you call the method Foo and passing x by refrence when x is increased by one in Foo the original value is increased too
注意,x = 1,但当你调用方法Foo,当x在Foo中增加1时,它通过x,它的原始值也增加了。