zoukankan      html  css  js  c++  java
  • [转]Java se 7 最新特性研究(一)

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp81

     

    从2006到现在等待了多年的jdk7终于发布了.这里将对它的一些新特做一些初步的研究:

    jdk7 相对于jdk6主要在以一几个方面有了非常显著的改进的提升:

    首先分析其对java语言本身的改进:

    二进制表示的支持(Binary Literals)的支持。

    在JDK7中可以用二进制的形式表示整形数据(byte,short,int,long),这些整形类数据如果用二制表示则需0b或0B打头。如:

    byte aByte=(byte)0b01;

    short aShort=(short)0B001;

    int aInt=0b01001;

    long aLong=0B011101L;

    使用二进制表示数据有什么好处呢?我们看一下下面的例字。

    示例一:

    要求定义一个数组。表示按位旋转(即后个数是前一个数向左旋转一位,请注意是旋转)。用二制可以比较清晰地表示这种数据结构:

    public static final int[] phases = {
      0b00110001,
      0b01100010,
      0b11000100,
      0b10001001,
      0b00010011,
      0b00100110,
      0b01001100,
      0b10011000
    }

    大家看了上面的定义可以比较快地理解定义的内容。如果用16进制或8进制那可能就比较难理解了。如下:

    public static final int[] phases = {
        0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98
    }

    在定义和设计一些模拟器,如微处理器等方面也很有帮助。如下面是定义的一个8位的微处理器的模拟器:

    public State decodeInstruction(int instruction, State state) {
      if ((instruction & 0b11100000) == 0b00000000) {
        final int register = instruction & 0b00001111;
        switch (instruction & 0b11110000) {
          case 0b00000000: return state.nop();
          case 0b00010000: return state.copyAccumTo(register);
          case 0b00100000: return state.addToAccum(register);
          case 0b00110000: return state.subFromAccum(register);
          case 0b01000000: return state.multiplyAccumBy(register);
          case 0b01010000: return state.divideAccumBy(register);
          case 0b01100000: return state.setAccumFrom(register);
          case 0b01110000: return state.returnFromCall();
          default: throw new IllegalArgumentException();
        }
      } else {
        final int address = instruction & 0b00011111;
        switch (instruction & 0b11100000) {
          case 0b00100000: return state.jumpTo(address);
          case 0b01000000: return state.jumpIfAccumZeroTo(address);
          case 0b01000000: return state.jumpIfAccumNonzeroTo(address);
          case 0b01100000: return state.setAccumFromMemory(address);
          case 0b10100000: return state.writeAccumToMemory(address);
          case 0b11000000: return state.callTo(address);
          default: throw new IllegalArgumentException();
        }
      }
    }

    当然你可以很利二进制定义一张图片。如:


    public static final short[] HAPPY_FACE = {
       (short)0b0000011111100000;
       (short)0b0000100000010000;
       (short)0b0001000000001000;
       (short)0b0010000000000100;
       (short)0b0100000000000010;
       (short)0b1000011001100001;
       (short)0b1000011001100001;
       (short)0b1000000000000001;
       (short)0b1000000000000001;
       (short)0b1001000000001001;
       (short)0b1000100000010001;
       (short)0b0100011111100010;
       (short)0b0010000000000100;
       (short)0b0001000000001000;
       (short)0b0000100000010000;
       (short)0b0000011111100000;
    }

     

    总之,JDK7新增的这种整形的表示方对处理二制的数据操作提供了很好的帮助。

     

    String 可以作为switch的statement,很多编程人员都遇到过通过一个字符串来决定程序流转的问题,以JDK7以前大家得通过一些特殊的处理才能得以实现。如先通过一个转换器将

    字符串转换为数字类型。JDK7就不需要做那些转换可以直接将字符串做条件来使用。如下:

    public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
         String typeOfDay;
         switch (dayOfWeekArg) {
             case "Monday":
                 typeOfDay = "Start of work week";
                 break;
             case "Tuesday":
             case "Wednesday":
             case "Thursday":
                 typeOfDay = "Midweek";
                 break;
             case "Friday":
                 typeOfDay = "End of work week";
                 break;
             case "Saturday":
             case "Sunday":
                 typeOfDay = "Weekend";
                 break;
             default:
                 throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
         }
         return typeOfDay;
    }

    是不是觉得上面的代码很简明?

     

    今天先研究到这里,明天研究JDK7最新的异常处理处理特性.

  • 相关阅读:
    Java [Leetcode 191]Number of 1 Bits
    Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree
    Java [Leetcode 169]Majority Element
    Java [Leetcode 171]Excel Sheet Column Number
    Java [Leetcode 217]Contains Duplicate
    Java [Leetcode 242]Valid Anagram
    Java [Leetcode 100]Same Tree
    Java [Leetcode 258]Add Digits
    Java [Leetcode 104]Maximum Depth of Binary Tree
    D365 FO财务维度
  • 原文地址:https://www.cnblogs.com/grefr/p/5046288.html
Copyright © 2011-2022 走看看