不积跬步,无以至千里;不积小流,无以成江海。

java语法糖的味道

JAVA 康康 1445℃ 0评论

语法糖(Syntactic Sugar):也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言的功能没有影响,但是更方便程序员使用。通常来说,使用语法糖能够增加程序的可读性,减少程序代码出错的机会。

Java中的语法糖包括但不限于以下10颗:泛型与类型擦除、自动装箱和拆箱、遍历循环、变长参数、条件编译、内部类、枚举类、断言语句、对枚举和字符串的switch支持、在try语句中定义和关闭资源。

1.泛型与类型擦除

源代码:

  1. public static void main(String[] args){
  2. Map<String,Integer> map = new HashMap<String,Integer>();
  3. map.put("hello" , 1);
  4. map.put("world" , 2);
  5. System.out.println(map.get("hello"));
  6. System.out.println(map.get("world"));
  7. }

编译后的class文件:

  1. public static void main(String[] paramArrayOfString)
  2. {
  3. HashMap localHashMap = new HashMap();
  4. localHashMap.put("hello", Integer.valueOf(1));
  5. localHashMap.put("world", Integer.valueOf(2));
  6. System.out.println(localHashMap.get("hello"));
  7. System.out.println(localHashMap.get("world"));
  8. }

在编译后的字节码中,已经被替换为原来的原生类型了,所以有人说java的泛型是伪泛型

2.自动拆装箱

源代码:

  1. public static void main(String[] args) {
  2. Integer a = 1;
  3. Integer b = 2;
  4. Integer c = 3;
  5. Integer d = 3;
  6. Integer e = 321;
  7. Integer f = 321;
  8. Long g = 3L;
  9. System.out.println(c ==d ); //true
  10. System.out.println(e ==f ); //false
  11. System.out.println(c == (a+b)); //true
  12. System.out.println(c.equals(a+b)); //true
  13. System.out.println(g == (a+b)); //true
  14. System.out.println(g.equals(a + b)); //false
  15. }

编译后的class文件:

  1. public static void main(String[] args)
  2. {
  3. Integer a = Integer.valueOf(1);
  4. Integer b = Integer.valueOf(2);
  5. Integer c = Integer.valueOf(3);
  6. Integer d = Integer.valueOf(3);
  7. Integer e = Integer.valueOf(321);
  8. Integer f = Integer.valueOf(321);
  9. Long g = Long.valueOf(3L);
  10. System.out.println(c == d);
  11. System.out.println(e == f);
  12. System.out.println(c.intValue() == a.intValue() + b.intValue());
  13. System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
  14. System.out.println(g.longValue() == a.intValue() + b.intValue());
  15. System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
  16. }

包装类的"=="运算 在不遇到算数运算的情况下不会自动拆箱,equals也不会处理数据转型

3.条件编译

源代码:

  1. public static void main(String[] args){
  2. if(true){
  3. System.out.print("a");
  4. }else{
  5. System.out.print("b");
  6. }
  7. }

编译后的class文件:

  1. public static void main(String[] paramArrayOfString) {
  2. System.out.print("a");
  3. }
  4. }

条件编译可以帮助我们消除源代码中的一些死代码

4.变长参数

源代码:

  1. public static void main(String[] args) {
  2. Demo5.printParams(1, 2, 3, 4, 5);
  3. }
  4. public static void printParams(Integer... a) {
  5. Integer[] as = a;
  6. for(Integer i : as){
  7. System.out.println(i);
  8. }
  9. }

编译后的class文件:

  1. public static void main(String[] paramArrayOfString){
  2. printParams(new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) });
  3. }
  4. public static void printParams(Integer... paramVarArgs){
  5. Integer[] arrayOfInteger1 = paramVarArgs;
  6. for (Integer localInteger : arrayOfInteger1) {
  7. System.out.println(localInteger);
  8. }
  9. }

5.遍历与循环

源代码:

  1. public static void main(String[] args){
  2. List list1 = Arrays.asList(1,2,3,4,5);
  3. int sum = 0;
  4. for(int i : list1){
  5. sum += i;
  6. }
  7. System.out.println("sum is :"+sum);
  8. }

编译后的class文件:

  1. public static void main(String[] paramArrayOfString){
  2. List localList = Arrays.asList(new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) });
  3. int i = 0;
  4. for (Iterator localIterator = localList.iterator(); localIterator.hasNext();){
  5. int j = ((Integer)localIterator.next()).intValue();
  6. i += j;
  7. }
  8. System.out.println("sum is :" + i);
  9. }

从编译后的代码来看foreach在遍历时实际上还是调用了底层的迭代方法

6.内部类

源代码:

  1. public class Demo8 {
  2. class Demo8_1{
  3. private String name="";
  4. Demo8_1(String name){
  5. this.name = name;
  6. }
  7. }
  8. }

编译后的class文件:

  1. 有内部类会编译出多个class文件
  2. Demo8.class:
  3. public class Demo8{
  4. class Demo8_1{
  5. private String name = "";
  6. Demo8_1(String name){
  7. this.name = name;
  8. }
  9. }
  10. }
  11. Demo8$Demo8_1.class
  12. class Demo8$Demo8_1{
  13. private String name = "";
  14. Demo8$Demo8_1(Demo8 paramDemo8, String name){
  15. this.name = name;
  16. }
  17. }

7.对字符串的switch支持(jdk1.7)

源代码:

  1. public static void stringSwitch() {
  2. String str = "a";
  3. switch (str) {
  4. case "a":
  5. System.out.println("a");
  6. break;
  7. case "b":
  8. System.out.println("b");
  9. break;
  10. default:
  11. System.out.println("default");
  12. break;
  13. }
  14. }

编译后的class文件:

  1. public static void stringSwitch() {
  2. String str = "a";
  3. String str1 = str;
  4. switch (str.hashCode()){
  5. case 97:
  6. if (str1.equals("a")) {
  7. break;
  8. }
  9. break;
  10. case 98:
  11. if (!str1.equals("b")){
  12. break label82;
  13. System.out.println("a");
  14. return;
  15. }else {
  16. System.out.println("b");
  17. }
  18. break;
  19. }
  20. label82:
  21. System.out.println("default");
  22. }

8.自动为try代码快中的资源进行关闭(jdk7中为大多数资源对象实现了AutoCloseable接口)

源代码:

  1. public static String readFirstLineFromFile(String path) throws IOException{
  2. try(BufferedReader br=new BufferedReader(new FileReader(path))){
  3. return br.readLine();
  4. }
  5. }

编译后的class文件:

  1. public static String readFirstLineFromFile(String path)
  2. throws IOException{
  3. Object localObject1 = null;Object localObject4 = null;
  4. Object localObject3;
  5. try{
  6. BufferedReader br = new BufferedReader(new FileReader(path));
  7. try {
  8. return br.readLine();
  9. }
  10. finally {
  11. if (br != null) {
  12. br.close();
  13. }
  14. }
  15. }
  16. finally {
  17. if (localObject2 == null) {
  18. localObject3 = localThrowable;
  19. } else if (localObject3 != localThrowable) {
  20. localObject3.addSuppressed(localThrowable);
  21. }
  22. }
  23. }

 

  • 参考资料:《深入理解Java虚拟机》
  • 反编译工具:JD-JUI

 

 

转载请注明:左手代码右手诗 » java语法糖的味道

喜欢 (1)or分享 (0)
发表我的评论
取消评论

 

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址