What is meant by ‘value semantics’, and what is meant by ‘implicit pointer semantics’?
'值语义'是什么意思,'隐式指针语义'是什么意思?
4 个解决方案
#1
12
Java is using implicit pointer semantics for Object types and value semantics for primitives.
Java正在使用Object类型的隐式指针语义和基元的值语义。
Value semantics means that you deal directly with values and that you pass copies around. The point here is that when you have a value, you can trust it won't change behind your back.
值语义意味着您直接处理值并传递副本。这里的要点是,当你有一个价值时,你可以相信它不会在你背后改变。
With pointer semantics, you don't have a value, you have an 'address'. Someone else could alter what is there, you can't know.
使用指针语义,您没有值,您有一个'地址'。别人可以改变那里的东西,你无法知道。
Pointer Semantics in C++ :
C ++中的指针语义:
void foo(Bar * b) ...
... b->bar() ...
You need an * to ask for pointer semantics and -> to call methods on the pointee.
你需要一个*来询问指针语义和 - >来调用指针对象上的方法。
Implicit Pointer Semantics in Java :
Java中的隐式指针语义:
void foo(Bar b) ...
... b.bar() ...
Since you don't have the choice of using value semantics, the * isn't needed nor the distinction between -> and ., hence the implicit.
由于您没有使用值语义的选择,因此不需要*,也不需要区分 - >和。因此是隐式的。
#2
11
Basically, value semantics means that assigning one value to another creates a copy:
基本上,值语义意味着将一个值分配给另一个值会创建一个副本:
int x = 1;
int y = x;
x = 2; // y remains the same!
A special case is a function call which gets passed an argument:
一个特殊情况是一个函数调用,它传递一个参数:
void f(int x) {
x = 5;
}
int a = 1;
f(a);
// a is still 1
This is actually the same for Java and C++. However, Java knows only a few primitive types, among them int
, double
, boolean
and char
, along with enums which behave in this manner. All other types use reference semantics which means that an assignment of one value to another actually redirects a pointer instead of copying the underlying value:
对于Java和C ++,这实际上是相同的。但是,Java只知道一些基本类型,其中包括int,double,boolean和char,以及以这种方式运行的枚举。所有其他类型都使用引用语义,这意味着将一个值赋值给另一个实际上重定向指针而不是复制基础值:
class Foo {
int x;
public Foo(int x) { this.x = x; }
}
Foo a = new Foo(42);
Foo b = a; // b and a share the same instance!
a.x = 32;
//b.x is now also changed.
There are a few caveats however. For example, many reference types (String
, Integer
…) are actually immutables. Their value cannot be changed and any assignment to them overrides the old value.
但是有一些警告。例如,许多引用类型(String,Integer ...)实际上是不可变的。它们的值无法更改,对它们的任何赋值都会覆盖旧值。
Also, arguments still get passed by value. This means that the value of an object passed to a function can be changed but its reference can't:
此外,参数仍然按值传递。这意味着传递给函数的对象的值可以更改,但其引用不能:
void f(Foo foo) {
foo.x = 42;
}
void g(Foo foo) {
foo = new Foo(42);
}
Foo a = new Foo(23);
f(a);
// a.x is now 42!
Foo b = new Foo(1);
g(b);
// b remains unchanged!
#3
2
Java is pass by value. C++ can use both, value and reference semantics.
Java是按值传递的。 C ++可以使用值,值和引用语义。
#4
1
Java uses implicit pointer semantics
on variable access (you can not directly edit the reference, it autmatically (implicit) gets resolved to the Object on access) and also uses Pass-by-Value semantics
on method parameters passing.
Java对变量访问使用隐式指针语义(您不能直接编辑引用,它可以自动(隐式)在访问时解析为Object),并且还使用传递方法参数的Pass-by-Value语义。
Read Pass-by-value semantics in Java applications:
读取Java应用程序中的按值传递语义:
In Java applications, when an object reference is a parameter to a method, you are passing a copy of the reference (pass by value), not the reference itself. Note that the calling method's object reference and the copy are pointing to the same object. This is an important distinction. A Java application does nothing differently when passing parameters of varying types like C++ does. Java applications pass all parameters by value, thus making copies of all parameters regardless of type.
在Java应用程序中,当对象引用是方法的参数时,您传递的是引用的副本(按值传递),而不是引用本身。请注意,调用方法的对象引用和副本指向同一个对象。这是一个重要的区别。当传递不同类型的参数(如C ++)时,Java应用程序没有任何不同。 Java应用程序按值传递所有参数,从而复制所有参数,而不管类型如何。
Short: All parameters in Java are passed by value. But that doesn't mean an Object gets copied (like the default in PHP4), but the reference to that object gets copied.
简短:Java中的所有参数都按值传递。但这并不意味着Object被复制(就像PHP4中的默认值一样),但是对该对象的引用被复制了。
You'll see all explanations and in-depth examples on Pass-by-value semantics in Java applications
您将在Java应用程序中看到有关Pass-by-value语义的所有解释和深入示例
#1
12
Java is using implicit pointer semantics for Object types and value semantics for primitives.
Java正在使用Object类型的隐式指针语义和基元的值语义。
Value semantics means that you deal directly with values and that you pass copies around. The point here is that when you have a value, you can trust it won't change behind your back.
值语义意味着您直接处理值并传递副本。这里的要点是,当你有一个价值时,你可以相信它不会在你背后改变。
With pointer semantics, you don't have a value, you have an 'address'. Someone else could alter what is there, you can't know.
使用指针语义,您没有值,您有一个'地址'。别人可以改变那里的东西,你无法知道。
Pointer Semantics in C++ :
C ++中的指针语义:
void foo(Bar * b) ...
... b->bar() ...
You need an * to ask for pointer semantics and -> to call methods on the pointee.
你需要一个*来询问指针语义和 - >来调用指针对象上的方法。
Implicit Pointer Semantics in Java :
Java中的隐式指针语义:
void foo(Bar b) ...
... b.bar() ...
Since you don't have the choice of using value semantics, the * isn't needed nor the distinction between -> and ., hence the implicit.
由于您没有使用值语义的选择,因此不需要*,也不需要区分 - >和。因此是隐式的。
#2
11
Basically, value semantics means that assigning one value to another creates a copy:
基本上,值语义意味着将一个值分配给另一个值会创建一个副本:
int x = 1;
int y = x;
x = 2; // y remains the same!
A special case is a function call which gets passed an argument:
一个特殊情况是一个函数调用,它传递一个参数:
void f(int x) {
x = 5;
}
int a = 1;
f(a);
// a is still 1
This is actually the same for Java and C++. However, Java knows only a few primitive types, among them int
, double
, boolean
and char
, along with enums which behave in this manner. All other types use reference semantics which means that an assignment of one value to another actually redirects a pointer instead of copying the underlying value:
对于Java和C ++,这实际上是相同的。但是,Java只知道一些基本类型,其中包括int,double,boolean和char,以及以这种方式运行的枚举。所有其他类型都使用引用语义,这意味着将一个值赋值给另一个实际上重定向指针而不是复制基础值:
class Foo {
int x;
public Foo(int x) { this.x = x; }
}
Foo a = new Foo(42);
Foo b = a; // b and a share the same instance!
a.x = 32;
//b.x is now also changed.
There are a few caveats however. For example, many reference types (String
, Integer
…) are actually immutables. Their value cannot be changed and any assignment to them overrides the old value.
但是有一些警告。例如,许多引用类型(String,Integer ...)实际上是不可变的。它们的值无法更改,对它们的任何赋值都会覆盖旧值。
Also, arguments still get passed by value. This means that the value of an object passed to a function can be changed but its reference can't:
此外,参数仍然按值传递。这意味着传递给函数的对象的值可以更改,但其引用不能:
void f(Foo foo) {
foo.x = 42;
}
void g(Foo foo) {
foo = new Foo(42);
}
Foo a = new Foo(23);
f(a);
// a.x is now 42!
Foo b = new Foo(1);
g(b);
// b remains unchanged!
#3
2
Java is pass by value. C++ can use both, value and reference semantics.
Java是按值传递的。 C ++可以使用值,值和引用语义。
#4
1
Java uses implicit pointer semantics
on variable access (you can not directly edit the reference, it autmatically (implicit) gets resolved to the Object on access) and also uses Pass-by-Value semantics
on method parameters passing.
Java对变量访问使用隐式指针语义(您不能直接编辑引用,它可以自动(隐式)在访问时解析为Object),并且还使用传递方法参数的Pass-by-Value语义。
Read Pass-by-value semantics in Java applications:
读取Java应用程序中的按值传递语义:
In Java applications, when an object reference is a parameter to a method, you are passing a copy of the reference (pass by value), not the reference itself. Note that the calling method's object reference and the copy are pointing to the same object. This is an important distinction. A Java application does nothing differently when passing parameters of varying types like C++ does. Java applications pass all parameters by value, thus making copies of all parameters regardless of type.
在Java应用程序中,当对象引用是方法的参数时,您传递的是引用的副本(按值传递),而不是引用本身。请注意,调用方法的对象引用和副本指向同一个对象。这是一个重要的区别。当传递不同类型的参数(如C ++)时,Java应用程序没有任何不同。 Java应用程序按值传递所有参数,从而复制所有参数,而不管类型如何。
Short: All parameters in Java are passed by value. But that doesn't mean an Object gets copied (like the default in PHP4), but the reference to that object gets copied.
简短:Java中的所有参数都按值传递。但这并不意味着Object被复制(就像PHP4中的默认值一样),但是对该对象的引用被复制了。
You'll see all explanations and in-depth examples on Pass-by-value semantics in Java applications
您将在Java应用程序中看到有关Pass-by-value语义的所有解释和深入示例