Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Method Parameters


Java Method Parameters

In Java, method parameters are variables declared within the parentheses of a method declaration. They allow you to pass data into a method when it is called, enabling the method to perform its task with the provided information. Here's an overview of Java method parameters:

Syntax

        returnType methodName(parameterType1 parameterName1, parameterType2 parameterName2, ...) {
            // Method body
        }
      

Example

        public void greet(String name) {
            System.out.println("Hello, " + name + "!");
        }
      

Types of Parameters

  1. Primitive Parameters: Parameters that hold primitive data types such as int, double, char, etc.
  2.         public void printNumber(int number) {
                System.out.println("Number: " + number);
            }
          
  3. Reference Parameters: Parameters that hold references to objects.

  4.         public void processArray(int[] arr) {
                // Method logic to process the array
            }
          
  5. Varargs Parameters: Introduced in Java 5, varargs (variable-length arguments) allow methods to accept a variable number of arguments of the same type.
  6.         public int sum(int... numbers) {
                int result = 0;
                for (int num : numbers) {
                    result += num;
                }
                return result;
            }
          


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java