Search code examples
compiler-errorsparameter-passingcompile-timeshort

Error when I pass short data type in the acutal parameters of my method


public static void example(short a, int b, int c){
     System.out.println("example");
}
public static void main(String[]args){
example(1,2,3); /*I'm getting a compile time error "the method   
                example(short,int,int) is not applicable for the 
                arguments(int, int, int)*/

I did fix it by declaring all values as int data types in the formal parameters, but isn't 1 a short data type?? I just want to know why I couldn't pass 1 in the actual parameters. I'm using Java btw


Solution

  • You don't say what programming language this is, so it is impossible to say definitively, however, it is clear from the error message that this:

    isn't 1 a short data type??

    is false.

    From the error message, it is easy to see that, no, 1 is not a literal denoting a short, it is in fact a literal denoting an int.

    Again, you don't say what language you are using, so it is impossible to tell with absolute certainty whether that is the case or not, but from the error message, it certainly appears that way. If you look at Java, for example, the Java Language Specification clearly says:

    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).

    So, if the language in your code snippet were Java, then 1 would be an int.