All posts tagged #java all posts RSS-feed

Performance in debug mode (Java/IntelliJ)

I'd like to highlight two steps to avoid performance issues when using breakpoints. Method/field breakpoints Is your application noticeably slower in debug mode, even when not in a break point? Did this problem start suddenly? Do computation-heavy tasks take especially long (like password hashing)? Then you may have some breakpoints that cost a lot of performance (why?). Breakpoints on lines of code are fast Breakpoints or fields and methods are slow (Breakpoints on declarations/comments don't work) In my experience, over... full post»

Example log4j2.xml (development)

For local development, I like to change some things in my log4j2 configuration. Make lines shorter, e.g. no worker names (just pid) or dates (just times). Prevent all logging to files, just console. Use colors for errors and warnings so they stand out. Filter out specific types of messages, and keep only specific other ones at debug level. Use buffering and async for better performance. So here it is! <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Configuration monitorInterval="30"> <Appenders> <Console name="Console" target="SYSTEM_OUT" bufferSize="100">... full post»

Java performance: exceptions

This was a real performance problems recently, with some details redacted. It was found that filling in translated strings took up quite a lot of time. Translation requests are spread out all across the frontend, so no single step stood out, but with the help of YourKit, the problem was uncovered. As it turns out, the main problem was with this construct: for (int i = translationFiles.length - 1; i >= 0; i--) { try { ResourceBundle bundle = ResourceBundle.getBundle(translationFiles[i],... full post»

Java findFirst stream pitfall

Java streams have findAny and findFirst methods out of the box. And they works fine, they do exactly what they say: it gets one of the matches. But in my experience, often I don't want 'one of the matches', I want there to be a single match, and I want that one. The problem For example, finding the instances in a stream of customers var formExpressionInstance = getCustomers().stream() .filter(instance -> isDateInRange(ferenceDate, instance.getDateStart(), instance.getDateEnd())) .findFirst() If there are multiple matches, maybe... full post»

Java overload + vararg pitfall

If you are using overloads In Java, and one of them uses varargs, then there's a rare pitfall to be aware of. I ran into this a while back. There were two methods, similar to this: public void method( @Nullable Object first, @Nullable String second, @Nullable Integer third, @Nullable BigDecimal fourth, @Nullable Double fifth ) throws Exception { and public void method( @Nonnull Integer... varargs ) throws Exception { Then, someone added a sixth argument to the first method. Simultaneously... full post»

Visitor pattern and dynamic dispatch

The Design Patterns book by the infamous Gang of Four contains 23 object oriented design patterns. Some of them are kind of obvious (Adapter), or have become common knowledge since the book was published in 1994 (Iterator). My personal favourite is Visitor. Visitor "represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operations" [except you need to add an... full post»

Analyzing a single, trivial Java statement

Java isn't a bad language, but it is also not great, and it's age shows. Let's look at this simple Java statement: if (!Boolean.TRUE.equals(getIsRequired())) { It's very simple and common, and yet it includes a lot of the things I don't particularly like about Java: We need a getter for a trivial property access. I like the Python way with @property, or the Kotlin way with getters and setters being called implicitly. It is not explicit that getIsRequired is a... full post»