Shouldn't “static” patterns always be static?

前端 未结 5 1550
名媛妹妹
名媛妹妹 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:51

    Yes, compiling the Pattern on each use is wasteful, and defining it statically would result in better performance. See this SO thread for a similar discussion.

提交回复
热议问题