为什么默认为"int"而不是"byte"? [英] Why 'int' by default, but not 'byte'?

查看:104
本文介绍了为什么默认为"int"而不是"byte"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请向我解释一下,为什么,当我编写4个重载方法并调用它时,它会选择默认值为'int'而不是'byte'的方法,因为它可以存储-127的值,因此默认值更接近/更好.到128?

Explain me please, why, when I write 4 overloaded methods and call it => it chooses method with 'int' as default, but not 'byte', which is closer/better, because it can storage values from -127 to 128?

class Main {
    public static void method(short s) {
        System.out.println("short");
    }

    public static void method(byte b) {
        System.out.println("byte");
    }

    public static void method(int i) {
        System.out.println("int");
    }

    public static void method(long l) {
        System.out.println("long");
    }

    public static void main(String[] args) {
        // put your code here
        method(10);
    }
}

推荐答案

因为 Java语言规范 是这样.

Because the Java Language Specification says so.

3.10.1.整数文字说:

整数文字(如果后缀为ASCII字母 L l (类型为 int (

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

因此您的数字文字 10 的类型为 int .

So your numeric literal 10 is of type int.

这篇关于为什么默认为"int"而不是"byte"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆