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

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

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

      <tfoot id='IZHxQ'></tfoot>
    1. <legend id='IZHxQ'><style id='IZHxQ'><dir id='IZHxQ'><q id='IZHxQ'></q></dir></style></legend>
    2. 为什么我需要在 byte 和 short 上显式转换 char 原语

      时间:2023-07-28
        <bdo id='oBB36'></bdo><ul id='oBB36'></ul>
          <i id='oBB36'><tr id='oBB36'><dt id='oBB36'><q id='oBB36'><span id='oBB36'><b id='oBB36'><form id='oBB36'><ins id='oBB36'></ins><ul id='oBB36'></ul><sub id='oBB36'></sub></form><legend id='oBB36'></legend><bdo id='oBB36'><pre id='oBB36'><center id='oBB36'></center></pre></bdo></b><th id='oBB36'></th></span></q></dt></tr></i><div id='oBB36'><tfoot id='oBB36'></tfoot><dl id='oBB36'><fieldset id='oBB36'></fieldset></dl></div>
            <tbody id='oBB36'></tbody>

              <tfoot id='oBB36'></tfoot>
            • <small id='oBB36'></small><noframes id='oBB36'>

              <legend id='oBB36'><style id='oBB36'><dir id='oBB36'><q id='oBB36'></q></dir></style></legend>
              • 本文介绍了为什么我需要在 byte 和 short 上显式转换 char 原语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                关于原语:当我从较小的类型转换为较大的类型时,转换是隐式的,当我从较大的类型转换为较小的类型时,我需要显式地转换原语,由于数据丢失,这很明显.但有些东西我不明白.当我在某些情况下(字节和短)向上或向下转换为 char 时,我总是需要在两个方向上显式转换,尽管 byte(8 位)适合 char(16 位)?

                Concerning primitives: When I cast from smaller to bigger types, the casts are implicit, when I cast from bigger to smaller types, I need to explicitly cast the primitives, that's clear due to loss of data. But there is something I don't get. When I up- or downcast to char in some cases (byte and short), I always need to explicitly cast in both directions although byte (8bit) fits into char (16bit)?

                (另请参见 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

                看我的例子...

                public class CastingTest
                {
                    public static void main(String[] args)
                    {
                        //casting from smaller to bigger types
                        short c = 13;
                        int d = c;
                
                        byte f = 34;
                        short g = f;
                
                        byte h = 20;
                        long i = h;
                
                        byte var03 = 6;
                        double var04 = var03;   
                
                        //casting from bigger to smaller types
                        int j = 12;
                        short k = (short)j;
                
                        long m = 56;
                        int n = (int)m;
                
                        double o = 19;
                        short p = (short)o;
                
                        //not possible without explicit cast, but why?
                        byte var01 = 3;
                        char var02 = (char)var01;
                
                        short var05 = 5;
                        char var06 = (char)var05;
                
                        char var07 = 'k';
                        short var08 = (short)var07;
                    }
                }
                

                推荐答案

                char 是 Java 唯一的 unsigned 类型,因此它的取值范围不完全包含任何其他 Java 类型的值范围.

                char is Java's only unsigned type, therefore its value range does not fully contain any other Java type's value range.

                对于目标类型范围未完全覆盖源类型范围的任何转换,您必须使用显式转换运算符.

                You must use an explicit cast operator for any conversion where the target type's range doesn't fully cover the source type's range.

                这篇关于为什么我需要在 byte 和 short 上显式转换 char 原语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Java 中检查字符是否为元音的最佳方法是什么 下一篇:读取文本文件并存储出现的每个字符

                相关文章

                最新文章

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

              • <legend id='clSFX'><style id='clSFX'><dir id='clSFX'><q id='clSFX'></q></dir></style></legend>

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

                    <tfoot id='clSFX'></tfoot>