对于原始类型和非原始类型,Java 总是按值传递。例如。,void JavaMethod(int i, Foo f){ i = 9; //not changed from caller's point of view f = new Foo(); //not changed from caller's point of view}除非使用“ref”关键字,否则 C# 是按值传递的:void CSharpMethod(int i, Foo f, ref int j, ref Bar b){ i = 9; //not changed from caller's point of view f = new Foo(); //not changed from caller's point of view j = 9; //changed from caller's point of view b = new Bar(); //changed from caller's point of view}在这两种语言中,您都可以在按值传递时修改非原始对象的内部状态:void JavaOrCSharpMethod(Foo f){ f.field = 9; //internal state is changed from caller's point of view}请记住将新实例分配给参数和修改对象的内部状态之间的区别。不理解这一点是很多关于这个主题的困惑的根源。此外,除了原始类型没有改变其状态的成员之外,是否使用原始类型或非原始类型没有实质内容,因此它总是通过赋值完成。任何只能通过赋值改变状态的非原始类型看起来都是一样的。我还会包括一些 C++ 示例,但这会使讨论变得非常复杂,因为 C++ 有许多实现按引用传递的方法。