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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:48:07+00:00 2026-06-14T19:48:07+00:00

When it comes to overloading functions, I have not completely understood how Java determines

  • 0

When it comes to overloading functions, I have not completely understood how Java determines which function to execute on runtime. Let’s assume we have a simple program like this:

public class Test {

    public static int numberTest(short x, int y) {
        // ...
    }
    public static int numberTest(short x, short y) {
        // ...
    }

    public static void main(String[] args) {
        short number = (short) 5;
        System.out.println(numberTest(number, 3));
    }

}

I’ve tested this – and Java uses the first numberTest() function. Why? Why isn’t it using the second one, or rather, why isn’t it showing a compiler error?

First parameter is short, okay. But second one distinguishes the two functions. As the function call uses just 3, it could be both, couldn’t it? And there is no type conversion needed. Or does Java apply type conversion whenever I use “3” as int? Does it always start with byte and then convert to short and int then?

  • 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-14T19:48:09+00:00Added an answer on June 14, 2026 at 7:48 pm

    First parameter is short, okay. But second one distinguishes the two functions. As the function call uses just 3, it could be both, couldn’t it?

    No. Integer literals in Java are always either int or long. As a simple example, this code:

    static void foo(short x) {
    }
    
    ...
    foo(3);
    

    Gives an error like this:

    Test.java:3: error: method foo in class Test cannot be applied to given types;
            foo(3);
            ^
      required: short
      found: int
      reason: actual argument int cannot be converted to short by method invocation
      conversion
    1 error
    

    From section 3.10.1 of the JLS:

    An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

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

Sidebar

Related Questions

Possible Duplicate: Operator overloading I have to code a clock program in which I
Data comes in by email as a zipped file. The Java solution we wrote
CoffeeScript comes with a few helper functions . How to use them? flatten(Array) for
FEniCS that comes in the Ubuntu 12.04 repository does not work with Enthought EPD
Why don't more mainstream statically typed languages support function/method overloading by return type? I
I have a question related to operator overloading, and it is easy to define
I am a little confused about overloading rules, let's say there are following literal
i was asked a few java interview questions which i had no idea how
I have some basic questions on operator overloading in C++. I am reading Thinking
I've always wondered about this for a bit, but why is operator overloading not

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.