Welcome to another issue of my newsletter!
The last days brought quite some interesting changes for me. On Nov, 1st, I started a new role as Lead Java Developer Advocate at IBM. I’ll continue to evangelize Enterprise Java, Jakarta EE, MicroProfile, cloud native technologies, and everything related to enterprise software, and I’m very much looking forward to the upcoming months.
That means, the first November travels were already part of my new job. Last week, I visited the Utrecht JUG, Amsterdam JUG and JFall conference. I very much enjoyed the time in the Netherlands and was happy to share some knowledge on service meshes, productive development, effective testing, and MicroProfile.
Right now, I’m traveling back from Devoxx Belgium. I’m happy that I could share my views on Jakarta EE, MicroProfile, and which combinations of these technologies make sense for enterprise projects. Next week, I’ll be attending the Øredev conference in Malmö, Sweden. So the conference travel continues :-)
Matching Patterns With Java
There’s a high change you’ve used Java’s pattern matching functionality at some point in your career.
String#matches(String) method internally uses the
Pattern type, which is comprises more complex functionality:
Pattern is created by compiling a regular expression.
The pattern matches any input string and can optionally find capturing groups, that isolate certain parts of your string data.
The API is used as follows:
Pattern pattern = Pattern.compile("([\\^\\S]+) is powerful"); Matcher matcher = pattern.matcher("Java is powerful"); System.out.println(matcher.find()); // true System.out.println(matcher.group()); // Java is powerful System.out.println(matcher.group(1)); // Java
find() method finds the next occurrence of the pattern, which matches the whole input string in this example.
group() method returns either the whole capturing group, that is, matching the whole pattern, or, when qualified with an index, returns the individual capturing groups.
The capturing groups indexes start at
1, not at
There’s also a
matches() method which works slightly differently:
Pattern pattern = Pattern.compile("([\\^\\S]+) is powerful"); Matcher matcher = pattern.matcher("Our Java is powerful"); System.out.println(matcher.matches()); // false System.out.println(matcher.find()); // true
matches() attempts to match the whole input string to the pattern, from start to end, while
find() only tries to find the patterns somewhere in the input string.
Also, as reminder: Please use the shortcut methods
Pattern#matches(String, CharSequence) only for single matching invocations that are not repeated over and over again.
Patterns are rather heavy to compile and we should rather leverage the immutability of the Pattern type and reuse it for multiple input matches.
OpenJ9, Class Sharing And AOT
Thanks to my buddy Billy Korando, I just learned about the OpenJ9 JVM and all it’s performance-related features. Billy gave a talk at Jfall on how to boost the startup time and resource consumptions of Java applications by running them on J9 with certain performance optimizations.
This project seems to gain quite some traction right now and the class sharing and ahead-of-time (AOT) compilation looks promising. You can have a look at the project’s website and how class sharing in OpenJ9 works.
Thanks a lot for reading and see you next time!
Did you like the content? You can subscribe to the newsletter for free:
All opinions are my own and do not reflect those of my employer or colleagues.