Is Java ?pass-by-reference? or ?pass-by-value??

  Sep 4, 2017

Is Java ?pass-by-reference? or ?pass-by-value??

Please specify with an example thanks.

Solved 0
6 months ago 1 Answer 0 views

Answers ( 1 )

  1. 4 months ago

    Java is pass by value which means when we call a method with an argument as a reference variable then this passed reference variable is actually a copy of the real declared variable. for example -

    Dog dog = new Dog ();

    in above example, the dog passed inside nameDog() method is actually a copy of dog reference variable used to declare the actual object. But the catch here is this, that if we want to make any change in the Dog object from inside the nameDog() function using argument 'dog' variable that change will change the state of actual Object because both the reference variables refer o the same Dog object . But this is not the case with premitive, and now you can figure it out why.

Your Answer