Here are some notes about type conversion in the Java Silver range.
First, let's review the base numbers lightly.
――Binary number: In the computer, data is handled by 0 and 1. —— Decimal number: Expressed as a number from 0 to 9 that humans often use --Hexary numbers: 0 ~ 9 and A ~ F used for color codes etc.
Based on the above, I will review how to convert with the following code.
28 / 2 =14 remainder 0
14 / 2 =7 remainder 0
7 / 2 =3 remainder 1
3 / 2 =1 remainder 1
★☆★☆★☆★☆★☆★☆★☆★★☆★☆★☆★☆★☆★☆
28 / 2 =14 remainder 0
14 / 2 =7 remainder 0 ↑
7 / 2 =3 remainder 1 ↑
3 / 2 =1 remainder 1 ↑
Read as shown by the arrow →→→
★☆★☆★☆★☆★☆★☆★☆★★☆★☆★☆★☆★☆★☆
//Binary number
11100
For the value expressed in binary, write 1, 2, 4, 8, 16, 32, 64 .. from the right bit as shown below, and add up the parts where the bit is "1".
//In the following cases, it becomes "146".
010010010 = 146
0 1 0 0 1 0 0 1 0
256 128 64 32 16 8 4 2 1 //Find the place where the above value is "1"
128 + 16 + 2 = 146 //Sum the corresponding values.
I try to output a binary variable e in byte type, but I get a compile error for the above reason.
public static void main(String[] args) {
//Explicit cast
int a = 100;
//Convert to short type
short b = (short) a;
//byte type-128 ~Since it is a data type that handles values up to 127, an error will occur if the range is exceeded.
//Compilable for int type literals within byte
byte c = 127;
//Compile error due to int type literal that does not fit in the byte range
byte d = 128;
//Compile error because it becomes 256 in decimal number
byte e = 0b10000000;
System.out.println(e);
}
As shown below, long type literals cannot be assigned to Int type variables.
public static void main(String[] args) {
int f = 2 * 3L;
System.out.println(f);
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch:Cannot convert from long to int
As shown below, I am trying to assign a float type that can use less than that to the double type variable "g", so this also causes a compile error.
public static void main(String[] args) {
float g = 10.0;
System.out.println(g);
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch:Cannot convert from double to float
Be aware of these behaviors as a test measure.
Also, since the range that can be handled by byte and short is narrow, remember the range accurately.
--byte is -128 ~ 127 --short is 32768 to 32767
-[Thorough capture Java SE 11 Silver problem collection [1Z0-815] correspondence](https://www.amazon.co.jp/%E5%BE%B9%E5%BA%95%E6%94%BB%E7 % 95% A5Java-SE-11-Silver% E5% 95% 8F% E9% A1% 8C% E9% 9B% 86-1Z0-815/dp/4295007625/ref = pd_lpo_14_img_0/356-5238196-1895250? _Encoding = UTF8 & pd_rd_i = 4295007625 & pd_rd_r = 1c21865b-a9fe-43e8-ac01-46170e44abca & pd_rd_w = 9El0Q & pd_rd_wg = hh5Hc & pf_rd_p = 4b55d259-ebf0-4306-905a-7762d1b93740 & pf_rd_r = 72AVCH
Recommended Posts