ARTS-0

作者: LazyLion | 来源:发表于2019-03-24 22:54 被阅读0次

    Algorithm

    代码地址:https://github.com/Lazy-Lion/Algorithm/blob/master/java/src/dynamicprogramming/LongestIncreasingSubsequence.java

    代码地址: https://github.com/Lazy-Lion/Algorithm/blob/master/java/src/dynamicprogramming/WildcardMatching.java

    Review

    Strings, Literally
    文章介绍了java 中String字面量以及String对象的一些知识点。

    针对String字面量进行如下的代码测试 (JDK8):

    public class StringTest {
        static String temp3 = "CD";
        final static String temp4 = "CD";
         
        public static void main(String[] args) {
             
            String s1 = "ABCD";
            String s2= "A" + "B" + "C" + "D";
             
            String temp = "CD";
            String s3 = "AB" + temp;
             
            final String temp2 = "CD";
            String s4 = "AB" + temp2;
    
            String s5 = "AB" + temp3;
             
            String s6 = "AB" + temp4;
             
            String s7 = "AB" + get();
             
            String s8 = "AB" + getF();
             
            //tip: 编写代码System.out.println("s1 == s2 ? " + s1 == s2); 漏写括号,
            //导致输出的是"(s1 == s2 ? " + s1) == s2" 的结果
            System.out.println("s1 == s2 ? " + (s1 == s2));  
            System.out.println("s1 == s3 ? " + (s1 == s3));
            System.out.println("s1 == s4 ? " + (s1 == s4));
            System.out.println("s1 == s5 ? " + (s1 == s5));
            System.out.println("s1 == s6 ? " + (s1 == s6));
            System.out.println("s1 == s7 ? " + (s1 == s7));
            System.out.println("s1 == s8 ? " + (s1 == s8));
        }
         
        public static String get(){
            return "CD";
        }
         
        public final static String getF(){
            return "CD";
        }
    }
    

    对上述代码进行反编译:

    public class StringTest {
        static String temp3 = "CD";
        static final String temp4 = "CD";
     
        public static void main(String[] args) {
            String s1 = "ABCD";
            String s2 = "ABCD";
            String temp = "CD";
            String s3 = "AB" + temp;
            String temp2 = "CD";
            String s4 = "ABCD";
            String s5 = "AB" + temp3;
            String s6 = "ABCD";
            String s7 = "AB" + get();
            String s8 = "AB" + getF();
            System.out.println("s1 == s2 ? " + (s1 == s2));
            System.out.println("s1 == s3 ? " + (s1 == s3));
            System.out.println("s1 == s4 ? " + (s1 == s4));
            System.out.println("s1 == s5 ? " + (s1 == s5));
            System.out.println("s1 == s6 ? " + (s1 == s6));
            System.out.println("s1 == s7 ? " + (s1 == s7));
            System.out.println("s1 == s8 ? " + (s1 == s8));
        }
     
        public static String get() {
            return "CD";
        }
     
        public static final String getF() {
            return "CD";
        }
    }
    

    可以看到,对于"+"连接,编译期间存在部分优化:

    • "+" 直接连接多个字面量,编译期间优化成一个字面量
    • "+" 连接字面量和final变量,编译期间优化成一个字面量
    • "+" 连接字面量和非final变量,不优化
    • "+" 连接字面量和方法返回值,不优化

    Tip

    工作中遇到为了去除重复数据而使用TreeSet的场景

    TreeSet<K> set = new TreeSet<>();
    

    java中的Set一般都是通过Map实现,使用TreeSet的无参构造方法实例化一个对象时,实际上是通过TreeMap实现的,这时泛型对象K必须是实现了Comparable接口的对象。TreeMap的put方法进行大小比较时,先判断是否传入外部比较器(Comparator),如果没有则使用K类的Comparable接口实现,而无参构造器实例化时是没有外部比较器的。

    Share

    Unicode的流言终结者和编码大揭秘
    关于Unicode字符集以及Unicode编码之间的区别与联系

    相关文章

      网友评论

          本文标题:ARTS-0

          本文链接:https://www.haomeiwen.com/subject/svrpvqtx.html