bytetest.java
来自「JAVA 2入门经典 练习答案」· Java 代码 · 共 40 行
JAVA
40 行
//Chapter 2, Exercise 1
public class ByteTest { // Ensure classes/variables have distinctive names.
public static void main(String args[]) {
// Initialize variable data of type byte to 1:
byte data = 1;
data *= 2; // Multiply by 2: 1st time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 2nd time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 3rd time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 4th time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 5th time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 6th time
System.out.println("data = " + data);
data *= 2; // Multiply by 2: 7th time
System.out.println("data = " + data);
// Value of data is now out of the byte range, which is -128 to +127
// At this point we have stored 128 in data - in binary 1000 0000
// which corresponds to -128 as a value of type byte.
data *= 2; // Multiply by 2: 8th time
System.out.println("data = " + data);
// Don't forget that these integer calculations will be done
// with 32 bits precision. The result is then stored in data taking the
// low-order 8 bits of the result as the value to be stored.
// Since we multiply the -128 value by 2, the result is -256. In binary the
// low-order 8 bits for this value are zero.
// This calculation would normally be done in a loop (subject covered in chapter 3).
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?