第10章+早期(编译器)优化+10.3 Java语法糖的味道
目录
10.3 Java语法糖的味道
10.3.1 泛型与类型擦除
代码清单10-2, 泛型擦除前的例子
/*
* 代码清单10-2, 泛型擦除前的例子
*/
package cn.chapter10;
import java.util.HashMap;
import java.util.Map;
public class Solution10_2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("hello", "你好");
map.put("how are you?", "吃了没?");
System.out.println(map.get("hello"));
System.out.println(map.get("how are you?"));
}
}
运行结果:
你好
吃了没?
代码清单10-3, 泛型擦除后的例子
/*
* 10-3, 泛型擦除后的例子
*/
package cn.chapter10;
import java.util.HashMap;
import java.util.Map;
public class Solution10_3 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("hello", "你好");
map.put("how are you?", "吃了没?");
System.out.println((String)map.get("hello"));//我试了下加不加(String)输出没有影响,书上为什么加???
System.out.println((String)map.get("how are you?"));
}
}
输出和10-2一样
代码清单10-4, 当泛型遇见重载1
/*
* 代码清单10-4, 当泛型遇见重载1
*/
package cn.chapter10;
import java.util.List;
public class GenericTypes {
public static void method(List<String> list){
System.out.println("invoke method(List<String>)");
}
public static void method(List<Integer> list){
System.out.println("invoke method(List<Integer> list)");
}
}
代码清单10-5, 当泛型遇见重载2(书中10-5可以被编译运行,但是我试了下不行,可能是因为我使用的是myeclise10,jdk是1.7,这是一个猜测,不确定)
/*
* 代码清单10-5, 当泛型遇见重载2
*/
package cn.chapter10;
import java.util.ArrayList;
import java.util.List;
public class GenericTypes_2 {
public static String method(List<String> list){
System.out.println("invoke method(List<String> list)");
return "";
}
public static int method(List<Integer> list){
System.out.println("invoke method(List<Integer> list)");
return 1;
}
public static void main(String[] args) {
method(new ArrayList<String>());
method(new ArrayList<Integer>());
}
}
10.3.2 自动装箱、拆箱与遍历循环(foreach)
代码清单10-6, 自动装箱、拆箱与遍历循环
/*
* 代码清单10-6, 自动装箱、拆箱与遍历循环
*/
package cn.chapter10;
import java.util.Arrays;
import java.util.List;
public class Solution10_6 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
//能让上面这句代码进一步简写撑List<Integer> list = [1,2,3,4];
int sum = 0;
for(int i : list){
sum += i;
}
System.out.println(sum);
}
}
运行结果:
10
代码清单10-7, 自动装箱、拆箱与遍历循环编译之后
/*
* 代码清单10-7, 自动装箱、拆箱与遍历循环编译之后
*/
package cn.chapter10;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Solution10_7 {
public static void main(String[] args) {
List list = Arrays.asList(new Integer[]{
Integer.valueOf(1),//Integer.valueOf(1)自动装箱
Integer.valueOf(2),
Integer.valueOf(3),
Integer.valueOf(4)
});
int sum = 0;
for(Iterator localIterator = list.iterator();localIterator.hasNext();){//遍历循环
// (Integer) 泛型
int i = ((Integer) localIterator.next()).intValue();//intValue()自动拆箱
sum += i;
}
System.out.println(sum);
}
}
运行结果:
10
代码清单10-8, 自动装箱的陷阱
/*
* 代码清单10-8, 自动装箱的陷阱
*/
package cn.chapter10;
public class Solution10_8 {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c == (a+b));
System.out.println(c.equals(a+b));
System.out.println(g == a+b);
System.out.println(g.equals(a+b));
}
}
运行结果:(运行结果我没太看懂!!!)
true
false(为什么???)
true
true(为什么???)
true(为什么???)
false(为什么???)
== 和equals()两种比较方法,在使用时要注意:
1、如果测试两个简单类型的数值是否相等,则一定要用“==”来比较;
2、如果要比较两个引用变量(例如Integer)对象的值是否相等,则要用对象的equals()方法进行比较;
3、如果需要比较两个引用变量是否指向同一对象,则使用“==”来进行比较;
还有,对于自定义的类,应该根据情况覆盖其父类或Object类中的equals()方法,否则默认的equals()方法功能与“==”相同。