<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    Why int can't be passed to wrapper type Byte constructor?

    Let's take a look at the following code:

    class Program {
    public static void main (String[] args) {
    byte a = 1;
    Byte b = new Byte(primitiveByte);
    Byte c = new Byte(1);
    Byte d = new Byte((byte)1);
    System.out.print(b.byteValue() + c.byteValue());
    }
    }

    Why does the line "byte a = 1;" compile without error? and Why does the "Byte c = new Byte(1);" has a compile-time error?. Both of them use the integer literal value 1 within the range of byte. So why explicit casting "(byte)1" is needed?

    The assignment conversion (such as "byte a = 1;") is different than the method invocation conversion (such as "Byte c = new Byte(1);").  They are two subsections in Java Language Specification 3rd Edition:

    • 5.2 Assignment Conversion : Assignment conversion converts the type of an expression to the type of a specified variable.
    • 5.3 Method Invocation Conversion : Method invocation conversion is applied to each argument in a method or constructor invocation and, except in one case, performs the same conversions that assignment conversion does.

    Assignment Conversion

    Please read the above link for the detail about Assignment Conversion in Java. For our example:

    byte a = 1; 

    The assignment will not generate the compiler error because at any time if an integral value is assigned to any of the primitive types (byte, char, short), and the right hand side value is within the range of the left hand side data type, it will not generate an error. Hence as 1 is within the range of byte (-128 to 127), it is not generating compiler error. For example:

    byte a = 129; //Compile-time error

    When you assign an intergal value to any primitive type variable, the compiler will do a range checking to see if this value is in the range of the left side data type. If it is NOT within the range of data type, a compile-time error occurs.

    If the righ-hand of assignment is a variable or expression, can narrowing primitive conversions still work (5.1.3 Narrowing Primitive Conversions in Java Language Specification 3rd Edition)?

    • If the variable is a compile-time constant and the constants value is within the range of the left side data type, then it can be assigned to a narrower primitive type. Otherwise, a compiler error will occurs.
    • If the expression is a compile-time constant expression and the evaluated value is within the range of the left side data type, then it can be assigned to a narrower primitive type. Otherwise, a compiler time error will occurs.

    For example:

    int i1=1;
    final int i2 = 127;
    final int i3 = 245;
    byte b1 = i1; //Compile-time error, not final variable
    byte b2 = i2; //OK, compile time constant
    byte b3 = i3; //Compile-time error, over range
    byte b4 = (i2 + 2); //OK, compile time constant expression
    byte b5 = (i1 + 2); //Compile-time error, not final variable

    Method Invocation Conversion

    Please read the above link for the detail about the Method Invocation Conversion in Java. For our example:

    Byte c = new Byte(1);

    A compile-time error will occurs because you are passing an integer to the constructor of the Wrapper class Byte, the compiler will not do the implicit casting here.

    The method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.

    posted on 2010-07-08 15:11 gembin 閱讀(440) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JavaSE

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(6)

    隨筆分類(lèi)(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊(cè)

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    free counters
    主站蜘蛛池模板: 四虎1515hh永久久免费| 日韩精品视频在线观看免费| 视频免费在线观看| 久久精品国产亚洲一区二区| 精品一区二区三区免费视频 | 亚洲人成综合在线播放| 五月婷婷综合免费| 亚洲日韩精品无码专区加勒比☆| 四虎免费大片aⅴ入口| 国产精品亚洲精品日韩电影| 久久精品国产精品亚洲下载| 日本一道本不卡免费 | 国产午夜亚洲精品不卡免下载| 亚洲图片激情小说| 免费国产怡红院在线观看| 中文字幕免费在线看线人动作大片 | 日韩一卡2卡3卡4卡新区亚洲| 中文字幕视频免费| 亚洲国产欧美日韩精品一区二区三区| MM131亚洲国产美女久久| 色片在线免费观看| 免费无码又爽又黄又刺激网站| 亚洲熟妇成人精品一区| 国产精品亚洲аv无码播放| 免费无码A片一区二三区| 一级视频在线免费观看| 亚洲喷奶水中文字幕电影 | 日本卡1卡2卡三卡免费| 久久www免费人成看国产片| 国产精品高清免费网站| 亚洲伊人久久大香线焦| 亚洲日本国产乱码va在线观看| 亚洲AV日韩精品久久久久| 香蕉视频在线观看免费国产婷婷| 成人片黄网站色大片免费观看cn| 成年免费大片黄在线观看com| 亚洲国产综合在线| 激情综合亚洲色婷婷五月APP| 中文字幕在亚洲第一在线| 国产亚洲日韩在线三区| 亚洲av永久无码精品秋霞电影影院|