javarevisited |
Does Java pass by value or pass by reference - Interview Question Posted: 18 Dec 2012 05:57 AM PST Does Java is pass by value or pass by reference is one of the tricky Java question mostly asked on fresher level interviews. Before debating whether Java is pass by value or pass by reference lets first clear what is pass by value and what is pass by reference. This question has its origin on C and C++ where you can pass function parameter either value or memory address, where value is stored (pointer). As per Java specification everything in Java is pass by value whether its primitive value or objects and it does make sense because Java doesn't support pointers or pointer arithmetic, Similarly multiple inheritance and operator overloading is also not supported in Java. This question becomes confusing when interviewer ask about how object is passed in Java ? Answer to this question is simple whenever a method parameter expect object, reference of that object is passed. Many programmer confuses reference with pointers here which is not correct, reference is a kind of handle which is used to locate object or change object, but it doesn't allows any pointer arithmetic i.e. you can not increase or decrease memory address and locate a different object using reference in Java. |
You are subscribed to email updates from Javarevisited To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Post a Comment