【问题标题】:Java casting error?Java 转换错误?
【发布时间】:2013-01-10 18:05:20
【问题描述】:

有人知道为什么编译器不能将值 '7' 转换为 'short' 吗?显式转换正在工作,但在传递参数时它不起作用!!!

class Alien {
    String invade(short ships) { return "a few"; }
    String invade(short... ships) { return "many"; }
}
public class Wind {
    public static void main(String [] args) {
        short temp = 7;
        System.out.println(new Alien().invade(7));
    }
}

【问题讨论】:

标签: java casting downcast


【解决方案1】:

整数 literals(我们在这里讨论的内容)是 int 值,除非它们有后缀来表明它们是 long 值。

来自section 3.10.1 of the specification

如果整数文字以 ASCII 字母 L 或 l (ell) 为后缀,则它是 long 类型;否则它是 int 类型(§4.2.1)。

但是选角没问题。也完全有可能有一个不是文字的常量。例如:

public static final short THIS_IS_A_SHORT = 7;

这里THIS_IS_A_SHORTshort 类型的常量。在这种情况下,您甚至不需要演员表,因为这是一项任务。作业以JLS section 5.2为准,其中包括:

如果变量的类型是 byte、short 或 char,并且常量表达式的值可以在变量的类型中表示,则可以使用缩小原语转换。

方法参数进行赋值转换。

【讨论】:

    【解决方案2】:

    默认情况下,整数文字在 java 中被视为 int 原始类型。

    invade(7) 查找带有int 类型参数的方法入侵。

    【讨论】:

      【解决方案3】:

      因为整数值常量是int 类型的常量。所以 7 本身就是一个 int 常量。

      统一更新: 当 Java 搜索要调用的方法时,它会搜索某种方法原型。在您的情况下,它是 invade(int) 并且没有任何具有此类参数类型的方法。只有invade(short)invade(short...)

      当您创建一个新变量时,即short temp = 7; Java“理解”7 是一个短值并且允许在不进行强制转换的情况下进行赋值。

      【讨论】:

        【解决方案4】:

        JAVA 有意写成没有短文字:

        Why are there no byte or short literals in Java?

        http://www.velocityreviews.com/forums/t149350-java-byte-literals-and-short-literals.html


        7 是 int 文字,但不能直接用作 short 文字。你只需要这个:

        System.out.println(new Alien().invade((short) 7));
        

        【讨论】:

          【解决方案5】:

          Java 将任何 Integer 字面量视为 int,并且没有任何方法带有此类参数。另一方面,java 不会进行自动向下转换,因为它可能会导致数据丢失和标记错误。

          如果您进行以下更改。尽管您在调用方法时传递了短暂的信息,但向上转换并没有什么害处,java 会为您做到这一点。产生输出“一些”

          class Alien {
              String invade(int ships) { return "a few"; }
               String invade(int... ships) { return "many"; }
           }
           public class Wind {
              public static void main(String [] args) {
                 short temp = 7;
                System.out.println(new Alien().invade(temp));
            }
          }
          

          【讨论】:

            【解决方案6】:
            class Alien {
            String invade(short ships) { return "a few"; }
            String invade(short... ships) { return "many"; }
            }
            
            
            public class Wind {
            public static void main(String [] args) {
                short temp = 7; // the variable temp get a value of 7
                System.out.println(new Alien().invade(temp)); // but 7 is not short by default, 
                                                              // so use variable temp instead of 7
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2011-12-06
              • 2019-10-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-10-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多