Parameter Passing Techniques in Java with Examples19 Jun 2025 | 4 min read Parameter passing in Java refers to the mechanism of transferring data between methods or functions. Java supports two types of parameters passing techniques
Understanding these techniques is essential for effectively utilizing method parameters in Java. ![]() Types of Parameters:1. Formal Parameter:A variable and its corresponding data type are referred to as formal parameters when they exist in the definition or prototype of a function or method. As soon as the function or method is called and it serves as a placeholder for an argument that will be supplied. The function or method performs calculations or actions using the formal parameter. Syntax: In the above syntax:
2. Actual Parameter:The value or expression that corresponds to a formal parameter and is supplied to a function or method during a function or method call is referred to as an actual parameter is also known as an argument. It offers the real information or value that the method or function will work with. Syntax: functionName(argument) In the above syntax:
1. Call-by-Value:In Call-by-value the copy of the value of the actual parameter is passed to the formal parameter of the method. Any of the modifications made to the formal parameter within the method do not affect the actual parameter. ALGORITHM: Step 1: Create a class named CallByValueExample. Step 2: Inside the main method: Step 2.1: Declare an integer variable num and assign it the value 10. Step 2.2: Print the value of num before calling the method. Step 2.3: Call the modifyValue method, passing num as the actual parameter. Step 2.4: Print the value of num after calling the method. Step 3: Define the modifyValue method that takes an integer parameter value: Step 3.1: Modify the formal parameter value by assigning it the value 20. Step 3.2: Print the value of value inside the method. Implementation: The implementation of the above steps given below FileName: CallByValueExample.java Output: Before calling method: 10 Inside method: 20 After calling method: 10 Complexity Analysis: Time Complexity is O(1). Space Complexity is O(1). Call-by-Reference:call by reference" is a method of passing arguments to functions or methods where the memory address (or reference) of the variable is passed rather than the value itself. This means that changes made to the formal parameter within the function affect the actual parameter in the calling environment. In "call by reference," when a reference to a variable is passed, any modifications made to the parameter inside the function are transmitted back to the caller. This is because the formal parameter receives a reference (or pointer) to the actual data. ALGORITHM:Step 1: Start Step 2: Define the class "CallByReference" Step 2.1: Declare instance variables: a (int) and b (int) Step 2.1: Define a constructor to assign values to a and b Step 3: Define the method "changeValue" inside the "CallByReference" class: Step 3.1: Accept a parameter of type "CallByReference" called "obj" Step 3.2: Add 10 to the value of "obj.a" Step 3.3: Add 20 to the value of "obj.b" Step 4: Define the class "Main" Step 4.1: Define the main method Step 4.2: Create an instance of "CallByReference" called "object" with values 10 and 20 Step 4.3: Print the values of "object.a" and "object.b" Step 4.4: Call the "changeValue" method on "object" and pass "object" as an argument Step 4.5: Print the updated values of "object.a" and "object.b" Step 5: End Implementation:The implementation of the above steps given below FileName: CallByReferenceExample.java Output: Value of a: 10 & b: 20 Value of a: 20 & b: 40 Complexity Analysis: Time Complexity is O(1). Space Complexity is O(1). |
? Java Final Methods The final keyword in Java can be used to prohibit method overriding, declare constants, and stop inheritance. A method that is marked as final indicates that subclasses are not allowed to override it. It can be very helpful in a number of situations,...
3 min read
How to sort a list in Java We can use the following methods to sort the list: Using stream.sorted() method Using Comparator.reverseOrder() method Using Comparator.naturalOrder() method Using Collections.reverseOrder() method Using Collections.sort() method Java Stream interface Java Stream interface provides two methods for sorting the list: sorted() method Stream interface provides a sorted() method to sort a...
3 min read
Calculating the value of a number raised to its reverse offers a captivating blend of arithmetic and numerical exploration. This intriguing concept invites curiosity about the interplay between numbers and their reversals, highlighting the beauty of mathematical patterns and relationships. Problem Statement We have given a number P...
4 min read
A prerequisite for an independent set of graphs is the set of vertices in which no two are neighboring. Just by definition, it is the opposite of a clique, so understanding a graph's complements is essential to moving on. In essence, the concept of a planar...
17 min read
In Java, an operator is a symbol that performs specified operations. In this section, we will discuss only the bitwise operator and its types with appropriate examples. Types of Bitwise Operators There are six types of bitwise operators in Java: Bitwise AND Bitwise Exclusive OR Bitwise Inclusive OR Bitwise Complement Bit Shift...
7 min read
In Java, creating test cases for exceptions is not a difficult task. The JUnit testing tool of Java provides a way to track the exception handling of code. We can write the test cases to check whether the code throws a desired exception or not. In order...
4 min read
In Java programming, packages serve as the containers for organizing classes, interfaces, and other related resources. They provide a way to group the related code together and help in creating a modular and maintainable application structure. While packages themselves are essential for organizing the code, Java...
3 min read
One of the core ideas of object-oriented programming (OOP) is data hiding, which is limiting access to an object's features and only revealing what is required. It is a fundamental idea that helps encapsulate data into classes, improving code maintainability and security. Data hiding, sometimes referred to...
5 min read
How to ? Opening a file in Java is a fundamental operation that can be achieved through various classes and methods provided by the Java API, tailored to different file operations like reading or writing. For reading text files, the FileReader class combined with BufferedReader allows efficient...
5 min read
In Java, number programs such as Armstrong number, Prime number, and Palindrome number are frequently asked by the interviewer. Unlike other number programs, the Economical number is not frequently asked by the interviewers. An economical number is a number whose number of digits in the prime factorization...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India