Thinking in java 第3章 操作符
3.4 赋值
1. 基本类型存储了实际的数值,而非指向一个对象的引用,所以在赋值时,是直接将一个地方的内容复制到了另一个地方;
对象在赋值时实际是将引用从一个地方复制到另一个地方,修改一处会同时变两处。
2. 因对象之间的赋值引发的问题称为别名现象。在方法调用中传递进方法的对象也为引用。
3.7 关系操作符
1. 在对象中"=="比较的是地址值,对于自己定义的类来说,需要重写equals方法来比较两个对象的内容。
3.8 逻辑操作符
1. 在java中不能把非布尔值当作布尔值使用,比如把1当作true或把0当作false。
2. 短路是指一旦能明确无误地确定整个表达式的值,就不再计算表达式剩余的部分了。
3.9 直接常量
L | long |
D | double |
F | float |
0 | 八进制 |
0x | 十六进制 |
1. Integer和Long中有toBinaryString()方法转化为二进制。
习题
练习1:使用“简短的”和正常的打印语句来编写一个程序。
略。
练习2:创建一个包含一个float域的类,并用这个类来展示别名机制。
public class E3_2 {
private float f;
public E3_2(float f) {
this.f = f;
}
public float getF() {
return f;
}
public void setF(float f) {
this.f = f;
}
public E3_2() {
}
}
public class Main {
public static void main(String[] args) {
E3_2 e = new E3_2(1.1f);
E3_2 e2 = e;
e2.setF(1.3f);
System.out.println(e.getF());
}
}
/*
1.3
*/
练习3:创建一个包含一个float域的类,并用这个类来展示方法调用时的别名机制。
public class E3_2 {
private float f;
public E3_2(float f) {
this.f = f;
}
public float getF() {
return f;
}
public void setF(float f) {
this.f = f;
}
public E3_2() {
}
}
public class Main {
public static void main(String[] args) {
E3_2 e = new E3_2(1.1f);
change(e);
System.out.println(e.getF());
}
public static void change(E3_2 e) {
e.setF(5f);
}
}
/*
5.0
*/
练习4:编写一个计算速度的程序,它所使用的距离和时间都是常量。
public class E3_4 {
public static void main(String[] args) {
Random r = new Random();
double s = r.nextDouble();
double t = r.nextDouble();
System.out.println("s = " + s + ", t = " + t + ", v = " + (s/t));
}
}
/*
s = 0.33294763377119274, t = 0.3875726368796141, v = 0.8590586694968647
*/
练习4:创建一个名为Dog的类,它包含两个String域:name和says。在main()方法中,创建两个Dog对象,一个名为spot(它的叫声为“Ruff!”),另一个名为scruffy(它的叫声为“Wurf!”)。然后显示它们的名字和叫声。
练习5:在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象。
public class Dog {
private String name;
private String says;
public Dog(String name, String says) {
this.name = name;
this.says = says;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSays() {
return says;
}
public void setSays(String says) {
this.says = says;
}
public Dog() {
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", says='" + says + '\'' +
'}';
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog("spot","Ruff!");
Dog d2 = new Dog("scruffy","Wurf!");
System.out.println(d1);
System.out.println(d2);
Dog d3 = d1;
System.out.println(d3 == d1);
System.out.println(d3.equals(d1));
}
}
/*
Dog{name='spot', says='Ruff!'}
Dog{name='scruffy', says='Wurf!'}
true
true
*/
练习7:编写一个程序,模拟扔硬币的结果。
import java.util.Random;
public class E3_7 {
public static void main(String[] args) {
Random r = new Random();
int i = r.nextInt(1);
int j = r.nextInt(1);
System.out.println("第一次为"+func(i));
System.out.println("第二次为"+func(j));
System.out.println("两次"+(i == j?"相等":"不同"));
}
public static String func(int a) {
return a == 0?"反":"正";
}
}
/*
第一次为反
第二次为反
两次相等
*/
练习8:展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果。
public class E3_8 {
public static void main(String[] args) {
long l1 = 011;
System.out.println(""+l1+": "+Long.toBinaryString(l1));
long l2 = 0x11;
System.out.println(""+l2+": "+Long.toBinaryString(l2));
}
}
/*
9: 1001
17: 10001
*/
练习9:分别显示用float和double指数记数法所能表示的最大和最小的数字。
public class E3_9 {
public static void main(String[] args) {
System.out.println("float max = " + Float.MAX_VALUE);
System.out.println("float min = " + Float.MIN_VALUE);
System.out.println("double max = " + Double.MAX_VALUE);
System.out.println("double min = " + Double.MIN_VALUE);
}
}
/*
float max = 3.4028235E38
float min = 1.4E-45
double max = 1.7976931348623157E308
double min = 4.9E-324
*/
练习10-13 位运算
略。
练习14:编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结果打印出来。做==和!=比较的同时,用equals()做测试。在main()里面用几个不同的字符串对象调用这个方法。
public class E3_14 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
func(s1,s2);
}
public static void func(String a, String b) {
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a.equals(b));
}
}
/*
true
false
true
*/