誰もがすでに述べたように、+ =は暗黙のキャストを持っています。それを説明するのを助けるために、私は後に書いたこのアプリを使って、これらのタイプの質問に最適です。 http://javabytes.herokuapp.com/
とその意味のテーブル:あなたが生産されています実際のバイトコードをチェックアウトすることができますので、それはオンライン逆アセンブラです http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
それでは、いくつかの簡単なJavaコードからバイトコードを見てみましょう:
int i = 5;
long j = 8;
i += j;
逆アセンブルされたコード。私のコメントには//が前に付きます。
Code:
0: iconst_5 //load int 5 onto stack
1: istore_0 //store int value into variable 0 (we called it i)
2: ldc2_w #2; //long 8l
//load long 8 value onto stack. Note the long 8l above
//is not my comment but how the disassembled code displays
//the value long 8 being used with the ldc2_w instruction
5: lstore_1 //store long value into variable 1 (we called it j)
6: iload_0 //load int value from variable 0
7: i2l //convert int into a long. At this point we have 5 long
8: lload_1 //load value from variable 1
9: ladd //add the two values together. We are adding two longs
//so it's no problem
10: l2i //THIS IS THE MAGIC. This converts the sum back to an int
11: istore_0 //store in variable 0 (we called it i)
あなたはそれをさらに読むためにいくつかのリンクを提供できますか?ありがとうございます – hhafez
私は、 "深い"理由は、それが割り当て中に許可されていないためだと思う:バイト= intは許可されていないint = doubleも。単純なバイトaを行います。 a + = 2;コンパイルに失敗すると、人々はjavaで靴を投げるだろう。しかし、私はまだそのキャストなしで動作するように余分なルールが好きだったでしょう:( –
私は深い理由は確かではありませんが、Java言語仕様は明示的に暗黙のキャストを定義します:http://java.sun.com/ docs/books/jls/second_edition/html/expressions.doc.html#5304 – DefLog