Search code examples
javaparametersprimitive

what is the difference between passing an object and a primitive data as the parameter in Java?


I'm a little bit confused about the difference between passing an object and a primitive data as the parameter in Java. I read a post here explaining that when you pass a primitive data, you copy that data and pass it, but if you pass an object then you pass by the object reference. And I also read this discussion explaining that there's no pass-by-reference in Java. So what is the real difference between above two passes and why Java deals with them differently? Thanks in advance.


Solution

  • There's no difference between passing a primitive and an object reference. Both are passed by value. In the first case, the primitive value is copied; in the second case, the reference value is copied.