Why we said JAVA is Pass by Value

Today, I was asked a question that Does Java pass by reference or pass by value?

A popular mistake is that when we pass primitives like int, char, etc. it pass by value, and when we pass objects, it pass by reference.

It is exactly wrong!!!

Look at following code:

What is the right answer?

The changes are not reflected back if we change the object itself to refer some other location or object.

So, like the code above. we pass ob into method, that exactly pass a copy of ob, and then this reference change to another new Object, it does not matter about the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *