1.

What type of parameter passing does Java support?

Answer»

There are basically TWO types of parameter passing:

  • Pass by value
  • Pass by reference

JAVA supports Pass by value.

Passing by value means that when a method is called, a copy of the parameters is SENT to the memory. When we are using the parameters inside the method or the block, the actual arguments aren’t used but the copy of those arguments (formal parameters) are worked with. There is no change in the actual parameters.

Passing by reference means that the changes in the parameters will be reflected in the original value . This occurs because the method RECEIVES the memory address of the parameters i.e. the address of the parameters.

What Java does is that it passes the reference of the object by value.

In Java, arguments are always passed by value irrespective of their original VARIABLE type. Each time a method is called, a copy is created in the stack memory and its version is passed to the method.

There are two situations which arise:

  • The original variable type is primitive: If the original variable type is primitive, then a copy is created in the stack memory and is further passed to the method.
  • The original variable type is non-primitive: If the original variable type is non-primitive, then a new reference is formed in the stack memory which points to the actual parameters and this new reference is sent to the method.


Discussion

No Comment Found