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

      <bdo id='BcDlk'></bdo><ul id='BcDlk'></ul>
      1. <legend id='BcDlk'><style id='BcDlk'><dir id='BcDlk'><q id='BcDlk'></q></dir></style></legend>

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

        在java中如果“char c = 'a'"为什么“c =

        时间:2023-07-27
          <tfoot id='csUEC'></tfoot>
            <tbody id='csUEC'></tbody>

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

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

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

                  本文介绍了在java中如果“char c = 'a'"为什么“c = c + 1"?不编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我尝试编译以下代码:

                  public static void main(String[] args){
                      for (char c = 'a'; c <='z'; c = c + 1) {
                          System.out.println(c);
                      }
                  }
                  

                  当我尝试编译时,它会抛出:

                  When I try to compile, it throws:

                  错误:(5, 41) java: 不兼容的类型: 可能的有损转换int转char

                  Error:(5, 41) java: incompatible types: possible lossy conversion from int to char

                  问题是,如果我编写 c = (char)(c + 1)c += 1c++.

                  The thing is, it does work if I write c = (char)(c + 1), c += 1 or c++.

                  我检查过,当我尝试 char c = Character.MAX_VALUE + 1; 时编译器会抛出类似的错误,但我认为 'c' 的值无法传递 'char' 类型最大值在原始函数中.

                  I checked and the compiler throws a similar error when I try char c = Character.MAX_VALUE + 1; but I see no way that the value of 'c' can pass 'char' type maximum in the original function.

                  推荐答案

                  c + 1 是一个 int,因为操作数经过 二进制数字提升:

                  c + 1 is an int, as the operands undergo binary numeric promotion:

                  • c 是一个 char
                  • 1 是一个 int
                  • c is a char
                  • 1 is an int

                  所以 c 必须扩展为 int 以使其兼容添加;并且表达式的结果是 int 类型的.

                  so c has to be widened to int to make it compatible for addition; and the result of the expression is of type int.

                  至于有效"的东西:

                  • c = (char)(c + 1) 将表达式显式转换为 char,因此其值与变量的类型兼容;
                  • c += 1 等价于 c = (char) ((c) + (1)),所以和上一个基本一样.
                  • c++ 是类型 char,所以不需要强制转换.
                  • c = (char)(c + 1) is explicitly casting the expression to char, so its value is compatible with the variable's type;
                  • c += 1 is equivalent to c = (char) ((c) + (1)), so it's basically the same as the previous one.
                  • c++ is of type char, so no cast is required.

                  这篇关于在java中如果“char c = 'a'"为什么“c = c + 1"?不编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                      • <bdo id='p5Vjl'></bdo><ul id='p5Vjl'></ul>
                          <tbody id='p5Vjl'></tbody>

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