1. 程式人生 > 實用技巧 >Java-學習日記(Shell與String底層原理)

Java-學習日記(Shell與String底層原理)

Java雜記-2020.08.07

@Test中測試所有getter,setter方法

最近一週在寫codereview,相關技術是kmock1.0.19,gradle

  • build.gradle中配置檔案
    testCompile 'pl.pojo:pojo-tester:0.7.6'
    testCompile('junit:junit:4.12')
    testCompile('kmock:kmock:1.0.19')
  • @Test中的使用,這樣就能覆蓋所有getter,setter方法了
    @Test
    public void ExcelInvoiceRes(){
        // given
        final Class<?> classUnderTest = ExcelInvoiceRes.class;
        // when
        // then
        assertPojoMethodsFor(classUnderTest).quickly().testing(Method.GETTER, Method.SETTER)
                .testing(Method.CONSTRUCTOR)
                .areWellImplemented();
    }

Shell面試題:批量生成隨機字元檔名

for i in {1..10}
 do
   filename=$(uuidgen|tr '0-9' 'a-z'|cut -c 1-10)
   touch ${filename}_empirefree.html
 done

刪除以 .html結尾的檔案

find . -name '*.html'| xargs rm -rf

Shell面試題:批量修改檔名

for i in $(ls *html)
do
  rn=$(echo $i|cut -c -10)
  mv $i $(rn).HTML
done

Shell面試題:https://blog.51cto.com/13520779/2093146

String和final String

都知道String內部是設定成final的,因為執行效率高和安全性這兩個優點,前面加final的話就類似常量了,具體如下

String a = "a";
System.out.println((a + "b") == "ab");      //false
System.out.println(("a" + "b") == "ab");    //true
final String b = "b";
System.out.println(("a" + b) == "ab");      //true
System.out.println((a + b) == "ab");        //false

執行效率高:當子類重寫(override)父類某個方法,JVM如果是用final修飾的方法,就能直接用,效率高,而不用去虛擬函式表裡面尋找
安全性:多執行緒中,如果String被修改了,會導致安全問題(String a = "b"只是修改了變數指引,實際"a"的記憶體並沒有消失)。還有就是Hashmap中如果String可變,就會產生不同的 Hash值,這也會引發安全性