Java wrapper 类 - 更改参数值

Java wrapper classes - change value of parameters

当我查找为什么要使用 wrapper 时,我一直看到以下内容 类:

Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).

  1. 如果我们将参数设置为 return 值,我们可以修改原语
  2. java 中的所有内容均按值传递

该声明的实际含义是什么?有人可以举个例子吗?

我搜索了为什么要使用 wrapper 类 并得出以下结果:

  1. https://www.tutorialspoint.com/why-do-we-need-a-wrapper-class-in-java

  2. https://www.geeksforgeeks.org/need-of-wrapper-classes-in-java/

  3. https://www.javatpoint.com/wrapper-class-in-java

他们都说同样的话。是完全错误还是他们想说别的?

引自https://www.tutorialspoint.com/why-do-we-need-a-wrapper-class-in-java:

The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value).

引自https://www.geeksforgeeks.org/need-of-wrapper-classes-in-java/:

Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).

引自https://www.javatpoint.com/wrapper-class-in-java:

But, if we convert the primitive value in an object, it will change the original value.

在包装器的上下文中,它们确实是完全错误的。原始类型的包装器 类 都是不可变的,一旦创建了包装器对象(当然不包括反射),包装器内部的实际(原始类型)值就不能更改。所以即使你有以下代码块:

Integer outside = Integer.valueOf(42);
someMethod(outside);

public static void someMethod(Integer inside) {
}

并且变量outsideinside会引用Integer.valueOf()创建的同一个对象,它不会帮你改变outside变量的引用值本身或 outside.

引用的对象