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 8673871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:34:05+00:00 2026-06-12T19:34:05+00:00

The following code simply uses a null reference as a varargs parameter. package currenttime;

  • 0

The following code simply uses a null reference as a varargs parameter.

package currenttime;

import java.util.Arrays;

public class Main
{
    private static void temp(String...str)
    {
        System.out.println(Arrays.asList(str));
    }

    public static void main(String[] args)
    {
        temp(null,null);
        temp(null);
    }
}

The first call to the method temp(null, null); displays [null, null] means that str[0]=null and str[1]=null.

but the later call to temp(null); causes the NullPointerException to be thrown which appears that the str itself is null.

If it’s type cast to String something like this temp((String)null);, it works and displays [null].

Why in the last call, an explicit type cast is required? It seems to me that it’s considered to be a string array with a null reference which is different from the first call. What is the correct answer?

  • 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-06-12T19:34:06+00:00Added an answer on June 12, 2026 at 7:34 pm

    It seems to me that it’s considered to be a string array with a null
    reference which is different from the first call

    Exactly. This is what happening.

    Actually, it’s just about precedence between: – exact-match, var-args, boxing and type-casting.

    Compiler follow the following precedence when checking for methods to be called, or how the argument will be passed: –

    Exact-Match > Type-Cast > Boxing > Var-args

    So, you can see that, var-args has the lowest precedence, and exact-match has the highest precedence. That means, if an argument is good-enough for an exact-match then it will be considered as such.

    Now, when you pass null as an argument, then null can directly be passed to your var-args parameter as the value of reference. It is an exact match for the parameter.
    So, you need to typecast it explicitly to String to tell that its actually the first element of your var-args parameter

    Whereas, in case of null, null, it will be taken as two elements of your var-args. As it cannot be a value of reference.

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

Sidebar

Related Questions

The following snippet uses simple Java code. package pkg; final public class Main {
Possible Duplicate: NullPointerException through auto-boxing-behavior of Java ternary operator The following code uses simple
I have the following code that simply uses a jquery ui modal dialog to
I have the following code that simply moves my image across the screen: /*
I simply could not understand why the following code does not work. What could
I am using the following code to connect to a MySQL DB and simply
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
The following code uses getchar() to accept a line of input. #include <stdio.h> #include
I want to rewrite some code that uses a lot of unsigned char arrays,
Argument exception error is thrown at the following code. It's a simple code but

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.