No.1ベストアンサー
- 回答日時:
Javaの仕様でそうなると決まっているから。
自分で仕様を読みに行く、以外で分かる方法はなく、自分だけで考えようとしてもわかるはずがない。どう決まっているかというと
http://java.sun.com/docs/books/jls/third_edition …
>5.5 Casting Conversion
>Casting conversion is applied to the operand of a cast operator (§15.16): the type of the operand expression must be converted to the type explicitly named by the cast operator. Casting contexts allow the use of:
>A value of a primitive type can be cast to another primitive type by identity conversion, if the types are the same, or by a widening primitive conversion or a narrowing primitive conversion.
narrowing primitive conversionにある方法を使え、ってんで、後から見に行くと
http://java.sun.com/docs/books/jls/third_edition …
>5.1.3 Narrowing Primitive Conversions
>The following 22 specific conversions on primitive types are called the narrowing primitive conversions:
(中略)
>int to byte, short, or char
>The following 22 specific conversions on primitive types are called the narrowing primitive conversions:
>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.
符号付き整数から、汎整数型(integral type)Tへの縮小変換は、単に低い方nビットを捨てる。なお、nは型Tを表現するのに必要なビット数とする。その値の大きさに関する情報の欠落に加え、この変換は、結果の値の符号が入力値と異なるものになることがある。
http://java.sun.com/docs/books/jls/third_edition …
>4.2 Primitive Types and Values
(略)
>The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).
byteは8bitで、int型は16bit。258は00000001 00000010で表されるから
00000010だけが残る。これは整数の2だ。
だから同様に
http://ideone.com/NqWmg
ね、-1になったでしょ?「書いてある通り」
==============
ちなみに、Integral Typeとは
http://java.sun.com/docs/books/jls/third_edition …
>4.2.1 Integral Types and Values
The values of the integral types are integers in the following ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807, inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Java Java プログラム public class Main { public static void 3 2023/08/10 23:46
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- Ruby 【JAVA】数字をひし形に出力するプログラムについて 2 2022/07/11 23:32
- Java 直し方について教えて頂きたいです。 4 2022/08/13 02:11
- Java java final 1 2022/06/10 22:49
- Java javaでのプログラム(配列)について質問です. 2 2022/10/14 22:27
- Java Java 配列<選挙> 4 2023/07/31 15:07
- Java eclipse実行ができない 2 2022/07/27 04:47
- C言語・C++・C# C#テキストボックスの文字を配列にいれてその後表示する 4 2022/07/17 04:47
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
javaで質問です。 文字列2023/2...
-
ダブルクォーテーションのrepla...
-
カタカナをローマジに変換する。
-
Path型をString型へ変換する(Java)
-
int型のゼロ埋め
-
Windows-31Jからutf-8への変換...
-
秒を時分秒へ変換
-
byte[] を long,float とかに...
-
テキストファイルの文字コードM...
-
CStringをLPCTSTRへ変換
-
Visual Basic でのコードをASCI...
-
error C2440: 'static_cast' : ...
-
JAVAでSJISのコード変換
-
NULL値をゼロとして扱うには?
-
javaの日本語並び替えについて...
-
javascriptで文字列のsjis利用...
-
C言語32bitから64bitの移行につ...
-
byte型のマイナスの扱いについて
-
javaの初歩的な質問です。
-
カタカナ変換
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
ダブルクォーテーションのrepla...
-
Path型をString型へ変換する(Java)
-
javaで質問です。 文字列2023/2...
-
javascriptで文字列のsjis利用...
-
C言語32bitから64bitの移行につ...
-
int型のゼロ埋め
-
カタカナをローマジに変換する。
-
Visual Basic でのコードをASCI...
-
JavaのStringクラスに「外字」...
-
エクセルVBAで「〜」が表現でき...
-
substring バイト単位でやりたい
-
UTF-8とUnicodeの互換性
-
COBOLで半角カナを全角カナに変換
-
スマホでテキストボックスの随...
-
byte[] を long,float とかに...
-
javaで「全角が含まれているか...
-
下記問題の答えが"D"になる意味...
-
Windows-31Jからutf-8への変換...
-
[Javascript]エンターキー押下...
-
Visual Studio 6.0でビルド可能...
おすすめ情報