Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:46:06+00:00 2026-05-26T09:46:06+00:00

An array parameter declaration causes a syntax error where the invocations happen . Yet

  • 0

An array parameter declaration causes a syntax error where the invocations happen. Yet the main method uses String[] instead of String… How can I understand this inconsistency?

package domain.test;

import utilities.CConsole;

public class Tester {
    public static void main(String[] args) 
    {
        Test1 t = new Test1();
        t.method1(0); // the array will exist but have a length of zero
        t.method1(0, (Object[])null); // the array will not exist
        t.method1(0, "a");
        t.method1(0, "a", "b");
        CConsole.pw.format("\n");

        t.method2(0); // the array will exist but have a length of zero
        t.method2(0, (String[])null); // the array will not exist
        t.method2(0, "a");
        t.method2(0, "a", "b");
        CConsole.pw.format("\n");
    }
}

class Test1 {
    void method1(int number, Object... args) // Object[] causes syntax errors
    {
        if (args == null)
            CConsole.pw.format("args == null\n");
        else
        {
            CConsole.pw.format("args != null    ");
            CConsole.pw.format("args.length %d\n", args.length);
        }
    }

    void method2(int number, String... args) // String[] causes syntax errors
    {
        if (args == null)
            CConsole.pw.format("args == null\n");
        else
        {
            CConsole.pw.format("args != null    ");
            CConsole.pw.format("args.length %d\n", args.length);
        }
    }
}

How can the inconsistency be explained?

The following is included for the person that said that it compiles: To get this error change method1() to use Object[].

Summary edit: The lesson seems to be this. As @Andrew Barber has emphasized, String… is distinct from String[]. They are not interchangeable generally, so do not try to treat them the same way (even though I could name reasons why they seem interchangeable). They are interchangeable in the case of main(). In the case of main() some people might call this sugar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:46:07+00:00Added an answer on May 26, 2026 at 9:46 am

    In general , you can use varargs to specify the fact that a method is taking a variable number of arguments as input. Thus, collections, arrays, and simply individual objects can be sent interchangably in java. This simplifies methods but there are some gotchas.

    Varargs work naturally if

    1) They are at the END of a method signature
    2) The type of data you are defining as the var arg is linear (i.e. an array or collection) .

    As you can tell, main(String[] args) is thus a natural fit for using var args (that is, args is at the end of your method, it is the last parameter, so it is effectively the same as declaring “String… args” as the last parameter) . To understand them better I would suggest writing these two methods and watching what the compiler does :

    So to answer your question : you got lucky 🙂 Your method is sending a variable number of arguments to the main method, and new versions of java allow you to get away with var args as input to main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: What does “…” mean in Java? Java array argument “declaration” syntax Can
I have a method that gets a nested array as parameter: Number[][] data where
I have a question about the syntax of the Java main declaration: public static
I am trying to pass array parameter to SQL commnd in C# like below,
I get: "Resource id #8 Warning: mysql_fetch_array() expects parameter 1 to be resource, string
Prototype's Ajax accepts a parameter called 'parameters' with an hash array for parameters (
In my plugin architecture I am currently passing a plugin name (string), method name
I am trying to write a function that accepts an array as a parameter.
I'm using reflection to examine the following method declaration and am wondering if it
I am trying to call the method with the following declaration: extern C __declspec(dllimport)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.