一、创建一个包含一个float域的类,并用这个类来展示别名机制。
public class Main { public static void main(String[] args) { Test t1 = new Test(); Test t2 = t1; t1.f = 1.1F; System.out.println("1. t1.f:" + t1.f + ", t2.f:" + t2.f); t2 = new Test(); System.out.println("1. t1.f:" + t1.f + ", t2.f:" + t2.f); } } class Test { float f; }
返回:
1. t1.f:1.1, t2.f:1.1 2. t1.f:1.1, t2.f:0.0
二、创建一个包含一个float域的类,并用这个类来展示方法调用时的别名机制
public class Main { private static void test(Test test) { test.f = 2.2F; } public static void main(String[] args) { Test t = new Test(); t.f = 1.1F; System.out.println("1. t.f:" + t.f); test(t); System.out.println("2. t.f:" + t.f); } } class Test { float f; }
返回:
1. t.f:1.1 2. t.f:2.2
三、编写一个计算速度的程序,它所使用的距离和时间都是常量
public class Main { public static void main(String[] args) { float d = 100.10F; // 距离100米 float s = 9.5F; // 速度9.5m/s float t = d/s; // 时间 System.out.println("距离"+d+" 速度"+s+" 需要"+t+"时间到达。"); } }
返回:距离100.1 速度9.5 需要d%10.536842时间到达。
四、创建一个名为Dog的类,它包含两个String域:name和says。在main()中,创建两个Dog对象,一个名为spot(叫声为“Ruff!”),另一个名为scruffy(叫声为“Wurf!”)。然后显示它们的名字和叫声。
public class Main { public static void main(String[] args) { Dog d1 = new Dog(); Dog d2 = new Dog(); d1.name="spot"; d2.name="scruffy"; d1.says="Ruff!"; d2.says="Wurf!"; System.out.println(d1.name+"的叫声是"+d1.says); System.out.println(d2.name+"的叫声是"+d2.says); } } class Dog{ String name; String says; }
返回:
spot的叫声是Ruff! scruffy的叫声是Wurf!
五、在“四”的基础上,创建一个新的Dog索引,并对其赋值为spot对象。测试用==和equals()来比较所有引用的结果
public class Main { public static void main(String[] args) { Dog d1 = new Dog(); Dog d3 = d1; d1.name="spot"; d1.says="Ruff!"; System.out.println("d1==d3?"+(d1==d3)); System.out.println("d1.equals(d3)?"+(d1.equals(d3))); } } class Dog{ String name; String says; }
返回:
d1==d3?true d1.equals(d3)?true
六、编写一个程序,模拟扔硬币的结果。
public class Main { public static void main(String[] args) { Random r = new Random(); for (int i = 0; i < 10; i++) { int num = r.nextInt(10); if (num % 2 == 0) { System.out.print("正面"); } else { System.out.print("反面"); } if (i < (10 - 1)) { System.out.print(","); } } } }
返回:
正面,正面,反面,正面,反面,正面,正面,反面,反面,反面
七、展示用十六进制和八进制记数法来操作long,用Long.toBinaryString()来显示结果。
public class Main { public static void main(String[] args) { Long aLong1 = 0x6fL; Long aLong2 = 077L; System.out.println("十进制:aLong1:"+aLong1+", aLong2:"+aLong2); System.out.println("二进制:aLong1:"+Long.toBinaryString(aLong1)+", aLong2:"+Long.toBinaryString(aLong2)); } }
返回:
十进制:aLong1:111, aLong2:63 二进制:aLong1:1101111, aLong2:111111
八、分别显示用float和double指数记数法所能表示的最大值和最小值
public class Main { public static void main(String[] args) { float minF = Float.MIN_VALUE; float maxF = Float.MAX_VALUE; double minD = Double.MIN_VALUE; double maxD = Double.MAX_VALUE; System.out.println("minF:"+minF+", maxF:"+maxF); System.out.println("minD:"+minD+", maxD:"+maxD); } }
返回:
minF:1.4E-45, maxF:3.4028235E38 minD:4.9E-324, maxD:1.7976931348623157E308
九、编写一个具有两个常量值得程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,但是其最低有效位为1(使用十六进制常量来表示是最简单的方法)。取这两个值,用按位操作符以所有可能的方式结合运算它们,然后用Integer.toBinaryString() 显示。
public class Main { public static void main(String[] args) { int num1 = 0xAA; // 10101010 int num2 = 0x55; // 1010101 System.out.println("1. 10101010 & 1010101: " + Integer.toBinaryString(num1 & num2)); System.out.println("2. 10101010 | 1010101: " + Integer.toBinaryString(num1 | num2)); System.out.println("3. 10101010 ^ 1010101: " + Integer.toBinaryString(num1 ^ num2)); System.out.println("4. ~10101010: " + Integer.toBinaryString(~num1)); num2 &= num1; System.out.println("5. &= 10101010: " + Integer.toBinaryString(num2)); num2 = 0x55; num2 |= num1; System.out.println("6. |= 10101010: " + Integer.toBinaryString(num2)); num2 = 0x55; num2 ^= num1; System.out.println("7. ^= 10101010: " + Integer.toBinaryString(num2)); } }
返回:
1. 10101010 & 1010101: 0 2. 10101010 | 1010101: 11111111 3. 10101010 ^ 1010101: 11111111 4. ~10101010: 11111111111111111111111101010101 5. &= 10101010: 0 6. |= 10101010: 11111111 7. ^= 10101010: 11111111
十、以一个最高有效位为1的二进制数字开始(使用十六进制常量),用有符号右移操作对其进行右移,直至所有的二进制位都被移出为止,每移一位都要使用Integer.toBinaryString()显示结果
public class Main { public static void main(String[] args) { int num = 0x7f; // 11111111 System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); num >>= 1; System.out.println(Integer.toBinaryString(num)); } }
返回:
1111111 111111 11111 1111 111 11 1 0
十一、以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有二进制位都被移出为止,每移一位都要使用Integer.toBinaryString()显示结果
public class Main { public static void main(String[] args) { int num = 0x7f; // 11111111 System.out.println(Integer.toBinaryString(num)); num <<= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); num >>>= 1; System.out.println(Integer.toBinaryString(num)); } }
返回:
1111111 11111110 1111111 111111 11111 1111 111 11 1 0
十二、编写一个方法,它以二进制形式显示char类型的值。使用多个不同的字符来展示它
public class Main { public static void main(String[] args) { char a = 'A'; char b = 'B'; char c = 'C'; char z = 'Z'; System.out.println("A: "+Integer.toBinaryString(a)); System.out.println("B: "+Integer.toBinaryString(b)); System.out.println("C: "+Integer.toBinaryString(c)); System.out.println("Z: "+Integer.toBinaryString(z)); } }
返回:
A: 1000001 B: 1000010 C: 1000011 Z: 1011010