We have two simple methods — one method in Java one method in C#. Both of these two methods get one parameter and return parameter+1 after it. I changed this sample for testing an object input not primitive value.
C# (Part I)
Java (Part I)
Also, you can see our parameter does not change after call function sum.
We change param type from integer to a class with a one property as integer.
C# (Part II)
Java (Part II)
Now we change the code and create new instance in sum method of input object:
C# (Part III)
Java (Part III)
Now I change call by value to reference:
C# (Part IV)
Java (Part IV)
So we have an error here. You simply can’t do that in Java, since Java doesn’t support pointers …
- Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object.
- However, changing a property of an object referenced by a variable does change the underlying object.