短至int自动升级 [英] Short to int automatic promotion

查看:60
本文介绍了短至int自动升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Yikes1 {
    public static void go(Long n) {
        System.out.println("Long "); // printed
    }

    public static void go(Short n) {
        System.out.println("Short "); // don't know why isn't this printed
    }
    public static void go(int n) {
        System.out.println("int "); // printed
    }

    public static void main(String [] args) {
        short y = 6;
        long z = 7;
        go(y); 
        go(z);
    }
}

在打印输出之前,如何将short值转换为int?

How does the short value got converted to int before it prints the output?

我认为加宽仅在dataype-short不可用时起作用,因此它会寻找下一个可用的数据类型(在这种情况下为int),但是这里定义了short数据类型,那么如何自动提升呢?

I thought widening works only when the dataype-short is not available so it looks for next available data type which is int in this case but however short datatype is defined here so how come the automatic promotion happening?

推荐答案

绑定顺序如下:

  1. 完全匹配(例如 int-> int )
  2. 促销(例如 int-> long )
  3. 自动装箱/拆箱(例如 int->整数)
  4. 变量(例如 int-> int ... )

这篇关于短至int自动升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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