<legend id='BKb4o'><style id='BKb4o'><dir id='BKb4o'><q id='BKb4o'></q></dir></style></legend>
  • <tfoot id='BKb4o'></tfoot>

        <i id='BKb4o'><tr id='BKb4o'><dt id='BKb4o'><q id='BKb4o'><span id='BKb4o'><b id='BKb4o'><form id='BKb4o'><ins id='BKb4o'></ins><ul id='BKb4o'></ul><sub id='BKb4o'></sub></form><legend id='BKb4o'></legend><bdo id='BKb4o'><pre id='BKb4o'><center id='BKb4o'></center></pre></bdo></b><th id='BKb4o'></th></span></q></dt></tr></i><div id='BKb4o'><tfoot id='BKb4o'></tfoot><dl id='BKb4o'><fieldset id='BKb4o'></fieldset></dl></div>

        <small id='BKb4o'></small><noframes id='BKb4o'>

        • <bdo id='BKb4o'></bdo><ul id='BKb4o'></ul>

        为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节?

        Why can not I add two bytes and get an int and I can add two final bytes get a byte?(为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节?)

              <tbody id='p7Cpu'></tbody>
            <legend id='p7Cpu'><style id='p7Cpu'><dir id='p7Cpu'><q id='p7Cpu'></q></dir></style></legend>

              <tfoot id='p7Cpu'></tfoot>
                <bdo id='p7Cpu'></bdo><ul id='p7Cpu'></ul>
                • <i id='p7Cpu'><tr id='p7Cpu'><dt id='p7Cpu'><q id='p7Cpu'><span id='p7Cpu'><b id='p7Cpu'><form id='p7Cpu'><ins id='p7Cpu'></ins><ul id='p7Cpu'></ul><sub id='p7Cpu'></sub></form><legend id='p7Cpu'></legend><bdo id='p7Cpu'><pre id='p7Cpu'><center id='p7Cpu'></center></pre></bdo></b><th id='p7Cpu'></th></span></q></dt></tr></i><div id='p7Cpu'><tfoot id='p7Cpu'></tfoot><dl id='p7Cpu'><fieldset id='p7Cpu'></fieldset></dl></div>

                  <small id='p7Cpu'></small><noframes id='p7Cpu'>

                  本文介绍了为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  public class Java{
                      public static void main(String[] args){
                          final byte x = 1;
                          final byte y = 2;
                          byte z = x + y;//ok
                          System.out.println(z);
                  
                          byte a = 1;
                          byte b = 2;
                          byte c = a + b; //Compiler error
                          System.out.println(c);
                      }
                  }
                  

                  如果涉及任何 int 大小或更小的表达式的结果始终是 int,即使两个字节的总和适合一个字节.

                  If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte.

                  为什么当我们添加两个适合一个字节的最终字节时会发生这种情况?没有编译器错误.

                  推荐答案

                  来自JLS 5.2 赋值转换

                  此外,如果表达式是 byte、short、char 或 int 类型的常量表达式(第 15.28 节):- 如果类型为变量是 byte、short 或 char,以及常量的值表达式可以用变量的类型来表示.

                  In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: - A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

                  简而言之,表达式的值(在编译时是已知的,因为它是一个常量表达式)可以用字节变量的类型来表示.

                  In short the value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable that is byte.

                  考虑你的表达方式

                   final byte x = 1;
                   final byte y = 2;
                   byte z = x + y;//This is constant expression and value is known at compile time
                  

                  因此,当求和适合字节时,它不会引发编译错误.

                  So as summation fits into byte it does not raise an compilation error.

                  如果你这样做了

                  final byte x = 100;
                  final byte y = 100;
                  byte z = x + y;// Compilation error it no longer fits in byte
                  

                  这篇关于为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                    <tbody id='RDqKU'></tbody>

                        <legend id='RDqKU'><style id='RDqKU'><dir id='RDqKU'><q id='RDqKU'></q></dir></style></legend>

                        <tfoot id='RDqKU'></tfoot>

                        1. <i id='RDqKU'><tr id='RDqKU'><dt id='RDqKU'><q id='RDqKU'><span id='RDqKU'><b id='RDqKU'><form id='RDqKU'><ins id='RDqKU'></ins><ul id='RDqKU'></ul><sub id='RDqKU'></sub></form><legend id='RDqKU'></legend><bdo id='RDqKU'><pre id='RDqKU'><center id='RDqKU'></center></pre></bdo></b><th id='RDqKU'></th></span></q></dt></tr></i><div id='RDqKU'><tfoot id='RDqKU'></tfoot><dl id='RDqKU'><fieldset id='RDqKU'></fieldset></dl></div>
                            <bdo id='RDqKU'></bdo><ul id='RDqKU'></ul>

                            <small id='RDqKU'></small><noframes id='RDqKU'>