<legend id='7cnlu'><style id='7cnlu'><dir id='7cnlu'><q id='7cnlu'></q></dir></style></legend>
    <tfoot id='7cnlu'></tfoot>
    <i id='7cnlu'><tr id='7cnlu'><dt id='7cnlu'><q id='7cnlu'><span id='7cnlu'><b id='7cnlu'><form id='7cnlu'><ins id='7cnlu'></ins><ul id='7cnlu'></ul><sub id='7cnlu'></sub></form><legend id='7cnlu'></legend><bdo id='7cnlu'><pre id='7cnlu'><center id='7cnlu'></center></pre></bdo></b><th id='7cnlu'></th></span></q></dt></tr></i><div id='7cnlu'><tfoot id='7cnlu'></tfoot><dl id='7cnlu'><fieldset id='7cnlu'></fieldset></dl></div>
      • <bdo id='7cnlu'></bdo><ul id='7cnlu'></ul>
    1. <small id='7cnlu'></small><noframes id='7cnlu'>

    2. Java char 也是 int 吗?

      时间:2023-07-27
      <tfoot id='6mv88'></tfoot>
        • <bdo id='6mv88'></bdo><ul id='6mv88'></ul>

              <tbody id='6mv88'></tbody>

              <small id='6mv88'></small><noframes id='6mv88'>

                <i id='6mv88'><tr id='6mv88'><dt id='6mv88'><q id='6mv88'><span id='6mv88'><b id='6mv88'><form id='6mv88'><ins id='6mv88'></ins><ul id='6mv88'></ul><sub id='6mv88'></sub></form><legend id='6mv88'></legend><bdo id='6mv88'><pre id='6mv88'><center id='6mv88'></center></pre></bdo></b><th id='6mv88'></th></span></q></dt></tr></i><div id='6mv88'><tfoot id='6mv88'></tfoot><dl id='6mv88'><fieldset id='6mv88'></fieldset></dl></div>
                <legend id='6mv88'><style id='6mv88'><dir id='6mv88'><q id='6mv88'></q></dir></style></legend>
                本文介绍了Java char 也是 int 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试为课程完成一些代码:

                I was trying to get some code done for class:

                public int getValue(char value) {
                    if (value == 'y') return this.y;
                    else if (value == 'x') return this.x;
                

                由于我最终可能无法返回任何东西,所以它告诉我最后要这样做:

                Since I might not be able to return anything in the end, it told me to do this at the end:

                return value;
                

                这让我很惊讶,因为该方法的返回类型是 int 类型.然而,它告诉我返回一个 char!我正在使用 eclipse,并且习惯了无穷无尽的警告和东西,这是一个重大的惊喜.

                This surprised me because the return type for the method was of type int. Yet, it was telling me to return a char! I'm using eclipse, and accustomed to the endless number of warnings and stuff, this was a major surprise.

                那么,char 真的是 int 吗?为什么会这样?

                So, is a char really an int? Why is this happening?

                推荐答案

                Java 语言规范 状态

                当带有 Expression 的 return 语句出现在方法中时声明,Expression 必须 可分配(第 5.2 节) 到声明的方法的返回类型,或发生编译时错误.

                When a return statement with an Expression appears in a method declaration, the Expression must be assignable (§5.2) to the declared return type of the method, or a compile-time error occurs.

                控制一个值是否可分配给另一个值的规则定义为

                where the rules governing whether one value is assignable to another is defined as

                赋值上下文允许使用以下之一:

                Assignment contexts allow the use of one of the following:

                • 扩大原语转换(§5.1.2)

                原始类型的 19 种特定转换称为扩展原始转换:

                19 specific conversions on primitive types are called the widening primitive conversions:

                • charintlongfloat 或 `double
                • char to int, long, float, or `double

                最后

                扩展原语转换不会丢失有关在下列情况下,数值的总大小,其中数值被完全保留:[...]

                A widening primitive conversion does not lose information about the overall magnitude of a numeric value in the following cases, where the numeric value is preserved exactly: [...]

                char 到整数类型 T 的扩展转换将零扩展char 值的表示以填充更宽的格式.

                A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.

                简而言之,作为 return 语句表达式的 char 值可通过扩展原语转换分配给 int 的返回类型.

                In short, a char value as the expression of a return statement is assignable to a return type of int through widening primitive conversion.

                这篇关于Java char 也是 int 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在Java中将十六进制字符串转换为字节 下一篇:将 long 转换为字节数组并将其添加到另一个数组

                相关文章

                最新文章

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

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

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

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