今日总结
1.JAVA的基本运行单位是类
2、类由属性,方法,构造方法,内部类,块;
2.不同的枚举值是不同的对象,通过 “==” 比较只有在引用同一个枚举值时才返回true。在这个例子中,s和t分别引用不同的枚举值,所以s == t为false;而s和通过字符串转换得到的u都引用SMALL枚举值,所以s == u为true。
枚举类型不是原始数据类型,而是一种特殊的引用类型。通过s.getClass().isPrimitive()返回false确认了这一点。
可以通过valueOf方法从字符串中转换得到对应的枚举值,方便在某些情况下根据字符串来获取特定的枚举常量。
可以使用values方法遍历枚举类型的所有值,这在需要对所有枚举常量进行统一处理时非常有用。
3.public class NumberRepresentationTest {
public static void main(String[] args) {
int positiveNumber = 5;
int negativeNumber = -5;
System.out.println("Positive number (5):");
System.out.println("Binary representation (original): " + Integer.toBinaryString(positiveNumber));
System.out.println("Negative number (-5):");
System.out.println("Binary representation (original): " + Integer.toBinaryString(negativeNumber));
// Calculate the 2's complement (if it's the representation used)
int twosComplementNegative = ~negativeNumber + 1;
System.out.println("Binary representation (2's complement calculated): " + Integer.toBinaryString(twosComplementNegative));
// Calculate the 1's complement (if it's the representation used)
int onesComplementNegative = ~negativeNumber;
System.out.println("Binary representation (1's complement calculated): " + Integer.toBinaryString(onesComplementNegative));
}
}
在 Java 中,整数采用补码表示。通过这个程序可以观察到,对于负数,其在内存中的存储形式与通过取反加一得到的补码形式一致,而与原码和反码不同。
原码是符号位加上真值的绝对值,即最高位为符号位,0 表示正,1 表示负,其余位表示数值的大小。
反码是正数的反码与其原码相同;负数的反码是对其原码逐位取反,但符号位除外。
补码是正数的补码与其原码相同;负数的补码是在其反码的末位加 1。
4.public class VariableScopeExample {
public static void main(String[] args) {
int num = 10;
System.out.println("外部变量 num 的值:" + num);
{
int num = 20;
System.out.println("内部块中变量 num 的值:" + num);
}
System.out.println("外部变量 num 的值:" + num);
}
}
结果是
外部变量 num 的值:10
内部块中变量 num 的值:10
外部变量 num 的值:10
5.内容主要是关于 Java 中的类型转换,通过一张图展示了不同数据类型之间的转换关系,并提出动手动脑的问题,即查看 Java 中每个数据类型所占的位数和表示数值的范围,进而得出结论。具体提到了 byte、short、int、long 之间的转换无精度损失,而向 float、double 转换时有精度损失。

浙公网安备 33010602011771号