Skip to content

Commit 6783370

Browse files
Copilotbrunoborges
andcommitted
Address code review feedback: fix resource leaks and improve clarity
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 69591e2 commit 6783370

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

content/io/file-memory-mapping.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"modernLabel": "Java 22+",
1010
"oldApproach": "MappedByteBuffer",
1111
"modernApproach": "MemorySegment with Arena",
12-
"oldCode": "FileChannel channel =\n FileChannel.open(path,\n StandardOpenOption.READ,\n StandardOpenOption.WRITE);\nMappedByteBuffer buffer =\n channel.map(\n FileChannel.MapMode.READ_WRITE,\n 0, (int) channel.size());\n// Limited to 2GB\n// Freed by GC, no control",
12+
"oldCode": "try (FileChannel channel =\n FileChannel.open(path,\n StandardOpenOption.READ,\n StandardOpenOption.WRITE)) {\n MappedByteBuffer buffer =\n channel.map(\n FileChannel.MapMode.READ_WRITE,\n 0, (int) channel.size());\n // Limited to 2GB\n // Freed by GC, no control\n}",
1313
"modernCode": "FileChannel channel =\n FileChannel.open(path,\n StandardOpenOption.READ,\n StandardOpenOption.WRITE);\ntry (Arena arena = Arena.ofShared()) {\n MemorySegment segment =\n channel.map(\n FileChannel.MapMode.READ_WRITE,\n 0, channel.size(), arena);\n // No size limit\n // ...\n} // Deterministic cleanup",
1414
"summary": "Map files larger than 2GB with deterministic cleanup using MemorySegment.",
1515
"explanation": "The Foreign Function & Memory API (JEP 454) introduces MemorySegment for safe and efficient memory access. Unlike MappedByteBuffer, MemorySegment supports files larger than 2GB (Integer.MAX_VALUE), provides deterministic cleanup via Arena, and offers better performance with modern hardware.",

content/language/static-members-in-inner-classes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"oldApproach": "Must use static nested class",
1111
"modernApproach": "Static members in inner classes",
1212
"oldCode": "class Library {\n // Must be static nested class\n static class Book {\n static int globalBookCount;\n\n Book() {\n globalBookCount++;\n }\n }\n}\n\n// Usage\nvar book = new Library.Book();",
13-
"modernCode": "class Library {\n // Can be inner class with statics\n class Book {\n static int globalBookCount;\n\n Book() {\n globalBookCount++;\n }\n }\n}\n\n// Usage\nvar lib = new Library();\nvar book = lib.new Book();",
13+
"modernCode": "class Library {\n // Can be inner class with statics\n class Book {\n static int globalBookCount;\n\n Book() {\n Book.globalBookCount++;\n }\n }\n}\n\n// Usage\nvar lib = new Library();\nvar book = lib.new Book();",
1414
"summary": "Define static members in inner classes without requiring static nested classes.",
1515
"explanation": "Before Java 16, only static nested classes could contain static members. Inner (non-static) classes couldn't have statics because they required an enclosing instance. Java 16 relaxes this restriction, allowing static fields, methods, and even nested types in inner classes.",
1616
"whyModernWins": [

content/language/static-methods-in-interfaces.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"modernLabel": "Java 8+",
1010
"oldApproach": "Utility classes",
1111
"modernApproach": "Interface static methods",
12-
"oldCode": "// Separate utility class needed\npublic class StringUtils {\n public static boolean isBlank(\n String s) {\n return s == null ||\n s.trim().isEmpty();\n }\n}\n\n// Usage\nif (StringUtils.isBlank(input)) { ... }",
12+
"oldCode": "// Separate utility class needed\npublic class ValidatorUtils {\n public static boolean isBlank(\n String s) {\n return s == null ||\n s.trim().isEmpty();\n }\n}\n\n// Usage\nif (ValidatorUtils.isBlank(input)) { ... }",
1313
"modernCode": "public interface Validator {\n boolean validate(String s);\n\n static boolean isBlank(String s) {\n return s == null ||\n s.trim().isEmpty();\n }\n}\n\n// Usage\nif (Validator.isBlank(input)) { ... }",
1414
"summary": "Add static utility methods directly to interfaces instead of separate utility classes.",
1515
"explanation": "Before Java 8, utility methods related to an interface had to live in a separate class (e.g., Collections for Collection). Static methods in interfaces let you keep related utilities together. Common in modern APIs like Comparator.comparing(), Stream.of(), and List.of().",

0 commit comments

Comments
 (0)