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

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

    1. <small id='nkHSw'></small><noframes id='nkHSw'>

        <tfoot id='nkHSw'></tfoot>
          <bdo id='nkHSw'></bdo><ul id='nkHSw'></ul>
      1. Java中的整数如何转换为字节?

        时间:2023-10-01

          <bdo id='iQCm2'></bdo><ul id='iQCm2'></ul>

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

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

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

                  <tfoot id='iQCm2'></tfoot>
                  本文介绍了Java中的整数如何转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道 Java 不允许无符号类型,所以我想知道它是如何将整数转换为字节的.假设我有一个值为 255 的整数 a 并将整数转换为一个字节.该值是否以字节 11111111 表示?换句话说,该值是更多地被视为一个有符号的 8 位整数,还是直接复制整数的最后 8 位?

                  I know Java doesn't allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and I cast the integer to a byte. Is the value represented in the byte 11111111? In other words, is the value treated more as a signed 8 bit integer, or does it just directly copy the last 8 bits of the integer?

                  推荐答案

                  这叫做 缩小基元转换.根据规范:

                  有符号整数到整数类型的窄化转换T 只丢弃除n 个最低位之外的所有位,其中n 是用于表示类型 T 的位数.除了可能丢失有关数值大小的信息外,这还可能导致结果值的符号与输入值的符号不同.

                  A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

                  所以这是您列出的第二个选项(直接复制最后 8 位).

                  So it's the second option you listed (directly copying the last 8 bits).

                  我不确定你是否知道有符号整数值是如何表示的,所以为了安全起见,我会指出字节值 1111 1111 在 二的补码 系统(Java 使用).

                  I am unsure from your question whether or not you are aware of how signed integral values are represented, so just to be safe I'll point out that the byte value 1111 1111 is equal to -1 in the two's complement system (which Java uses).

                  这篇关于Java中的整数如何转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“int 不能被取消引用";在爪哇 下一篇:如何通过引用正确传递 Integer 类?

                  相关文章

                  最新文章

                  1. <legend id='PRs6b'><style id='PRs6b'><dir id='PRs6b'><q id='PRs6b'></q></dir></style></legend>
                    • <bdo id='PRs6b'></bdo><ul id='PRs6b'></ul>
                  2. <tfoot id='PRs6b'></tfoot>

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

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