
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Method Parameters
Introduction
We know that Java Method Parameters are variables used in method declarations to accept inputs and enable the methods to work with data that are dynamic.
Let us discuss the Java Method Parameters and their use in making methods reusable and adaptable. We will discuss the importance of data input, code reusability, and flexibility. We will also explain about the passbyvalue and passbyreference by using Java Parameters with basic programming examples. By understanding the article, we hope our readers will understand the importance of the Java Method Parameters as it helps to create essential and maintainable Java programs.
Definition of Java Method Parameters
Java method parameters are vital part of Java. It allows the methods to take data and make them adaptable and versatile. Java method parameters act as inputs to methods, they enable the passing of information when the method is called. By using parameters, we can create methods that work with various data, we also can perform reusability and avoid hardcoding specific values.
Parameters help to improve the flexibility of the methods. It enables the same method to process different values and reduces the need for repeated code.
passbyvalue by using Java Method Parameters
In this programming example we will see how can we pass the parameter inside a function in Java.
Example
public class PassValue { public static void modifyValue(int no) { no = no + 10; } public static void main(String args []) { int x = 5; System.out.println("Original value of x: " + x); modifyValue(x); System.out.println("Modified value of x: " + x); int y = 20; System.out.println("Original value of y: " + y); modifyValue(y); System.out.println("Modified value of y: " + y); int z = -15; System.out.println("Original value of z: " + z); modifyValue(z); System.out.println("Modified value of z: " + z); int largeNo = 1000; System.out.println("Original value of largeNo: " + largeNo); modifyValue(largeNo); System.out.println("Modified value of largeNo: " + largeNo); } }
Output
Original value of x: 5 Modified value of x: 5 Original value of y: 20 Modified value of y: 20 Original value of z: -15 Modified value of z: -15 Original value of largeNo: 1000 Modified value of largeNo: 1000
For this we have simply defined a class named PassValue which access modifier is public. Inside the class we have called a function named modifyValue (). Which is public, static and its return type is void. We have passed the value of a local variable no which is an integer type variable inside the parenthesis of this function as a parameter. Now inside the function we have added value 10 with this variable no.
public static void modifyValue(int no) { no = no + 10; }
Now we have called the main () function and inside this function we have declared a variable named "x" whose data type is integer. Now we have assigned some value to the variable. The assigned value is 5 and simply print this value. After that we called a function named modifyValue () and inside the parenthesis, we passed the value of "x" as an argument and print the value.
modifyValue(x); System.out.println("Modified value of x: " + x);
we have declared a variable named "y" whose data type is an integer. Now we have assigned some value to the variable. The assigned value is 20 and simply print this value. After that we called a function named modifyValue () and inside the parenthesis we passed the value of "y" as an argument and print the value.
modifyValue(y); System.out.println("Modified value of y: " + y);
we have declared a variable named "z" whose data type is an integer. Now we have assigned some value to the variable. The assigned value is -15 and simply print this value. After that we called a function named modifyValue () and inside the parenthesis we passed the value of "z" as an argument and print the value.
modifyValue(z); System.out.println("Modified value of z: " + z);
we have declared a variable named "x
largeNo" which data type is an integer. Now we have assigned some value to the variable. The assigned value is 1000. After that we called a function named modifyValue () and inside the parenthesis we have pass the value of "largeNo" as an argument and printed the value.
modifyValue(largeNo); System.out.println("Modified value of largeNo: " + largeNo);
Now we have got the resulting output.
Pass-by-Reference by using Java Method Parameters
In this programming example we will learn how to pass the reference of variable instead of the value of a variable through a function in Java.
Example
public class ReferenceParametersExample { public static void changeName (Student student) { student.setName ("Rahul Kumar"); } public static void main(String args []) { Student s = new Student ("Sneha Paul"); System.out.println("Original name: " + s.getName()); changeName(s); System.out.println("Modified name: " + s.getName()); } } class Student { private String name; public Student(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } }
Output
Original name: Sneha Paul Modified name: Rahul Kumar
For this we have simply defined a class named RefereceParametersExample and inside this class we have called a function named changeName () and passed the student reference as a parameter inside the parenthesis of this function.
public static void changeName (Student student) { student.setName ("Rahul Kumar"); }
Now we have called the main () function and inside the main () function we have created the object named student of the class named student and pass a value as reference as a parameter inside the parenthesis.
Student s = new Student ("Sneha Paul");
After that we have defined the class named student and inside this class, we have declared a string named "name" and called the constructor of this class to initialize the value of the string. After that we have to call the setName () function and call the "this" operator inside this function.
public void setName(String name) { this.name = name; }
Finally, we have to call the function getName () and end the program.
Conclusion
Discussing in detail how to set the parameters inside a method through different types of programming examples we have come to the conclusion that it is an important procedure to pass the value of a variable as well as the reference of the variable to make the code in Java too much scalable.