Does Pattern.compile cache?

前端 未结 5 645
盖世英雄少女心
盖世英雄少女心 2020-12-04 01:54

It is likely an implementation detail, but for the Oracle and IBM JDKs at least is the compiled pattern cached or do we as application developers need to perform the caching

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 02:10

    I don't believe the results are cached and there's no evidence of such behaviour in the code or the documentation. It would (of course) be relatively trivial to implement such a cache yourself, but I would be interested in a use case in which such caching is beneficial.

    Re. the comment below and String.split(), there's a different approach in that the code takes a distinct path for trivial 1 or 2 char patterns vs more complex regexps. But it still doesn't appear to cache.

提交回复
热议问题