Does introducing a default method to an interface really preserve back-compatibility?

Although adding a default method with the same name in the two interfaces would make the code fail to compile, but once you resolve the compilation error, the binaries obtained after compiling both the interfaces, and the class implementing the interfaces, would be backward compatible. So, the compatibility is really about binary compatibility. This is … Read more

Java8 Adding Hours To LocalDateTime Not Working

The documentation of LocalDateTime specifies the instance of LocalDateTime is immutable, for example plusHours public LocalDateTime plusHours(long hours) Returns a copy of this LocalDateTime with the specified number of hours added. This instance is immutable and unaffected by this method call. Parameters: hours – the hours to add, may be negative Returns: a LocalDateTime based … Read more

Empty methods noticeably slower in Java 11 than Java 8

You are measuring empty benchmarks, not empty methods. In other words, measuring the minimal infrastructure code that handles the benchmark itself. This is easy to dissect, because you’d expect only a few instructions on the hot path. JMH’s -prof perfasm or -prof xperfasm would give you those hottest instructions in seconds. I think the effect … Read more

How to convert LocalDateTime to OffsetDateTime?

There are many ways to convert LocalDateTime to OffsetDateTime. Some of them are listed below: 1. Using LocalDateTime#atOffset​(ZoneOffset offset): LocalDateTime ldt = LocalDateTime.now(); ZoneOffset offset = ZoneOffset.UTC; OffsetDateTime odt = ldt.atOffset(offset); 2. Using LocalDateTime#atZone​(ZoneId zone) => ZonedDateTime#toOffsetDateTime(): LocalDateTime ldt = LocalDateTime.now(); // Change the ZoneId as required e.g. ZoneId.of(“Europe/London”) ZoneId zoneId = ZoneId.systemDefault(); OffsetDateTime odt … Read more

How can I return LocalDate.now() in milliseconds?

Calling toInstant().toEpochMilli(), as suggested by @JB Nizet’s comment, is the right answer, but there’s a little and tricky detail about using local dates that you must be aware of. But before that, some other minor details: Instead of ZoneId.of(“GMT”) you can use the built-in constant ZoneOffset.UTC. They’re equivalent, but there’s no need to create extra … Read more

Drawbacks of javac -parameters flag

The addition of parameter names to the class file format is covered by JEP 118, which was delivered in Java 8. There was a little bit of discussion about why inclusion of parameter names was made optional in OpenJDK email threads here and here. Briefly, the stated reasons to make parameter names optional are concerns … Read more

Getting the date from a ResultSet for use with java.time classes

Most database vendors don’t support JDBC 4.2 yet. This specification says that the new java.time-types like LocalDate will/should be supported using the existing methods setObject(…) and getObject(). No explicit conversion is required and offered (no API-change). A workaround for the missing support can be manual conversion as described on the Derby-mailing list. Something like: LocalDate … Read more

Java 8 is not maintaining the order while grouping

Not maintaining the order is a property of the Map that stores the result. If you need a specific Map behavior, you need to request a particular Map implementation. E.g. LinkedHashMap maintains the insertion order: groupedResult = people.collect(Collectors.groupingBy( p -> new GroupingKey(p, groupByColumns), LinkedHashMap::new, Collectors.mapping((Map<String, Object> p) -> p, toList()))); By the way, there is … Read more

tech