Методы передачи параметров в Java с примерами
Существуют разные способы передачи данных параметров в методы и функции и из них. Предположим, что функция B () вызывается из другой функции A () . В этом случае A называется «вызывающей функцией», а B называется «вызываемой функцией или вызываемой функцией» . Кроме того, аргументы, которые A отправляет B , называются фактическими аргументами, а параметры B называются формальными аргументами .
Типы параметров:
- Формальный параметр: переменная и ее тип в том виде, в котором они указаны в прототипе функции или метода.
Синтаксис:имя_функции (тип данных имя_переменной)
- Фактический параметр: переменная или выражение, соответствующее формальному параметру, который появляется в вызове функции или метода в вызывающей среде.
Синтаксис:func_name (имена переменных);
Важные методы передачи параметров
- Pass By Value: Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.
Java in fact is strictly call by value.

Example:
// Java program to illustrate// Call by Value// CalleeclassCallByValue {// Function to change the value// of the parameterspublicstaticvoidExample(intx,inty){x++;y++;}}// CallerpublicclassMain {publicstaticvoidmain(String[] args){inta =10;intb =20;// Instance of class is createdCallByValue object =newCallByValue();System.out.println("Value of a: "+ a+" & b: "+ b);// Passing variables in the class functionobject.Example(a, b);// Displaying values after// calling the functionSystem.out.println("Value of a: "+ a +" & b: "+ b);}}Output:Value of a: 10 & b: 20 Value of a: 10 & b: 20
Shortcomings:
- Inefficiency in storage allocation
- For objects and arrays, the copy semantics are costly
- Call by reference(aliasing): Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as <em>call by reference. This method is efficient in both time and space.

// Java program to illustrate// Call by Reference// CalleeclassCallByReference {inta, b;// Function to assign the value// to the class variablesCallByReference(intx,inty){a = x;b = y;}// Changing the values of class variablesvoidChangeValue(CallByReference obj){obj.a +=10;obj.b +=20;}}// CallerpublicclassMain {publicstaticvoidmain(String[] args){// Instance of class is created// and value is assigned using constructorCallByReference object=newCallByReference(10,20);System.out.println("Value of a: "+ object.a+" & b: "+ object.b);// Changing values in class functionobject.ChangeValue(object);// Displaying values// after calling the functionSystem.out.println("Value of a: "+ object.a+" & b: "+ object.b);}}Output:Value of a: 10 & b: 20 Value of a: 20 & b: 40
Please note that when we pass a reference, a new reference variable to the same object is created. So we can only change members of the object whose reference is passed. We cannot change the reference to refer to some other object as the received reference is a copy of the original reference. Please see example 2 in Java is Strictly Pass by Value!
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.