0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

数値の型が扱える範囲 Javaの問題をTypeScriptにしてみる 3-3

Posted at

次のプログラムの中からコンパイルエラーにならないものを選びなさい

A. byte a = 0b10000000;
B. short b = 128 + 128;
C. int c = 2 * 3L;
D. float d = 10.0;

答えは、Bです。

byte型は-128〜127の範囲の値しか扱えません。0b10000000は128のためbyteの範囲を超えています。よってコンパイルエラーとなります。
Lがつくとlong型のためint型には代入できないためコンパイルエラーとなります。
実数はdoubleで扱われます。floatはdoubleよりも範囲が狭いためキャストを行わないと代入できません。よってコンパイルエラーになります。

これをTypeScriptにすると、

A.var a:number = 0b10000000;
B.var b:number = 128 + 128;
C.var c:number = 2 * 3;
D.var d:number = 10.0;

このようになります。TypeScriptには数字を扱う型は、numberしかありません。
number型はjavaのdouble型と同じ桁数を扱えます。そのため全てのコンパイルが通ります。
Aの2進数は文字列で表現してparseInt("10000000",2);というように変換することも見かけます。バイトコードは文字列で受け取ることが多からかもしれません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?