Yes,it’s a misconception. It’s a huge,widespread misconception. It
leads to an interview question I hate: (“how does Java pass
arguments”). I hate it because roughly half of the interviewers
actually seem to want the wrong answer (“primitives by value,objects
by reference”). The right answer takes longer to give,and seems to
confuse some of them. And they won’t be convinced: I swear I flunked a
tech screen because the CSMajor-type screener had heard the
misconception in college and believed it as gospel. Feh. – CPerkins
Aug 13 ’09 at 14:34
有人可以解释一下,一个新的程序员可以把握,说什么有区别呢?
“在Java中,基元由值传递,对象通过引用传递.
和:
“在Java中没有什么是通过引用传递的,引用是通过值传递的”.
这些陈述在某种意义上都是真实的吗?我不想邀请游行,但这听起来像是一个非常重要的概念,而我还是不完全明白.
@R_403_323@
你可以做一个非常简单的测试来确定语言是否支持通过引用.问问自己是否可以用这种语言编写一个交换函数,即可以这样做的东西
x == A,y == B swap(x,y); x == B,y == A
作为Java程序员,您很快就意识到,您无法在Java中实现,因此您(正确地)得出Java不通过引用传递的结论.
回到你的句子:
>在Java中,对象通过引用传递.
这是假的我会认为你只能传递一个包含在变量中的东西,正如我上面所说的那样,一个变量不能包含一个对象,所以你不能在Java中传递一个对象.
>在Java中没有任何东西通过引用传递,引用是通过值传递的.
这是真的.