Shouldn't “static” patterns always be static?

前端 未结 5 1535
名媛妹妹
名媛妹妹 2021-01-01 10:42

I just found a bug in some code I didn\'t write and I\'m a bit surprised:

Pattern pattern = Pattern.compile(\"\\\\d{1,2}.\\\\d{1,2}.\\\\d{4}\");
Matcher matc         


        
5条回答
  •  再見小時候
    2021-01-01 10:57

    Static Patterns would remain in memory as long as the class is loaded.

    If you are worried about memory and want a throw-away Pattern that you use once in a while and that can get garbage collected when you are finished with it, then you can use a non-static Pattern.

提交回复
热议问题