consider the below code
while(100 times){
Pattern abc = Pattern.compile("[0-9]+");
}
Does java compile the pattern once or 100 times ?
consider the below code
while(100 times){
Pattern abc = Pattern.compile("[0-9]+");
}
Does java compile the pattern once or 100 times ?
JIT might optimize it away at least in theory, but even then it would require more than 100 loops.
The displayed code is just poor code, since the pattern doesn't change and can easily be reused by declaring it outside of the loop. Of course compiling 100 Pattern
s is still so fast that you wouldn't really see a difference.
The javac
compiler will not attempt to do anything, as it optimizes only very simple things.