Monday, November 18, 2024
Google search engine
HomeLanguagesJavaJava Method Parameters

Java Method Parameters

Parameters are variables defined in the method declaration after the method name, inside the parentheses. This includes primitive types such as int, float, boolean, etc, and non-primitive or object types such as an array, String, etc. You can pass values(Argument) to the method parameters, at the method call. The method call has the same number of arguments and is in the same order, as the parameters. To know more, refer to the article Difference between Parameters and Arguments.

The following example has a method example that takes a String as a parameter and returns a String type value.

Java




import java.io.*;
  
class GFG {
    public static void main(String[] args) {
        // call method pass value to the parameter
          // return value store in a str
        String str = example("GFG!");
        System.out.println(str);
    }
  
    // para is a parameter of type String inside 
      // method name method return String
    public static String example(String para) {
        return "Hello " + para;
    }
}


Output

Hello GFG!

The above example static method has only one parameter. You can add many different types of parameters but java gives a limit, the limit says you can add 255 parameters or less. All primitive or non-primitive types take one unit of parameter length, except long and double. long or double takes two units of parameter length. The total length of parameters is calculated by the sum of the total units of all parameters.

Note: long or double takes two units of parameter length, hence you can add only 127 parameters of this type.

The Basic Syntax is:

public static String methodSyntax(int a0, int a1 .... int a253, int a254) {
    return "Method Executed";
}

The following example has a static method that takes 255 parameters of type int and we pass 255 arguments in a method. The method returns a String value which tells us, there is no error.

Java




import java.io.*;
  
class GFG {
    public static void main(String[] args) {
        // call method
        String value = isRun(
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
            14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
            26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
            38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
            50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
            62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
            74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
            86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
            98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
            108, 109, 110, 111, 112, 113, 114, 115, 116,
            117, 118, 119, 120, 121, 122, 123, 124, 125,
            126, 127, 128, 129, 130, 131, 132, 133, 134,
            135, 136, 137, 138, 139, 140, 141, 142, 143,
            144, 145, 146, 147, 148, 149, 150, 151, 152,
            153, 154, 155, 156, 157, 158, 159, 160, 161,
            162, 163, 164, 165, 166, 167, 168, 169, 170,
            171, 172, 173, 174, 175, 176, 177, 178, 179,
            180, 181, 182, 183, 184, 185, 186, 187, 188,
            189, 190, 191, 192, 193, 194, 195, 196, 197,
            198, 199, 200, 201, 202, 203, 204, 205, 206,
            207, 208, 209, 210, 211, 212, 213, 214, 215,
            216, 217, 218, 219, 220, 221, 222, 223, 224,
            225, 226, 227, 228, 229, 230, 231, 232, 233,
            234, 235, 236, 237, 238, 239, 240, 241, 242,
            243, 244, 245, 246, 247, 248, 249, 250, 251,
            252, 253, 254);
        System.out.println(value);
    }
  
    // method have 255 parameters
    public static String isRun(int a0, int a1, int a2, int a3, int a4, int a5,
          int a6, int a7, int a8, int a9, int a10, int a11,
          int a12, int a13, int a14, int a15, int a16,
          int a17, int a18, int a19, int a20, int a21,
          int a22, int a23, int a24, int a25, int a26,
          int a27, int a28, int a29, int a30, int a31,
          int a32, int a33, int a34, int a35, int a36,
          int a37, int a38, int a39, int a40, int a41,
          int a42, int a43, int a44, int a45, int a46,
          int a47, int a48, int a49, int a50, int a51,
          int a52, int a53, int a54, int a55, int a56,
          int a57, int a58, int a59, int a60, int a61,
          int a62, int a63, int a64, int a65, int a66,
          int a67, int a68, int a69, int a70, int a71,
          int a72, int a73, int a74, int a75, int a76,
          int a77, int a78, int a79, int a80, int a81,
          int a82, int a83, int a84, int a85, int a86,
          int a87, int a88, int a89, int a90, int a91,
          int a92, int a93, int a94, int a95, int a96,
          int a97, int a98, int a99, int a100, int a101,
          int a102, int a103, int a104, int a105, int a106,
          int a107, int a108, int a109, int a110, int a111,
          int a112, int a113, int a114, int a115, int a116,
          int a117, int a118, int a119, int a120, int a121,
          int a122, int a123, int a124, int a125, int a126,
          int a127, int a128, int a129, int a130, int a131,
          int a132, int a133, int a134, int a135, int a136,
          int a137, int a138, int a139, int a140, int a141,
          int a142, int a143, int a144, int a145, int a146,
          int a147, int a148, int a149, int a150, int a151,
          int a152, int a153, int a154, int a155, int a156,
          int a157, int a158, int a159, int a160, int a161,
          int a162, int a163, int a164, int a165, int a166,
          int a167, int a168, int a169, int a170, int a171,
          int a172, int a173, int a174, int a175, int a176,
          int a177, int a178, int a179, int a180, int a181,
          int a182, int a183, int a184, int a185, int a186,
          int a187, int a188, int a189, int a190, int a191,
          int a192, int a193, int a194, int a195, int a196,
          int a197, int a198, int a199, int a200, int a201,
          int a202, int a203, int a204, int a205, int a206,
          int a207, int a208, int a209, int a210, int a211,
          int a212, int a213, int a214, int a215, int a216,
          int a217, int a218, int a219, int a220, int a221,
          int a222, int a223, int a224, int a225, int a226,
          int a227, int a228, int a229, int a230, int a231,
          int a232, int a233, int a234, int a235, int a236,
          int a237, int a238, int a239, int a240, int a241,
          int a242, int a243, int a244, int a245, int a246,
          int a247, int a248, int a249, int a250, int a251,
          int a252, int a253, int a254) {
        return "Method Executed";
    }
}


Output

Method Executed

Note: When we talk about the non-static method, the non-static method accepts only 254 parameters.

RELATED ARTICLES

Most Popular

Recent Comments