├── docs ├── 44_q_and_a.adoc ├── img │ └── sealed-class-hierarchy.png ├── 36_sealed_classes_sealed_class_diagram.adoc ├── 42_garbage_collectors.adoc ├── 41_removals.adoc ├── 34_sealed_classes_intro1.adoc ├── 03_jmh_tests_measurement_pattern.adoc ├── 14_var_intro.adoc ├── 32_records_old_style.adoc ├── 22_text_blocks_new_style.adoc ├── 24_helpful_npe_intro.adoc ├── 37_sealed_classes_sealed_interface.adoc ├── 43_take_aways.adoc ├── 21_text_blocks_old_style.adoc ├── 00_toc.adoc ├── 01_intro.adoc ├── 16_switch_statements_intro.adoc ├── 13_performance_tests_summary.adoc ├── 28_pattern_matching_for_instanceof_intro.adoc ├── 02_jmh_tests_intro.adoc ├── 25_helpful_npe_old_style.adoc ├── 15_var_code.adoc ├── 20_text_blocks_intro.adoc ├── 23_text_blocks_injecting_variables.adoc ├── 38_sealed_classes_sealed_interface_hierarchy.adoc ├── 10_memory_tests_intro.adoc ├── 33_records_new_style.adoc ├── 12_memory_tests_measurements.adoc ├── 19_switch_expressions_yield.adoc ├── 06_jmh_tests_map_put_string_measurements.adoc ├── 09_jmh_tests_map_get_string_measurements.adoc ├── 20_switch_statements_enums.adoc ├── 31_records_intro.adoc ├── 04_jmh_tests_code_put.adoc ├── 05_jmh_tests_map_put_integer_measurements.adoc ├── 08_jmh_tests_map_get_integer_measurements.adoc ├── 26_helpful_npe_new_style_1.adoc ├── 27_helpful_npe_new_style_2.adoc ├── 39_sealed_classes_sealed_class_hierarchy.adoc ├── 40_sealed_classes_sealed_non_sealed_hierarchy.adoc ├── 35_sealed_classes_intro2.adoc ├── 29_pattern_matching_for_instanceof_old_style.adoc ├── 11_memory_tests_code.adoc ├── 07_jmh_tests_code_get.adoc ├── 30_pattern_matching_for_instanceof_new_style.adoc ├── 18_switch_expressions_updated_style.adoc ├── 17_switch_statements_old_style.adoc └── 42b_garbage_collectors_notes.adoc ├── code-examples ├── src │ ├── main │ │ └── java │ │ │ └── nikhil │ │ │ └── nani │ │ │ └── code │ │ │ └── examples │ │ │ ├── sealed │ │ │ └── classes │ │ │ │ ├── Owner.java │ │ │ │ ├── DayGuest.java │ │ │ │ ├── SuperAdmin.java │ │ │ │ ├── TempAdmin.java │ │ │ │ ├── Resident.java │ │ │ │ ├── OvernightGuest.java │ │ │ │ ├── User.java │ │ │ │ └── Admin.java │ │ │ └── Main.java │ └── test │ │ └── java │ │ └── nikhil │ │ └── nani │ │ └── code │ │ └── examples │ │ ├── ExamplesVarTest.java │ │ ├── ExamplesHelpfulNPEsTest.java │ │ ├── sealed │ │ └── classes │ │ │ └── ExamplesSealedClassesTest.java │ │ ├── ExamplesPatternMatchingTest.java │ │ ├── ExamplesTextBlockTest.java │ │ ├── ExamplesRecordTest.java │ │ └── ExamplesSwitchTest.java └── pom.xml ├── .gitignore ├── README.md ├── performance-tests-jdk17 ├── src │ └── main │ │ └── java │ │ └── nikhil │ │ └── nani │ │ ├── JmhRunner17.java │ │ ├── MemoryTestsJdk17.java │ │ ├── MapPutJmhJdk17Tests.java │ │ └── MapGetJmhJdk17Tests.java └── pom.xml ├── performance-tests-jdk8 ├── src │ └── main │ │ └── java │ │ └── nikhil │ │ └── nani │ │ ├── JmhRunnerJdk8.java │ │ ├── MemoryTestsJdk8.java │ │ ├── MapPutJmhJdk8Tests.java │ │ └── MapGetJmhJdk8Tests.java └── pom.xml ├── performance-tests-jdk11 ├── src │ └── main │ │ └── java │ │ └── nikhil │ │ └── nani │ │ ├── JmhRunnerJdk11.java │ │ ├── MemoryTestsJdk11.java │ │ ├── MapPutJmhJdk11Tests.java │ │ └── MapGetJmhJdk11Tests.java └── pom.xml ├── performance-measurements ├── jdk8-jmh-tests.txt ├── jdk11-jmh-tests.txt ├── jdk17-jmh-tests.txt └── MemoryTests.txt ├── pom.xml └── LICENSE /docs/44_q_and_a.adoc: -------------------------------------------------------------------------------- 1 | == Q & A? 2 | 3 | --- 4 | 5 | link:./00_toc.adoc[TOC] / 6 | link:./43_take_aways.adoc[Take Aways] -------------------------------------------------------------------------------- /docs/img/sealed-class-hierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikhilnanivadekar/ShouldIUpgradeMyJava/HEAD/docs/img/sealed-class-hierarchy.png -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/Owner.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public final class Owner implements User { 4 | } -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/DayGuest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public final class DayGuest extends Resident { 4 | } 5 | -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/SuperAdmin.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public final class SuperAdmin extends Admin { 4 | } -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/TempAdmin.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | final class TempAdmin // extends Admin 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/Resident.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public non-sealed class Resident implements User { 4 | } 5 | 6 | -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/OvernightGuest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public final class OvernightGuest extends Resident { 4 | } 5 | -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/Main.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Hello world!"); 6 | } 7 | } -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/User.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public sealed interface User permits Owner, Admin, Resident { 4 | default String login() { 5 | return "Logged in"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /code-examples/src/main/java/nikhil/nani/code/examples/sealed/classes/Admin.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | public sealed class Admin implements User permits SuperAdmin { 4 | public String changePassword() { 5 | return "Password changed"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /docs/36_sealed_classes_sealed_class_diagram.adoc: -------------------------------------------------------------------------------- 1 | == Class Diagram 2 | 3 | image::img/sealed-class-hierarchy.png[] 4 | --- 5 | 6 | link:./00_toc.adoc[TOC] / 7 | link:./35_sealed_classes_intro2.adoc[Sealed Classes Intro - Contd] / 8 | link:./37_sealed_classes_sealed_interface.adoc[Sealed Interface] 9 | -------------------------------------------------------------------------------- /docs/42_garbage_collectors.adoc: -------------------------------------------------------------------------------- 1 | == Garbage Collectors 2 | 3 | ** Serial GC 4 | ** Parallel GC 5 | ** G1 GC 6 | ** ZGC 7 | ** Shenandoah GC 8 | ** Epsilon (Experimental as of Sept 2022) 9 | 10 | --- 11 | 12 | link:./00_toc.adoc[TOC] / 13 | link:./41_removals.adoc[Removals] / 14 | link:./43_take_aways.adoc[Take Aways] 15 | -------------------------------------------------------------------------------- /docs/41_removals.adoc: -------------------------------------------------------------------------------- 1 | == Removal 2 | 3 | ** Nashorn Engine (JDK 15) 4 | ** CMS Garbage Collector (JDK 14) 5 | ** Strongly Encapsulate JDK Internals (JDK 17) 6 | ** And a lot more deprecations 7 | 8 | --- 9 | 10 | link:./00_toc.adoc[TOC] / 11 | link:./40_sealed_classes_sealed_non_sealed_hierarchy.adoc[Non-Sealed Classes Hierarchy] / 12 | link:./42_garbage_collectors.adoc[Garbage Collectors] 13 | -------------------------------------------------------------------------------- /docs/34_sealed_classes_intro1.adoc: -------------------------------------------------------------------------------- 1 | == Sealed Classes (JDK 17) 2 | 3 | ** Provide a fine-grained inheritance control in Java 4 | ** Prior to sealed classes: every non-final class could be extended by any number of sub-classes 5 | 6 | --- 7 | 8 | link:./00_toc.adoc[TOC] / 9 | link:./33_records_new_style.adoc[Records New Style] / 10 | link:./35_sealed_classes_intro2.adoc[Sealed Classes Intro - Contd] 11 | -------------------------------------------------------------------------------- /docs/03_jmh_tests_measurement_pattern.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests: Measurement 2 | 3 | * Measure performance for some common usage pattern 4 | * `Map.get()` and `Map.put()` along with some `if` condition and `equals()` 5 | * Measure for `String` and `Integer` 6 | 7 | --- 8 | link:./00_toc.adoc[TOC] / 9 | link:./02_jmh_tests_intro.adoc[JMH Tests - Intro] / 10 | link:./04_jmh_tests_code_put.adoc[JMH Tests - put()] 11 | -------------------------------------------------------------------------------- /docs/14_var_intro.adoc: -------------------------------------------------------------------------------- 1 | == var (JDK 10) 2 | 3 | ** Local variable type inference 4 | ** Type inference is the idea that compiler can figure out the static types (so you don't have to type them) 5 | ** `var` can only be used for local variables with an explicit initialization 6 | 7 | --- 8 | 9 | link:./00_toc.adoc[TOC] / 10 | link:./13_performance_tests_summary.adoc[Performance Tests Summary] / 11 | link:./15_var_code.adoc[var code] 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | .idea/ 14 | *.iml 15 | 16 | # Package Files # 17 | *.jar 18 | *.war 19 | *.nar 20 | *.ear 21 | *.zip 22 | *.tar.gz 23 | *.rar 24 | 25 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 26 | hs_err_pid* 27 | 28 | # target files 29 | **/target/** 30 | -------------------------------------------------------------------------------- /docs/32_records_old_style.adoc: -------------------------------------------------------------------------------- 1 | == Records - Old Style 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | public final class PersonNonRecord { 6 | private final String firstName; 7 | private final String lastName; 8 | /** 9 | * Constructor, Accessor, hashcode, equals, toString 10 | */ 11 | } 12 | ---- 13 | 14 | --- 15 | 16 | link:./00_toc.adoc[TOC] / 17 | link:./31_records_intro.adoc[Records Intro] / 18 | link:./33_records_new_style.adoc[Records New Style] 19 | -------------------------------------------------------------------------------- /docs/22_text_blocks_new_style.adoc: -------------------------------------------------------------------------------- 1 | == Text Blocks 2 | 3 | -- 4 | [source,highlight=2..3] 5 | ---- 6 | String textBlockJson = """ 7 | { 8 | "firstName": "Nikhil", 9 | "lastName": "N", 10 | "city": "Seattle", 11 | "phone": 123 12 | }"""; 13 | ---- 14 | 15 | --- 16 | link:./00_toc.adoc[TOC] / 17 | link:./21_text_blocks_old_style.adoc[Text Block Old Style] / 18 | link:./23_text_blocks_injecting_variables.adoc[Text Block formatted] 19 | -------------------------------------------------------------------------------- /docs/24_helpful_npe_intro.adoc: -------------------------------------------------------------------------------- 1 | == Helpful Null Pointer Exceptions (JDK 14) 2 | 3 | ** Prior to Java 14: Difficult to isolate NPEs in chained method calls 4 | ** From Java 14: `NullPointerException` prints a detailed helpful message 5 | ** Only when the JVM throws an NPE and not when an NPE is explicitly thrown 6 | 7 | --- 8 | 9 | link:./00_toc.adoc[TOC] / 10 | link:./23_text_blocks_injecting_variables.adoc[Text Block formatted] / 11 | link:./25_helpful_npe_old_style.adoc[NPEs Old Style] 12 | -------------------------------------------------------------------------------- /docs/37_sealed_classes_sealed_interface.adoc: -------------------------------------------------------------------------------- 1 | == Sealed Interface 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | public sealed interface User permits Owner, Admin, Resident { 6 | default String login() { 7 | return "Logged in"; 8 | } 9 | } 10 | 11 | ---- 12 | 13 | --- 14 | 15 | link:./00_toc.adoc[TOC] / 16 | link:./36_sealed_classes_sealed_class_diagram.adoc[Hierarchy Diagram] / 17 | link:./38_sealed_classes_sealed_interface_hierarchy.adoc[Sealed Interface Hierarchy] 18 | -------------------------------------------------------------------------------- /docs/43_take_aways.adoc: -------------------------------------------------------------------------------- 1 | == Take-Aways 2 | 3 | ** Performance improvements, new features 4 | ** Upgrade to JDK 17 5 | ** If still on JDK 8 skip JDK 11 and go directly to JDK 17 6 | ** JDK 17 is more than a year old (released September 2021) 7 | ** JDK 19 is already released 8 | ** JDK 21 will be out in September 2023 9 | ** Embrace the future 10 | 11 | == Thank You! 12 | 13 | --- 14 | 15 | link:./00_toc.adoc[TOC] / 16 | link:./41_removals.adoc[Removals] / 17 | link:./44_q_and_a.adoc[Q & As] / 18 | -------------------------------------------------------------------------------- /docs/21_text_blocks_old_style.adoc: -------------------------------------------------------------------------------- 1 | == Text Blocks - The LONG String 2 | 3 | -- 4 | [source,highlight=2..3] 5 | ---- 6 | String json = "{\n" + 7 | " \"firstName\": \"Nikhil\",\n" + 8 | " \"lastName\": \"N\",\n" + 9 | " \"city\": \"Seattle\",\n" + 10 | " \"phone\": 123\n" + 11 | "}"; 12 | ---- 13 | 14 | --- 15 | link:./00_toc.adoc[TOC] / 16 | link:./20_text_blocks_intro.adoc[Text Blocks Intro] / 17 | link:./22_text_blocks_new_style.adoc[Text Block New Style] 18 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesVarTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Map; 7 | 8 | public class ExamplesVarTest { 9 | 10 | @Test 11 | public void var() { 12 | 13 | Map map = Map.of(1, "One", 2, "Two"); 14 | 15 | var withVar = Map.of(1, "One", 2, "Two"); 16 | 17 | Assertions.assertEquals(map, withVar); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /docs/00_toc.adoc: -------------------------------------------------------------------------------- 1 | == Agenda 2 | 3 | * link:./01_intro.adoc[Introduction] 4 | * link:./02_jmh_tests_intro.adoc[Performance Comparison (JDK 8 vs JDK 11 vs JDK 17)] 5 | * link:./14_var_intro.adoc[Features between JDK 8 to JDK 17. _Previews are not considered_] 6 | * link:./41_removals.adoc[Removal] 7 | * link:./42_garbage_collectors.adoc[Garbage Collectors] 8 | * link:./43_take_aways.adoc[Take Aways] 9 | * link:./44_q_and_a.adoc[Questions] 10 | 11 | --- 12 | 13 | link:./00_toc.adoc[TOC] / 14 | link:./01_intro.adoc[Intro] 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShouldIUpgradeMyJava 2 | 3 | Motivation for this project is to showcase the important features added after JDK 8. As new JDKs are released and new 4 | features are introduced this project can be updated to keep a list of new features. 5 | 6 | ## Slides 7 | 8 | https://github.com/nikhilnanivadekar/ShouldIUpgradeMyJava/blob/main/docs/00_toc.adoc 9 | 10 | ## Code Examples 11 | 12 | https://github.com/nikhilnanivadekar/ShouldIUpgradeMyJava/blob/main/code-examples/src/test/java/nikhil/nani/code/examples/ExamplesTest.java -------------------------------------------------------------------------------- /docs/01_intro.adoc: -------------------------------------------------------------------------------- 1 | == Introduction 2 | 3 | ** Nikhil Nanivadekar 4 | *** Active Project Lead, Eclipse Collections 5 | *** Java Champion 6 | *** Twitter: link:https://twitter.com/NikhilNanivade[NikhilNanivade] 7 | *** Project Repo: link:https://github.com/nikhilnanivadekar/ShouldIUpgradeMyJava[Should I Upgrade My Java] 8 | 9 | ** Special Thanks to: 10 | *** link:https://wkorando.github.io/presentations[Billy Korando] 11 | *** link:https://twitter.com/CGuntur[Chandra Guntur] 12 | 13 | --- 14 | link:./00_toc.adoc[TOC] / 15 | link:./02_jmh_tests_intro.adoc[JMH Tests - Intro] 16 | -------------------------------------------------------------------------------- /docs/16_switch_statements_intro.adoc: -------------------------------------------------------------------------------- 1 | == Updated Switch Statements (JDK 14) 2 | 3 | ** Can be used as an expression to yield value 4 | ** A `case` statement can contain multiple values separated by commas 5 | ** New restricted identifier `yield` to use for code blocks 6 | ** `switch` can be on enums 7 | ** `switch` expressions need to be exhaustive 8 | ** `switch` expressions _must_ `yield` a value or throw an exception 9 | 10 | --- 11 | 12 | link:./00_toc.adoc[TOC] / 13 | link:./15_var_code.adoc[var code] / 14 | link:./17_switch_statements_old_style.adoc[Switch Statements Old Style] 15 | -------------------------------------------------------------------------------- /docs/13_performance_tests_summary.adoc: -------------------------------------------------------------------------------- 1 | == Performance Tests Summary 2 | 3 | * Java 17 consistently performs better than Java 8 and Java 11 for `Map.put()` and `Map.get()` for `Integer` 4 | * Java 17 consistently performs better at large sizes for `Map.put()` and `Map.get()` for `String` 5 | * Java 17 consistently uses less memory 6 | * Your Mileage May Vary (YMMV) 7 | ** _Please test, benchmark and profile your application._ 8 | 9 | --- 10 | 11 | link:./00_toc.adoc[TOC] / 12 | link:./12_memory_tests_measurements.adoc[Memory Tests Measurements] / 13 | link:./14_var_intro.adoc[var Intro] 14 | -------------------------------------------------------------------------------- /docs/28_pattern_matching_for_instanceof_intro.adoc: -------------------------------------------------------------------------------- 1 | == Pattern Matching for Instanceof (JDK 16) 2 | 3 | ** Allows the conditional extraction of components from objects to be expressed more concisely and safely 4 | ** Casting is error-prone, difficult to maintain and has numerous other drawbacks 5 | ** Many languages (Haskell, C#, etc.) support pattern matching for its brevity and safety 6 | 7 | --- 8 | 9 | link:./00_toc.adoc[TOC] / 10 | link:./26_helpful_npe_new_style_1.adoc[Helpful NPE New Style - Contd] / 11 | link:./29_pattern_matching_for_instanceof_old_style.adoc[Patten Matching Old Style] 12 | -------------------------------------------------------------------------------- /docs/02_jmh_tests_intro.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests - Intro 2 | 3 | * link:https://openjdk.org/projects/code-tools/jmh/[JMH - Java Microbenchmark Harness] 4 | * 4 Core Intel i5 Processor 5 | * Measurement Configuration 6 | ** 3 forks, 5 Warm-up iterations, 5 Measurement iterations 7 | ** Measure Reported - Operations per second 8 | ** _Bigger_ numbers are better 9 | * Your Mileage May Vary (YMMV) 10 | ** _Please test, benchmark and profile your application._ 11 | 12 | 13 | --- 14 | link:./00_toc.adoc[TOC] / 15 | link:./01_intro.adoc[Intro] / 16 | link:./03_jmh_tests_measurement_pattern.adoc[JMH Measurement Pattern] 17 | -------------------------------------------------------------------------------- /docs/25_helpful_npe_old_style.adoc: -------------------------------------------------------------------------------- 1 | == Null Pointer Exceptions - Old Style 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | Person person = new Person("Nikhil", "N", null); 6 | String lowercaseCity = person.getAddress().getCity().toLowerCase(); 7 | 8 | /** Prints: 9 | Exception in thread "main" java.lang.NullPointerException 10 | at nikhil.nani.code.examples.ExamplesHelpfulNPEsTest.helpfulNpesTest1(ExamplesHelpfulNPEsTest.java:13) 11 | **/ 12 | ---- 13 | 14 | --- 15 | 16 | link:./00_toc.adoc[TOC] / 17 | link:./24_helpful_npe_intro.adoc[Helpful NPE Intro] / 18 | link:./26_helpful_npe_new_style_1.adoc[Helpful NPE New Style] 19 | -------------------------------------------------------------------------------- /docs/15_var_code.adoc: -------------------------------------------------------------------------------- 1 | == var 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | @Test 7 | public void var() { 8 | Map map = Map.of(1, "One", 2, "Two"); 9 | var withVar = Map.of(1, "One", 2, "Two"); 10 | Assertions.assertEquals(map, withVar); 11 | } 12 | ---- 13 | 14 | ** *Attention*: 15 | *** `var` variable cannot be initialized to `null` 16 | *** `var` cannot be used to assign lambda expressions 17 | *** Avoid `var` with polymorphic code 18 | 19 | --- 20 | link:./00_toc.adoc[TOC] / 21 | link:./14_var_intro.adoc[var Intro] / 22 | link:./16_switch_statements_intro.adoc[Switch Statements Intro] 23 | -------------------------------------------------------------------------------- /docs/20_text_blocks_intro.adoc: -------------------------------------------------------------------------------- 1 | == Text Blocks (JDK 15) 2 | 3 | ** Prior to JDK 15: Non-maintainable string concatenations and delimiters 4 | ** Minimize Java syntax required for a `string` 5 | ** Alternative representation of `string`, hence has all characteristics of `java.lang.String` 6 | ** Improve readability and clarity of code 7 | ** For more info visit: link:https://docs.oracle.com/en/java/javase/15/text-blocks/index.html[Programmer's guide to text blocks] 8 | 9 | --- 10 | link:./00_toc.adoc[TOC] / 11 | link:./19_switch_expressions_yield.adoc[Switch with Yield Syntax] / 12 | link:./21_text_blocks_old_style.adoc[Text Block Old Style] 13 | -------------------------------------------------------------------------------- /docs/23_text_blocks_injecting_variables.adoc: -------------------------------------------------------------------------------- 1 | == Text Blocks - Using `formatted` syntax 2 | 3 | [source,highlight=2..3] 4 | ---- 5 | String textBlockJson = """ 6 | { 7 | "firstName": "%s", 8 | "lastName": "%s", 9 | "city": "%s", 10 | "phone": %d 11 | }"""; 12 | 13 | String formattedTextBlockJson = textBlockJson.formatted( 14 | "Nikhil", 15 | "N", 16 | "Seattle", 17 | 123); 18 | ---- 19 | 20 | --- 21 | 22 | link:./00_toc.adoc[TOC] / 23 | link:./22_text_blocks_new_style.adoc[Text Block New Style] / 24 | link:./24_helpful_npe_intro.adoc[Helpful NPE Intro] 25 | -------------------------------------------------------------------------------- /docs/38_sealed_classes_sealed_interface_hierarchy.adoc: -------------------------------------------------------------------------------- 1 | == Sealed Classes 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | public final class Owner implements User { 7 | } 8 | 9 | ---- 10 | -- 11 | [source,java,highlight=2..3] 12 | ---- 13 | public sealed class Admin implements User permits SuperAdmin { 14 | } 15 | 16 | ---- 17 | 18 | -- 19 | [source,java,highlight=2..3] 20 | ---- 21 | public non-sealed class Resident implements User { 22 | } 23 | ---- 24 | 25 | --- 26 | link:./00_toc.adoc[TOC] / 27 | link:./37_sealed_classes_sealed_interface.adoc[Sealed Interface] / 28 | link:./39_sealed_classes_sealed_class_hierarchy.adoc[Sealed Classes Hierarchy] 29 | -------------------------------------------------------------------------------- /docs/10_memory_tests_intro.adoc: -------------------------------------------------------------------------------- 1 | == Memory Tests - Intro 2 | 3 | * Create a trivial program which loops 5 times and measure memory used after each iteration 4 | * For sake of representation only the memory after 5 iterations is reported 5 | * Measure Reported in Kb: `Used Memory = Total Memory - Free Memory` 6 | * _Smaller_ numbers are better 7 | * 4 Core Intel i5 Processor 8 | * Your Mileage May Vary (YMMV) 9 | ** _Please test, benchmark and profile your application._ 10 | 11 | --- 12 | link:./00_toc.adoc[TOC] / 13 | link:./09_jmh_tests_map_get_string_measurements.adoc[JMH Tests - get() String Measurements] / 14 | link:./11_memory_tests_code.adoc[Memory Tests Code] 15 | -------------------------------------------------------------------------------- /docs/33_records_new_style.adoc: -------------------------------------------------------------------------------- 1 | == Records 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | public record PersonRecord(String firstName, String lastName) { 6 | public String getFullName() { 7 | return firstName + ' ' + lastName; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return "Person{" + 13 | "firstName='" + firstName + '\'' + 14 | ", lastName='" + lastName + '\'' + 15 | '}'; 16 | } 17 | } 18 | ---- 19 | 20 | --- 21 | 22 | link:./00_toc.adoc[TOC] / 23 | link:./32_records_old_style.adoc[Records Old Style] / 24 | link:./34_sealed_classes_intro1.adoc[Sealed Classes - Intro] 25 | -------------------------------------------------------------------------------- /docs/12_memory_tests_measurements.adoc: -------------------------------------------------------------------------------- 1 | == Memory Measurements - Memory Usage 2 | 3 | [%header,cols=">1,>1,>1,>1"] 4 | |=== 5 | |Size (elements)|JDK 8 (Kb)|JDK 11 (Kb)|JDK 17 (Kb) 6 | |10|4061|1,337|*1,265* 7 | |100|4,074|1,350|*1,278* 8 | |1,000|4,195|1,471|*1,399* 9 | |10,000|5,389|2,665|*2,592* 10 | |100,000|18,895|17,914|*17,859* 11 | |1,000,000|148,199|*139,471*|139,550 12 | |=== 13 | 14 | * All data in Kb for memory usage 15 | ** `Used Memory = Total Memory - Free Memory` 16 | * Lower numbers are better 17 | 18 | --- 19 | 20 | link:./00_toc.adoc[TOC] / 21 | link:./11_memory_tests_code.adoc[Memory Tests Code] / 22 | link:./13_performance_tests_summary.adoc[Performance Tests Summary] 23 | -------------------------------------------------------------------------------- /docs/19_switch_expressions_yield.adoc: -------------------------------------------------------------------------------- 1 | == Switch with Yield Syntax 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | private String getQuarterYieldSyntax(Integer monthNumber) { 6 | return switch (monthNumber) { 7 | case 1, 2, 3 -> "Q1"; 8 | case 4, 5, 6 -> "Q2"; 9 | case 7, 8, 9 -> "Q3"; 10 | case 10, 11, 12 -> { 11 | System.out.println("Yay Last Quarter"); 12 | yield "Q4"; 13 | } 14 | default -> "Not a month in year"; 15 | }; 16 | } 17 | ---- 18 | 19 | --- 20 | 21 | link:./00_toc.adoc[TOC] / 22 | link:./18_switch_expressions_updated_style.adoc[Updated Switch Statements] / 23 | link:./20_switch_statements_enums.adoc[Switch with Enums] 24 | -------------------------------------------------------------------------------- /docs/06_jmh_tests_map_put_string_measurements.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests - `Map.put()` for `String` 2 | 3 | [%header,cols=">1,>1,>1,>1"] 4 | |=== 5 | |Size (elements)|JDK 8 (ops/s)|JDK 11 (ops/s)|JDK 17 (ops/s) 6 | |1,000 |29,140.25|*36,068.59*|32,850.46 7 | |10,000|2,528.09|*3,245.80*|3,048.40 8 | |25,000|926.22|*1,030.33*|998.98 9 | |50,000|279.80|324.28|*369.50* 10 | |75,000|154.85|134.66|*189.17* 11 | |100,000|119.88|99.62|*148.22* 12 | |=== 13 | 14 | * All data in ops/second for throughput. 15 | * Higher numbers are better 16 | 17 | --- 18 | 19 | link:./00_toc.adoc[TOC] / 20 | link:./05_jmh_tests_map_put_integer_measurements.adoc[JMH Tests - put() Integer Measurements] / 21 | link:./07_jmh_tests_code_get.adoc[JMH Tests - get()] -------------------------------------------------------------------------------- /docs/09_jmh_tests_map_get_string_measurements.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests - `Map.get()` for `String` 2 | 3 | [%header,cols=">1,>1,>1,>1"] 4 | |=== 5 | |Size (elements)|JDK 8 (ops/s)|JDK 11 (ops/s)|JDK 17 (ops/s) 6 | |1,000 |18,024.75|*31,894.17*|30,521.88 7 | |10,000|1,198.71|*2,742.31*|2,409.98 8 | |25,000|412.10|*847.56*|760.01 9 | |50,000|206.09|282.92|*322.42* 10 | |75,000|119.09|150.88|*174.60* 11 | |100,000|87.90|115.81|*129.45* 12 | |=== 13 | 14 | * All data in ops/second for throughput. 15 | * Higher numbers are better 16 | 17 | --- 18 | 19 | link:./00_toc.adoc[TOC] / 20 | link:./08_jmh_tests_map_get_integer_measurements.adoc[JMH Tests - get() Integer Measurements] / 21 | link:./10_memory_tests_intro.adoc[Memory Tests Intro] 22 | -------------------------------------------------------------------------------- /docs/20_switch_statements_enums.adoc: -------------------------------------------------------------------------------- 1 | == Switch with Enums 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | public enum Months { 6 | JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC 7 | } 8 | 9 | private String getQuarterEnum(Months month) { 10 | return switch (month) { 11 | case JAN, FEB, MAR -> "Q1"; 12 | case APR, MAY, JUN -> "Q2"; 13 | case JUL, AUG, SEP -> "Q3"; 14 | case OCT, NOV, DEC -> "Q4"; 15 | // No need for default as all possible values are covered (exhaustive). 16 | }; 17 | } 18 | ---- 19 | 20 | --- 21 | 22 | link:./00_toc.adoc[TOC] / 23 | link:./19_switch_expressions_yield.adoc[Switch with Yield Syntax] / 24 | link:./20_text_blocks_intro.adoc[Text Blocks Intro] 25 | -------------------------------------------------------------------------------- /docs/31_records_intro.adoc: -------------------------------------------------------------------------------- 1 | == Records (JDK 16) 2 | 3 | ** Ability to model data using classes 4 | ** Prior to Records: classes with boilerplate code 5 | ** Fields are `private final` (shallow immutable) 6 | ** Cannot declare instance fields 7 | ** Default implementation for accessors, hashcode, toString, equals, automatically generated 8 | ** Can override default implementations 9 | ** Can write custom method definitions 10 | ** For more info visit: link:https://www.infoq.com/articles/data-oriented-programming-java/[Data Oriented Programming in Java] 11 | 12 | --- 13 | link:./00_toc.adoc[TOC] / 14 | link:./30_pattern_matching_for_instanceof_new_style.adoc[Pattern Matching New Style] / 15 | link:./32_records_old_style.adoc[Records Old Style] 16 | -------------------------------------------------------------------------------- /docs/04_jmh_tests_code_put.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests: `Map.put()` Code 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | @Benchmark 7 | public int jdkIntegerPut() { 8 | this.elements.forEach(each -> this.jdkInteger.put(each, each)); 9 | return this.jdkInteger.size(); 10 | } 11 | 12 | @Benchmark 13 | public int jdkStringPut() { 14 | this.elements.forEach(each -> { 15 | String string = String.valueOf(each); 16 | this.jdkString.put(string, string); 17 | }); 18 | return this.jdkString.size(); 19 | } 20 | ---- 21 | 22 | --- 23 | link:./00_toc.adoc[TOC] / 24 | link:./03_jmh_tests_measurement_pattern.adoc[JMH Measurement Pattern] / 25 | link:./05_jmh_tests_map_put_integer_measurements.adoc[JMH Tests - put() Integer Measurements] 26 | -------------------------------------------------------------------------------- /docs/05_jmh_tests_map_put_integer_measurements.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests - `Map.put()` for `Integer` 2 | 3 | [%header,cols=">1,>1,>1,>1"] 4 | |=== 5 | |Size (elements)|JDK 8 (ops/s)|JDK 11 (ops/s)|JDK 17 (ops/s) 6 | |1,000 |125,791.09|111,523.32|*218,755.79* 7 | |10,000|11,435.21|12,177.52|*14,726.91* 8 | |25,000|4,718.32|4,254.20|*6,085.78* 9 | |50,000|2,343.39|2,454.25|*2,897.75* 10 | |75,000|1,533.04|1,672.23|*1,850.43* 11 | |100,000|1,097.44|1,006.22|*1,378.68* 12 | |=== 13 | 14 | * All data in ops/second for throughput. 15 | * Higher numbers are better 16 | 17 | --- 18 | 19 | link:./00_toc.adoc[TOC] / 20 | link:./04_jmh_tests_code_put.adoc[JMH Tests - put()] / 21 | link:./06_jmh_tests_map_put_string_measurements.adoc[JMH Tests - put() String Measurements] 22 | -------------------------------------------------------------------------------- /docs/08_jmh_tests_map_get_integer_measurements.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests - `Map.get()` for `Integer` 2 | 3 | [%header,cols=">1,>1,>1,>1"] 4 | |=== 5 | |Size (elements)|JDK 8 (ops/s)|JDK 11 (ops/s)|JDK 17 (ops/s) 6 | |1,000 |187,321.28|188,308.11|*234,226.57* 7 | |10,000|17,498.25|17,902.81|*21,941.64* 8 | |25,000|6,282.48|*6,922.84*|6,759.26 9 | |50,000|1,802.16|1,932.40|*1,975.21* 10 | |75,000|1,079.53|2,292.85|*2,969.34* 11 | |100,000|1,225.75|1,262.62|*1,327.07* 12 | |=== 13 | 14 | * All data in ops/second for throughput. 15 | * Higher numbers are better 16 | 17 | --- 18 | 19 | link:./00_toc.adoc[TOC] / 20 | link:./07_jmh_tests_code_get.adoc[JMH Tests - get()] / 21 | link:./09_jmh_tests_map_get_string_measurements.adoc[JMH Tests - get() String Measurements] 22 | -------------------------------------------------------------------------------- /docs/26_helpful_npe_new_style_1.adoc: -------------------------------------------------------------------------------- 1 | == Helpful Null Pointer Exceptions - 1 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | Person person = new Person("Nikhil", "N", null); 6 | String lowercaseCity = person.address().city().toLowerCase(); 7 | 8 | /** Prints: 9 | java.lang.NullPointerException: Cannot invoke "nikhil.nani.code.examples.ExamplesHelpfulNPEsTest$Address.city()" because the return value of "nikhil.nani.code.examples.ExamplesHelpfulNPEsTest$Person.address()" is null 10 | at nikhil.nani.code.examples.ExamplesHelpfulNPEsTest.helpfulNpesTest1(ExamplesHelpfulNPEsTest.java:13) 11 | **/ 12 | ---- 13 | 14 | --- 15 | 16 | link:./00_toc.adoc[TOC] / 17 | link:./25_helpful_npe_old_style.adoc[NPEs Old Style] / 18 | link:./27_helpful_npe_new_style_2.adoc[Helpful NPE New Style - Contd] -------------------------------------------------------------------------------- /docs/27_helpful_npe_new_style_2.adoc: -------------------------------------------------------------------------------- 1 | == Helpful Null Pointer Exceptions - 2 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | Person person = new Person("Nikhil", "N", new Address("", "", null)); 6 | String lowercaseCity = person.address().city().toLowerCase(); 7 | 8 | /** Prints: 9 | java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because the return value of "nikhil.nani.code.examples.ExamplesHelpfulNPEsTest$Address.city()" is null 10 | at nikhil.nani.code.examples.ExamplesHelpfulNPEsTest.helpfulNpesTest2(ExamplesHelpfulNPEsTest.java:23) 11 | **/ 12 | ---- 13 | 14 | --- 15 | 16 | link:./00_toc.adoc[TOC] / 17 | link:./26_helpful_npe_new_style_1.adoc[Helpful NPE New Style] / 18 | link:./28_pattern_matching_for_instanceof_intro.adoc[Pattern Matching for instanceof Intro] 19 | -------------------------------------------------------------------------------- /docs/39_sealed_classes_sealed_class_hierarchy.adoc: -------------------------------------------------------------------------------- 1 | == Class Hierarchy 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | public sealed class Admin implements User permits SuperAdmin { 7 | } 8 | 9 | ---- 10 | 11 | -- 12 | [source,java,highlight=2..3] 13 | ---- 14 | public final class SuperAdmin extends Admin { 15 | } 16 | // This test passes 17 | Assertions.assertEquals("Logged in", superAdmin.login()); 18 | 19 | // Below class TempAdmin does not compile as TempAdmin is not permitted in the sealed hierarchy 20 | public final class TempAdmin extends Admin { 21 | } 22 | ---- 23 | 24 | --- 25 | link:./00_toc.adoc[TOC] / 26 | link:./38_sealed_classes_sealed_interface_hierarchy.adoc[Sealed Interface Hierarchy] / 27 | link:./40_sealed_classes_sealed_non_sealed_hierarchy.adoc[Non-Sealed Classes Hierarchy] 28 | -------------------------------------------------------------------------------- /docs/40_sealed_classes_sealed_non_sealed_hierarchy.adoc: -------------------------------------------------------------------------------- 1 | == Class Hierarchy 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | public non-sealed class Resident implements User { 7 | } 8 | ---- 9 | 10 | [source,java,highlight=2..3] 11 | ---- 12 | // Since Resident is non-sealed, it is open for any number of sub-classes, hence it opens up the hierarchy 13 | public final class DayGuest extends Resident { 14 | } 15 | 16 | public final class OvernightGuest extends Resident { 17 | } 18 | // These tests passed 19 | Assertions.assertEquals("Logged in", dayGuest.login()); 20 | Assertions.assertEquals("Logged in", overnightGuest.login()); 21 | ---- 22 | 23 | --- 24 | 25 | link:./00_toc.adoc[TOC] / 26 | link:./39_sealed_classes_sealed_class_hierarchy.adoc[Sealed Classes Hierarchy] / 27 | link:./41_removals.adoc[Removals] 28 | -------------------------------------------------------------------------------- /docs/35_sealed_classes_intro2.adoc: -------------------------------------------------------------------------------- 1 | == Sealed Classes (JDK 17) 2 | 3 | ** With sealed classes we get the following modifiers and clauses 4 | *** `sealed`: can be extended or implemented only by those classes and interfaces permitted to do so 5 | *** `non-sealed`: allows to open up a part of the hierarchy without exposing the entire hierarchy 6 | *** `permits`: specifies the subclasses that can extend the sealed class 7 | ** If subclasses are in different files in the same module or package, then use `permits` to define the subclasses that may extend the sealed class 8 | ** If subclasses are in the same file as `sealed` superclass, then `permits` clause is not needed 9 | 10 | --- 11 | link:./00_toc.adoc[TOC] / 12 | link:./34_sealed_classes_intro1.adoc[Sealed Classes - Intro] / 13 | link:./36_sealed_classes_sealed_class_diagram.adoc[Hierarchy Diagram] 14 | -------------------------------------------------------------------------------- /performance-tests-jdk17/src/main/java/nikhil/nani/JmhRunner17.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.Mode; 4 | import org.openjdk.jmh.runner.Runner; 5 | import org.openjdk.jmh.runner.RunnerException; 6 | import org.openjdk.jmh.runner.options.Options; 7 | import org.openjdk.jmh.runner.options.OptionsBuilder; 8 | 9 | public class JmhRunner17 { 10 | public static void main(String[] args) throws RunnerException { 11 | Options opt = new OptionsBuilder() 12 | .include(MapGetJmhJdk17Tests.class.getSimpleName()) 13 | .include(MapPutJmhJdk17Tests.class.getSimpleName()) 14 | .forks(3) 15 | .warmupIterations(5) 16 | .measurementIterations(5) 17 | .mode(Mode.Throughput) 18 | .build(); 19 | 20 | new Runner(opt).run(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /performance-tests-jdk8/src/main/java/nikhil/nani/JmhRunnerJdk8.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.Mode; 4 | import org.openjdk.jmh.runner.Runner; 5 | import org.openjdk.jmh.runner.RunnerException; 6 | import org.openjdk.jmh.runner.options.Options; 7 | import org.openjdk.jmh.runner.options.OptionsBuilder; 8 | 9 | public class JmhRunnerJdk8 { 10 | public static void main(String[] args) throws RunnerException { 11 | Options opt = new OptionsBuilder() 12 | .include(MapGetJmhJdk8Tests.class.getSimpleName()) 13 | .include(MapPutJmhJdk8Tests.class.getSimpleName()) 14 | .forks(3) 15 | .warmupIterations(5) 16 | .measurementIterations(5) 17 | .mode(Mode.Throughput) 18 | .build(); 19 | 20 | new Runner(opt).run(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /performance-tests-jdk11/src/main/java/nikhil/nani/JmhRunnerJdk11.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.Mode; 4 | import org.openjdk.jmh.runner.Runner; 5 | import org.openjdk.jmh.runner.RunnerException; 6 | import org.openjdk.jmh.runner.options.Options; 7 | import org.openjdk.jmh.runner.options.OptionsBuilder; 8 | 9 | public class JmhRunnerJdk11 { 10 | public static void main(String[] args) throws RunnerException { 11 | Options opt = new OptionsBuilder() 12 | .include(MapGetJmhJdk11Tests.class.getSimpleName()) 13 | .include(MapPutJmhJdk11Tests.class.getSimpleName()) 14 | .forks(3) 15 | .warmupIterations(5) 16 | .measurementIterations(5) 17 | .mode(Mode.Throughput) 18 | .build(); 19 | 20 | new Runner(opt).run(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /docs/29_pattern_matching_for_instanceof_old_style.adoc: -------------------------------------------------------------------------------- 1 | == Pattern Matching for Instanceof - Casting 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | private String withoutPatternMatching(Object input) { 6 | if (input instanceof String) { 7 | String string = (String) input; 8 | return "This is a String:" + string; 9 | } 10 | if (input instanceof Integer) { 11 | Integer integer = (Integer) input; 12 | return "This is an Integer:" + integer; 13 | } 14 | if (input instanceof Double) { 15 | Double dbl = (Double) input; 16 | return "This is a Double:" + dbl; 17 | } 18 | return ""; 19 | } 20 | ---- 21 | 22 | --- 23 | 24 | link:./00_toc.adoc[TOC] / 25 | link:./28_pattern_matching_for_instanceof_intro.adoc[Pattern Matching for instanceof Intro] / 26 | link:./30_pattern_matching_for_instanceof_new_style.adoc[Pattern Matching New Style] 27 | -------------------------------------------------------------------------------- /docs/11_memory_tests_code.adoc: -------------------------------------------------------------------------------- 1 | == Memory Tests - Code 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | for (int k = 0; k < 5; k++) { 7 | Map stringMap = new HashMap<>(); 8 | List stringList = new ArrayList<>(); 9 | Set stringSet = new HashSet<>(); 10 | 11 | for (int i = 0; i < 7; i++) { 12 | int size = Double.valueOf(Math.pow(10, i)).intValue(); 13 | for (int j = 0; j < size; j++) { 14 | String aString = String.valueOf(j); 15 | stringMap.put(aString, aString); 16 | stringList.add(aString); 17 | stringSet.add(aString); 18 | } 19 | printMemoryUtilizationUsingRuntime("String", size); 20 | } 21 | } 22 | ---- 23 | --- 24 | link:./00_toc.adoc[TOC] / 25 | link:./10_memory_tests_intro.adoc[Memory Tests Intro] / 26 | link:./12_memory_tests_measurements.adoc[Memory Tests Measurements] 27 | -------------------------------------------------------------------------------- /docs/07_jmh_tests_code_get.adoc: -------------------------------------------------------------------------------- 1 | == JMH Tests: `Map.get()` Code 2 | 3 | -- 4 | [source,java,highlight=2..3] 5 | ---- 6 | @Benchmark 7 | public int jdkIntegerGet() { 8 | this.elements.forEach(each -> 9 | { 10 | if (this.jdkInteger.get(each) != -each) { 11 | throw new IllegalStateException(); 12 | } 13 | }); 14 | return this.jdkInteger.size(); 15 | } 16 | 17 | @Benchmark 18 | public int jdkStringGet() { 19 | this.elements.forEach(each -> 20 | { 21 | if (!this.jdkString.get(String.valueOf(each)).equals(String.valueOf(-each))) { 22 | throw new IllegalStateException(); 23 | } 24 | }); 25 | return this.jdkString.size(); 26 | } 27 | ---- 28 | 29 | --- 30 | link:./00_toc.adoc[TOC] / 31 | link:./06_jmh_tests_map_put_string_measurements.adoc[JMH Tests - put() String Measurements] / 32 | link:./08_jmh_tests_map_get_integer_measurements.adoc[JMH Tests - get() Integer Measurements] 33 | 34 | -------------------------------------------------------------------------------- /docs/30_pattern_matching_for_instanceof_new_style.adoc: -------------------------------------------------------------------------------- 1 | == Pattern Matching for Instanceof 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | // Note each pattern variable's scope is only within the respective flow 6 | private String withPatternMatching(Object input) { 7 | if (input instanceof String aString) { 8 | // Scope of aString is only within this block 9 | return "This is a String:" + aString; 10 | } 11 | if (input instanceof Integer anInteger) { 12 | // Scope of anInteger is only within this block 13 | return "This is an Integer:" + anInteger; 14 | } 15 | if (input instanceof Double aDouble) { 16 | // Scope of aDouble is only within this block 17 | return "This is a Double:" + aDouble; 18 | } 19 | return ""; 20 | } 21 | ---- 22 | 23 | --- 24 | 25 | link:./00_toc.adoc[TOC] / 26 | link:./29_pattern_matching_for_instanceof_old_style.adoc[Patten Matching Old Style] / 27 | link:./31_records_intro.adoc[Records Intro] 28 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesHelpfulNPEsTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExamplesHelpfulNPEsTest { 7 | 8 | @Test 9 | public void helpfulNpesTest1() { 10 | 11 | Person person = new Person("Nikhil", "N", null); 12 | 13 | String lowercaseCity = person.address().city().toLowerCase(); 14 | 15 | Assertions.fail("Should not reach here"); 16 | } 17 | 18 | @Test 19 | public void helpfulNpesTest2() { 20 | 21 | Person person = new Person("Nikhil", "N", new Address("", "", null)); 22 | 23 | String lowercaseCity = person.address().city().toLowerCase(); 24 | 25 | Assertions.fail("Should not reach here"); 26 | } 27 | 28 | record Person(String firstName, String lastName, Address address) { 29 | 30 | } 31 | 32 | record Address(String line1, String line2, String city) { 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /docs/18_switch_expressions_updated_style.adoc: -------------------------------------------------------------------------------- 1 | == Switch Expressions 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | private String getQuarterNewSwitch(Integer monthNumber) { 6 | return switch (monthNumber) { 7 | case 1, 2, 3 -> "Q1"; 8 | case 4, 5, 6 -> "Q2"; 9 | case 7, 8, 9 -> "Q3"; 10 | case 10, 11, 12 -> "Q4"; 11 | default -> "Not a month in year"; 12 | }; 13 | } 14 | ---- 15 | 16 | [source,java,highlight=2..3] 17 | ---- 18 | private String getQuarterNewSwitch(Integer monthNumber) { 19 | final String quarter = switch (monthNumber) { 20 | case 1, 2, 3 -> "Q1"; 21 | case 4, 5, 6 -> "Q2"; 22 | case 7, 8, 9 -> "Q3"; 23 | case 10, 11, 12 -> "Q4"; 24 | default -> "Not a month in year"; 25 | }; 26 | return quarter; 27 | } 28 | ---- 29 | 30 | --- 31 | 32 | link:./00_toc.adoc[TOC] / 33 | link:./17_switch_statements_old_style.adoc[Switch Statements Old Style] / 34 | link:./19_switch_expressions_yield.adoc[Switch with Yield Syntax] 35 | -------------------------------------------------------------------------------- /docs/17_switch_statements_old_style.adoc: -------------------------------------------------------------------------------- 1 | == Old switch-case syntax 2 | 3 | [source,java,highlight=2..3] 4 | ---- 5 | private String getQuarterOldSwitch(Integer monthNumber) { 6 | switch (monthNumber) { 7 | case 1: 8 | return "Q1"; 9 | case 2: 10 | return "Q1"; 11 | case 3: 12 | return "Q1"; 13 | case 4: 14 | return "Q2"; 15 | case 5: 16 | return "Q2"; 17 | case 6: 18 | return "Q2"; 19 | case 7: 20 | return "Q3"; 21 | case 8: 22 | return "Q3"; 23 | case 9: 24 | return "Q3"; 25 | case 10: 26 | return "Q4"; 27 | case 11: 28 | return "Q4"; 29 | case 12: 30 | return "Q4"; 31 | default: 32 | return "Not a month in year"; 33 | } 34 | } 35 | ---- 36 | 37 | --- 38 | 39 | link:./00_toc.adoc[TOC] / 40 | link:./16_switch_statements_intro.adoc[Switch Statements Intro] / 41 | link:./18_switch_expressions_updated_style.adoc[Switch Expressions] 42 | -------------------------------------------------------------------------------- /code-examples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | nikhil.nani 9 | java-upgrade 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | nikhil.nani 14 | code-examples 15 | 1.0-SNAPSHOT 16 | Code Examples 17 | 18 | 19 | 17 20 | 21 | 17 22 | 17 23 | UTF-8 24 | 25 | 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /performance-tests-jdk17/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java-upgrade 7 | nikhil.nani 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | performance-tests-jdk17 13 | 14 | 15 | 17 16 | 17 17 | UTF-8 18 | 19 | 20 | 21 | 22 | 23 | org.openjdk.jmh 24 | jmh-core 25 | 26 | 27 | 28 | org.openjdk.jmh 29 | jmh-generator-annprocess 30 | provided 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /performance-tests-jdk11/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java-upgrade 7 | nikhil.nani 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | performance-tests-jdk11 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | 19 | 20 | 21 | 22 | 23 | org.openjdk.jmh 24 | jmh-core 25 | 26 | 27 | 28 | org.openjdk.jmh 29 | jmh-generator-annprocess 30 | provided 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/sealed/classes/ExamplesSealedClassesTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples.sealed.classes; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExamplesSealedClassesTest { 7 | 8 | @Test 9 | public void test() { 10 | 11 | Admin admin = new Admin(); 12 | Assertions.assertEquals("Logged in", admin.login()); 13 | Assertions.assertEquals("Password changed", admin.changePassword()); 14 | 15 | SuperAdmin superAdmin = new SuperAdmin(); 16 | 17 | Assertions.assertEquals("Logged in", superAdmin.login()); 18 | Assertions.assertEquals("Password changed", superAdmin.changePassword()); 19 | 20 | Resident resident = new Resident(); 21 | Assertions.assertEquals("Logged in", resident.login()); 22 | 23 | DayGuest dayGuest = new DayGuest(); 24 | Assertions.assertEquals("Logged in", dayGuest.login()); 25 | 26 | OvernightGuest overnightGuest = new OvernightGuest(); 27 | Assertions.assertEquals("Logged in", overnightGuest.login()); 28 | 29 | Owner owner = new Owner(); 30 | Assertions.assertEquals("Logged in", owner.login()); 31 | } 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /performance-tests-jdk8/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java-upgrade 7 | nikhil.nani 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | performance-tests-jdk8 13 | 14 | 15 | 1.23 16 | 17 | 1.8 18 | 1.8 19 | UTF-8 20 | 21 | 22 | 23 | 24 | 25 | org.openjdk.jmh 26 | jmh-core 27 | ${jmh.version} 28 | 29 | 30 | 31 | org.openjdk.jmh 32 | jmh-generator-annprocess 33 | ${jmh.version} 34 | provided 35 | 36 | 37 | -------------------------------------------------------------------------------- /performance-tests-jdk8/src/main/java/nikhil/nani/MemoryTestsJdk8.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import java.util.*; 4 | 5 | public class MemoryTestsJdk8 { 6 | public static void main(String[] args) throws InterruptedException { 7 | 8 | 9 | for (int k = 0; k < 5; k++) { 10 | 11 | Map stringMap = new HashMap<>(); 12 | List stringList = new ArrayList<>(); 13 | Set stringSet = new HashSet<>(); 14 | 15 | for (int i = 0; i < 7; i++) { 16 | int size = Double.valueOf(Math.pow(10, i)).intValue(); 17 | 18 | for (int j = 0; j < size; j++) { 19 | String aString = String.valueOf(j); 20 | stringMap.put(aString, aString); 21 | stringList.add(aString); 22 | stringSet.add(aString); 23 | } 24 | 25 | printMemoryUtilizationUsingRuntime("String", size); 26 | } 27 | System.out.println("-----------------------------------------------------------"); 28 | } 29 | } 30 | 31 | public static void printMemoryUtilizationUsingRuntime(String type, int size) throws InterruptedException { 32 | System.gc(); 33 | Thread.sleep(1000); 34 | System.gc(); 35 | Thread.sleep(1000); 36 | System.gc(); 37 | Thread.sleep(1000); 38 | 39 | System.out.println("JDK8 " + type + " Size:" + size + " Memory:" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " Kb"); 40 | } 41 | } -------------------------------------------------------------------------------- /performance-tests-jdk17/src/main/java/nikhil/nani/MemoryTestsJdk17.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import java.util.*; 4 | 5 | public class MemoryTestsJdk17 { 6 | public static void main(String[] args) throws InterruptedException { 7 | 8 | 9 | for (int k = 0; k < 5; k++) { 10 | 11 | Map stringMap = new HashMap<>(); 12 | List stringList = new ArrayList<>(); 13 | Set stringSet = new HashSet<>(); 14 | 15 | for (int i = 0; i < 7; i++) { 16 | int size = Double.valueOf(Math.pow(10, i)).intValue(); 17 | 18 | for (int j = 0; j < size; j++) { 19 | String aString = String.valueOf(j); 20 | stringMap.put(aString, aString); 21 | stringList.add(aString); 22 | stringSet.add(aString); 23 | } 24 | 25 | printMemoryUtilizationUsingRuntime("String", size); 26 | } 27 | System.out.println("-----------------------------------------------------------"); 28 | } 29 | } 30 | 31 | public static void printMemoryUtilizationUsingRuntime(String type, int size) throws InterruptedException { 32 | System.gc(); 33 | Thread.sleep(1000); 34 | System.gc(); 35 | Thread.sleep(1000); 36 | System.gc(); 37 | Thread.sleep(1000); 38 | 39 | System.out.println("JDK17 " + type + " Size:" + size + " Memory:" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " Kb"); 40 | } 41 | } -------------------------------------------------------------------------------- /performance-tests-jdk11/src/main/java/nikhil/nani/MemoryTestsJdk11.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import java.util.*; 4 | 5 | public class MemoryTestsJdk11 { 6 | public static void main(String[] args) throws InterruptedException { 7 | 8 | 9 | for (int k = 0; k < 5; k++) { 10 | 11 | Map stringMap = new HashMap<>(); 12 | List stringList = new ArrayList<>(); 13 | Set stringSet = new HashSet<>(); 14 | 15 | for (int i = 0; i < 7; i++) { 16 | int size = Double.valueOf(Math.pow(10, i)).intValue(); 17 | 18 | for (int j = 0; j < size; j++) { 19 | String aString = String.valueOf(j); 20 | stringMap.put(aString, aString); 21 | stringList.add(aString); 22 | stringSet.add(aString); 23 | } 24 | 25 | printMemoryUtilizationUsingRuntime("String", size); 26 | } 27 | System.out.println("-----------------------------------------------------------"); 28 | } 29 | } 30 | 31 | public static void printMemoryUtilizationUsingRuntime(String type, int size) throws InterruptedException { 32 | System.gc(); 33 | Thread.sleep(1000); 34 | System.gc(); 35 | Thread.sleep(1000); 36 | System.gc(); 37 | Thread.sleep(1000); 38 | 39 | System.out.println("JDK11 " + type + " Size:" + size + " Memory:" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " Kb"); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /performance-tests-jdk8/src/main/java/nikhil/nani/MapPutJmhJdk8Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapPutJmhJdk8Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | } 34 | 35 | @Benchmark 36 | public int jdkIntegerPut() { 37 | this.elements.forEach(each -> this.jdkInteger.put(each, each)); 38 | return this.jdkInteger.size(); 39 | } 40 | 41 | @Benchmark 42 | public int jdkStringPut() { 43 | this.elements.forEach(each -> { 44 | String string = String.valueOf(each); 45 | this.jdkString.put(string, string); 46 | }); 47 | return this.jdkString.size(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /performance-tests-jdk11/src/main/java/nikhil/nani/MapPutJmhJdk11Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapPutJmhJdk11Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | } 34 | 35 | @Benchmark 36 | public int jdkIntegerPut() { 37 | this.elements.forEach(each -> this.jdkInteger.put(each, each)); 38 | return this.jdkInteger.size(); 39 | } 40 | 41 | @Benchmark 42 | public int jdkStringPut() { 43 | this.elements.forEach(each -> { 44 | String string = String.valueOf(each); 45 | this.jdkString.put(string, string); 46 | }); 47 | return this.jdkString.size(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /performance-tests-jdk17/src/main/java/nikhil/nani/MapPutJmhJdk17Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapPutJmhJdk17Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | } 34 | 35 | @Benchmark 36 | public int jdkIntegerPut() { 37 | this.elements.forEach(each -> this.jdkInteger.put(each, each)); 38 | return this.jdkInteger.size(); 39 | } 40 | 41 | @Benchmark 42 | public int jdkStringPut() { 43 | this.elements.forEach(each -> { 44 | String string = String.valueOf(each); 45 | this.jdkString.put(string, string); 46 | }); 47 | return this.jdkString.size(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /docs/42b_garbage_collectors_notes.adoc: -------------------------------------------------------------------------------- 1 | == Garbage Collectors 2 | 3 | ** Serial GC (JDK 1) 4 | *** Simplest and the oldest GC mainly designed for a single thread environment 5 | *** Uses a single thread for garbage collection 6 | *** Not a good idea to use it in multi-threaded applications like server environments 7 | 8 | --- 9 | 10 | ** Parallel GC (JDK 1.4) 11 | *** Uses multiple threads for garbage collection 12 | *** Freezes all application threads while performing garbage collection (similar to Serial GC) 13 | 14 | --- 15 | 16 | ** CMS GC (Removed in JDK 14) 17 | *** Uses multiple GC threads for garbage collection 18 | *** Removed in JDK 14 19 | 20 | --- 21 | 22 | ** G1 GC (JDK 8) 23 | *** Partitions heap in to set of equal sized heap regions and performs garbage collection within them in parallel 24 | *** Stands for Garbage First (G1) 25 | 26 | --- 27 | 28 | ** ZGC (JDK 16) 29 | *** Performs work concurrently in it's own threads without stopping execution of application threads for more than 10 ms 30 | *** Suitable for low latency, very large heap (multi-terabytes) 31 | 32 | --- 33 | 34 | ** Shenandoah GC (JDK 17 / JDK 11.0.14) 35 | *** Ultra low pause time GC 36 | *** Processing happens in multiple small phases mostly concurrent with the application 37 | *** Aggressively compacts the heap in parallel with application threads 38 | *** Provides a variety of tuning options to configure GC behavior based on application needs 39 | 40 | --- 41 | 42 | ** Epsilon (Experimental as of Sept 2022) 43 | *** Passive garbage collector: Allocates the memory but does not collect unused objects 44 | *** Epsilon GC allows applications to run out of memory and crash 45 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesPatternMatchingTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExamplesPatternMatchingTest { 7 | 8 | @Test 9 | public void patternMatching() { 10 | Assertions.assertEquals(this.withoutPatternMatching("ABC"), this.withPatternMatching("ABC")); 11 | Assertions.assertEquals(this.withoutPatternMatching(123), this.withPatternMatching(123)); 12 | Assertions.assertEquals(this.withoutPatternMatching(123.456), this.withPatternMatching(123.456)); 13 | 14 | } 15 | 16 | private String withoutPatternMatching(Object input) { 17 | 18 | if (input instanceof String) { 19 | String string = (String) input; 20 | return "This is a String:" + string; 21 | } 22 | if (input instanceof Integer) { 23 | Integer integer = (Integer) input; 24 | return "This is an Integer:" + integer; 25 | } 26 | if (input instanceof Double) { 27 | Double dbl = (Double) input; 28 | return "This is a Double:" + dbl; 29 | } 30 | return ""; 31 | } 32 | 33 | private String withPatternMatching(Object input) { 34 | 35 | if (input instanceof String string) { 36 | return "This is a String:" + string; 37 | } 38 | if (input instanceof Integer integer) { 39 | return "This is an Integer:" + integer; 40 | } 41 | if (input instanceof Double dbl) { 42 | return "This is a Double:" + dbl; 43 | } 44 | 45 | return ""; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesTextBlockTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExamplesTextBlockTest { 7 | 8 | @Test 9 | public void json() { 10 | String json = "{\n" + 11 | " \"firstName\": \"Nikhil\",\n" + 12 | " \"lastName\": \"N\",\n" + 13 | " \"city\": \"Seattle\",\n" + 14 | " \"phone\": 123\n" + 15 | "}"; 16 | 17 | String textBlockJson = """ 18 | { 19 | "firstName": "Nikhil", 20 | "lastName": "N", 21 | "city": "Seattle", 22 | "phone": 123 23 | }"""; 24 | 25 | Assertions.assertEquals(json, textBlockJson); 26 | } 27 | 28 | @Test 29 | public void formattedJson() { 30 | String json = "{\n" + 31 | " \"firstName\": \"Nikhil\",\n" + 32 | " \"lastName\": \"N\",\n" + 33 | " \"city\": \"Seattle\",\n" + 34 | " \"phone\": 123\n" + 35 | "}"; 36 | 37 | String textBlockJson = """ 38 | { 39 | "firstName": "%s", 40 | "lastName": "%s", 41 | "city": "%s", 42 | "phone": %d 43 | }"""; 44 | 45 | String formattedTextBlockJson = textBlockJson.formatted( 46 | "Nikhil", 47 | "N", 48 | "Seattle", 49 | 123); 50 | 51 | Assertions.assertEquals(json, formattedTextBlockJson); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /performance-tests-jdk11/src/main/java/nikhil/nani/MapGetJmhJdk11Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapGetJmhJdk11Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | this.elements.forEach(each -> 34 | { 35 | jdkInteger.put(each, -each); 36 | jdkString.put(String.valueOf(each), String.valueOf(-each)); 37 | }); 38 | } 39 | 40 | @Benchmark 41 | public int jdkIntegerGet() { 42 | this.elements.forEach(each -> 43 | { 44 | if (this.jdkInteger.get(each) != -each) { 45 | throw new IllegalStateException(); 46 | } 47 | }); 48 | return this.jdkInteger.size(); 49 | } 50 | 51 | @Benchmark 52 | public int jdkStringGet() { 53 | this.elements.forEach(each -> 54 | { 55 | if (!this.jdkString.get(String.valueOf(each)).equals(String.valueOf(-each))) { 56 | throw new IllegalStateException(); 57 | } 58 | }); 59 | return this.jdkString.size(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /performance-tests-jdk17/src/main/java/nikhil/nani/MapGetJmhJdk17Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapGetJmhJdk17Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | this.elements.forEach(each -> 34 | { 35 | jdkInteger.put(each, -each); 36 | jdkString.put(String.valueOf(each), String.valueOf(-each)); 37 | }); 38 | } 39 | 40 | @Benchmark 41 | public int jdkIntegerGet() { 42 | this.elements.forEach(each -> 43 | { 44 | if (this.jdkInteger.get(each) != -each) { 45 | throw new IllegalStateException(); 46 | } 47 | }); 48 | return this.jdkInteger.size(); 49 | } 50 | 51 | @Benchmark 52 | public int jdkStringGet() { 53 | this.elements.forEach(each -> 54 | { 55 | if (!this.jdkString.get(String.valueOf(each)).equals(String.valueOf(-each))) { 56 | throw new IllegalStateException(); 57 | } 58 | }); 59 | return this.jdkString.size(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /performance-tests-jdk8/src/main/java/nikhil/nani/MapGetJmhJdk8Tests.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani; 2 | 3 | import org.openjdk.jmh.annotations.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.IntStream; 11 | 12 | @State(Scope.Thread) 13 | @BenchmarkMode(Mode.Throughput) 14 | @OutputTimeUnit(TimeUnit.SECONDS) 15 | @Fork(3) 16 | @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 17 | @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 18 | public class MapGetJmhJdk8Tests { 19 | 20 | @Param({"1000", "10000", "25000", "50000", "75000", "100000"}) 21 | public int size; 22 | 23 | private Map jdkInteger; 24 | private Map jdkString; 25 | 26 | private List elements; 27 | 28 | @Setup 29 | public void setUp() { 30 | this.elements = IntStream.range(0, this.size).boxed().collect(Collectors.toList()); 31 | this.jdkInteger = new HashMap<>(); 32 | this.jdkString = new HashMap<>(); 33 | this.elements.forEach(each -> 34 | { 35 | jdkInteger.put(each, -each); 36 | jdkString.put(String.valueOf(each), String.valueOf(-each)); 37 | }); 38 | } 39 | 40 | @Benchmark 41 | public int jdkIntegerGet() { 42 | this.elements.forEach(each -> 43 | { 44 | if (this.jdkInteger.get(each) != -each) { 45 | throw new IllegalStateException(); 46 | } 47 | }); 48 | return this.jdkInteger.size(); 49 | } 50 | 51 | @Benchmark 52 | public int jdkStringGet() { 53 | this.elements.forEach(each -> 54 | { 55 | if (!this.jdkString.get(String.valueOf(each)).equals(String.valueOf(-each))) { 56 | throw new IllegalStateException(); 57 | } 58 | }); 59 | return this.jdkString.size(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /performance-measurements/jdk8-jmh-tests.txt: -------------------------------------------------------------------------------- 1 | Benchmark (size) Mode Cnt Score Error Units 2 | MapGetJmhJdk8Tests.jdkIntegerGet 1000 thrpt 15 187321.281 ± 5032.989 ops/s 3 | MapGetJmhJdk8Tests.jdkIntegerGet 10000 thrpt 15 17498.247 ± 531.044 ops/s 4 | MapGetJmhJdk8Tests.jdkIntegerGet 25000 thrpt 15 6282.480 ± 167.902 ops/s 5 | MapGetJmhJdk8Tests.jdkIntegerGet 50000 thrpt 15 1802.159 ± 25.323 ops/s 6 | MapGetJmhJdk8Tests.jdkIntegerGet 75000 thrpt 15 1079.526 ± 17.963 ops/s 7 | MapGetJmhJdk8Tests.jdkIntegerGet 100000 thrpt 15 1225.746 ± 138.559 ops/s 8 | MapGetJmhJdk8Tests.jdkStringGet 1000 thrpt 15 18024.746 ± 276.229 ops/s 9 | MapGetJmhJdk8Tests.jdkStringGet 10000 thrpt 15 1198.705 ± 184.656 ops/s 10 | MapGetJmhJdk8Tests.jdkStringGet 25000 thrpt 15 412.101 ± 31.929 ops/s 11 | MapGetJmhJdk8Tests.jdkStringGet 50000 thrpt 15 206.088 ± 21.199 ops/s 12 | MapGetJmhJdk8Tests.jdkStringGet 75000 thrpt 15 119.085 ± 5.081 ops/s 13 | MapGetJmhJdk8Tests.jdkStringGet 100000 thrpt 15 87.903 ± 0.869 ops/s 14 | MapPutJmhJdk8Tests.jdkIntegerPut 1000 thrpt 15 125791.085 ± 4908.417 ops/s 15 | MapPutJmhJdk8Tests.jdkIntegerPut 10000 thrpt 15 11435.205 ± 915.207 ops/s 16 | MapPutJmhJdk8Tests.jdkIntegerPut 25000 thrpt 15 4718.319 ± 248.869 ops/s 17 | MapPutJmhJdk8Tests.jdkIntegerPut 50000 thrpt 15 2343.386 ± 116.769 ops/s 18 | MapPutJmhJdk8Tests.jdkIntegerPut 75000 thrpt 15 1533.037 ± 80.343 ops/s 19 | MapPutJmhJdk8Tests.jdkIntegerPut 100000 thrpt 15 1097.441 ± 41.114 ops/s 20 | MapPutJmhJdk8Tests.jdkStringPut 1000 thrpt 15 29140.246 ± 452.815 ops/s 21 | MapPutJmhJdk8Tests.jdkStringPut 10000 thrpt 15 2528.088 ± 58.108 ops/s 22 | MapPutJmhJdk8Tests.jdkStringPut 25000 thrpt 15 926.220 ± 16.286 ops/s 23 | MapPutJmhJdk8Tests.jdkStringPut 50000 thrpt 15 279.804 ± 3.623 ops/s 24 | MapPutJmhJdk8Tests.jdkStringPut 75000 thrpt 15 154.850 ± 1.597 ops/s 25 | MapPutJmhJdk8Tests.jdkStringPut 100000 thrpt 15 119.884 ± 2.140 ops/s 26 | -------------------------------------------------------------------------------- /performance-measurements/jdk11-jmh-tests.txt: -------------------------------------------------------------------------------- 1 | Benchmark (size) Mode Cnt Score Error Units 2 | MapGetJmhJdk11Tests.jdkIntegerGet 1000 thrpt 15 188308.107 ± 6735.960 ops/s 3 | MapGetJmhJdk11Tests.jdkIntegerGet 10000 thrpt 15 17902.806 ± 261.094 ops/s 4 | MapGetJmhJdk11Tests.jdkIntegerGet 25000 thrpt 15 6922.843 ± 256.814 ops/s 5 | MapGetJmhJdk11Tests.jdkIntegerGet 50000 thrpt 15 1932.402 ± 47.397 ops/s 6 | MapGetJmhJdk11Tests.jdkIntegerGet 75000 thrpt 15 2292.846 ± 242.504 ops/s 7 | MapGetJmhJdk11Tests.jdkIntegerGet 100000 thrpt 15 1262.620 ± 14.389 ops/s 8 | MapGetJmhJdk11Tests.jdkStringGet 1000 thrpt 15 31894.172 ± 253.571 ops/s 9 | MapGetJmhJdk11Tests.jdkStringGet 10000 thrpt 15 2742.313 ± 126.542 ops/s 10 | MapGetJmhJdk11Tests.jdkStringGet 25000 thrpt 15 847.558 ± 44.335 ops/s 11 | MapGetJmhJdk11Tests.jdkStringGet 50000 thrpt 15 282.916 ± 9.674 ops/s 12 | MapGetJmhJdk11Tests.jdkStringGet 75000 thrpt 15 150.880 ± 1.412 ops/s 13 | MapGetJmhJdk11Tests.jdkStringGet 100000 thrpt 15 115.813 ± 1.574 ops/s 14 | MapPutJmhJdk11Tests.jdkIntegerPut 1000 thrpt 15 111523.324 ± 3163.340 ops/s 15 | MapPutJmhJdk11Tests.jdkIntegerPut 10000 thrpt 15 12177.524 ± 1937.508 ops/s 16 | MapPutJmhJdk11Tests.jdkIntegerPut 25000 thrpt 15 4254.201 ± 179.042 ops/s 17 | MapPutJmhJdk11Tests.jdkIntegerPut 50000 thrpt 15 2454.248 ± 257.209 ops/s 18 | MapPutJmhJdk11Tests.jdkIntegerPut 75000 thrpt 15 1672.234 ± 230.599 ops/s 19 | MapPutJmhJdk11Tests.jdkIntegerPut 100000 thrpt 15 1006.215 ± 32.109 ops/s 20 | MapPutJmhJdk11Tests.jdkStringPut 1000 thrpt 15 36068.594 ± 435.563 ops/s 21 | MapPutJmhJdk11Tests.jdkStringPut 10000 thrpt 15 3245.799 ± 103.400 ops/s 22 | MapPutJmhJdk11Tests.jdkStringPut 25000 thrpt 15 1030.331 ± 43.554 ops/s 23 | MapPutJmhJdk11Tests.jdkStringPut 50000 thrpt 15 324.278 ± 13.177 ops/s 24 | MapPutJmhJdk11Tests.jdkStringPut 75000 thrpt 15 134.659 ± 8.763 ops/s 25 | MapPutJmhJdk11Tests.jdkStringPut 100000 thrpt 15 99.624 ± 1.585 ops/s 26 | -------------------------------------------------------------------------------- /performance-measurements/jdk17-jmh-tests.txt: -------------------------------------------------------------------------------- 1 | Benchmark (size) Mode Cnt Score Error Units 2 | MapGetJmhJdk17Tests.jdkIntegerGet 1000 thrpt 15 234226.571 ± 22742.657 ops/s 3 | MapGetJmhJdk17Tests.jdkIntegerGet 10000 thrpt 15 21941.635 ± 343.162 ops/s 4 | MapGetJmhJdk17Tests.jdkIntegerGet 25000 thrpt 15 6759.260 ± 341.801 ops/s 5 | MapGetJmhJdk17Tests.jdkIntegerGet 50000 thrpt 15 1975.205 ± 45.142 ops/s 6 | MapGetJmhJdk17Tests.jdkIntegerGet 75000 thrpt 15 2969.342 ± 63.543 ops/s 7 | MapGetJmhJdk17Tests.jdkIntegerGet 100000 thrpt 15 1327.067 ± 64.412 ops/s 8 | MapGetJmhJdk17Tests.jdkStringGet 1000 thrpt 15 30521.881 ± 1461.748 ops/s 9 | MapGetJmhJdk17Tests.jdkStringGet 10000 thrpt 15 2409.984 ± 390.498 ops/s 10 | MapGetJmhJdk17Tests.jdkStringGet 25000 thrpt 15 760.014 ± 42.175 ops/s 11 | MapGetJmhJdk17Tests.jdkStringGet 50000 thrpt 15 322.417 ± 19.289 ops/s 12 | MapGetJmhJdk17Tests.jdkStringGet 75000 thrpt 15 174.604 ± 3.126 ops/s 13 | MapGetJmhJdk17Tests.jdkStringGet 100000 thrpt 15 129.445 ± 2.003 ops/s 14 | MapPutJmhJdk17Tests.jdkIntegerPut 1000 thrpt 15 218755.791 ± 84356.982 ops/s 15 | MapPutJmhJdk17Tests.jdkIntegerPut 10000 thrpt 15 14726.911 ± 7448.632 ops/s 16 | MapPutJmhJdk17Tests.jdkIntegerPut 25000 thrpt 15 6085.780 ± 2915.963 ops/s 17 | MapPutJmhJdk17Tests.jdkIntegerPut 50000 thrpt 15 2897.746 ± 1218.217 ops/s 18 | MapPutJmhJdk17Tests.jdkIntegerPut 75000 thrpt 15 1850.432 ± 885.742 ops/s 19 | MapPutJmhJdk17Tests.jdkIntegerPut 100000 thrpt 15 1378.678 ± 243.934 ops/s 20 | MapPutJmhJdk17Tests.jdkStringPut 1000 thrpt 15 32850.455 ± 2287.655 ops/s 21 | MapPutJmhJdk17Tests.jdkStringPut 10000 thrpt 15 3048.396 ± 248.991 ops/s 22 | MapPutJmhJdk17Tests.jdkStringPut 25000 thrpt 15 998.983 ± 18.346 ops/s 23 | MapPutJmhJdk17Tests.jdkStringPut 50000 thrpt 15 369.496 ± 17.690 ops/s 24 | MapPutJmhJdk17Tests.jdkStringPut 75000 thrpt 15 189.167 ± 1.940 ops/s 25 | MapPutJmhJdk17Tests.jdkStringPut 100000 thrpt 15 148.218 ± 4.660 ops/s 26 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | nikhil.nani 7 | java-upgrade 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | code-examples 12 | performance-tests-jdk17 13 | performance-tests-jdk11 14 | performance-tests-jdk8 15 | 16 | 17 | pom 18 | Should I Upgrade My Java 19 | 20 | 21 | 5.7.2 22 | 1.35 23 | 24 | UTF-8 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.junit.jupiter 32 | junit-jupiter 33 | ${junit5.version} 34 | test 35 | 36 | 37 | 38 | org.openjdk.jmh 39 | jmh-core 40 | ${jmh.version} 41 | 42 | 43 | 44 | org.openjdk.jmh 45 | jmh-generator-annprocess 46 | ${jmh.version} 47 | provided 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-surefire-plugin 57 | 3.0.0-M5 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesRecordTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Objects; 7 | 8 | public class ExamplesRecordTest { 9 | 10 | @Test 11 | public void recordTest() { 12 | PersonNonRecord pnr = new PersonNonRecord("Nikhil", "N"); 13 | PersonRecord pr = new PersonRecord("Nikhil", "N"); 14 | 15 | Assertions.assertEquals(pnr.getFirstName(), pr.firstName()); 16 | Assertions.assertEquals(pnr.getLastName(), pr.lastName()); 17 | Assertions.assertEquals(pnr.toString(), pr.toString()); 18 | } 19 | 20 | public record PersonRecord(String firstName, String lastName) { 21 | 22 | public String getFullName() { 23 | return firstName + ' ' + lastName; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "Person{" + 29 | "firstName='" + firstName + '\'' + 30 | ", lastName='" + lastName + '\'' + 31 | '}'; 32 | } 33 | } 34 | 35 | public final class PersonNonRecord { 36 | private final String firstName; 37 | private final String lastName; 38 | 39 | public PersonNonRecord(String firstName, String lastName) { 40 | this.firstName = firstName; 41 | this.lastName = lastName; 42 | } 43 | 44 | public String getFirstName() { 45 | return firstName; 46 | } 47 | 48 | public String getLastName() { 49 | return lastName; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) return true; 55 | if (o == null || getClass() != o.getClass()) return false; 56 | PersonNonRecord that = (PersonNonRecord) o; 57 | return Objects.equals(firstName, that.firstName) && Objects.equals(lastName, that.lastName); 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return Objects.hash(firstName, lastName); 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "Person{" + 68 | "firstName='" + firstName + '\'' + 69 | ", lastName='" + lastName + '\'' + 70 | '}'; 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /performance-measurements/MemoryTests.txt: -------------------------------------------------------------------------------- 1 | JDK8 String Size:1 Memory:1368 Kb 2 | JDK8 String Size:10 Memory:1370 Kb 3 | JDK8 String Size:100 Memory:1383 Kb 4 | JDK8 String Size:1000 Memory:1504 Kb 5 | JDK8 String Size:10000 Memory:2699 Kb 6 | JDK8 String Size:100000 Memory:16181 Kb 7 | JDK8 String Size:1000000 Memory:146601 Kb 8 | ----------------------------------------------------------- 9 | JDK8 String Size:1 Memory:2448 Kb 10 | JDK8 String Size:10 Memory:2449 Kb 11 | JDK8 String Size:100 Memory:2462 Kb 12 | JDK8 String Size:1000 Memory:2583 Kb 13 | JDK8 String Size:10000 Memory:3777 Kb 14 | JDK8 String Size:100000 Memory:17283 Kb 15 | JDK8 String Size:1000000 Memory:147080 Kb 16 | ----------------------------------------------------------- 17 | JDK8 String Size:1 Memory:2927 Kb 18 | JDK8 String Size:10 Memory:2928 Kb 19 | JDK8 String Size:100 Memory:2941 Kb 20 | JDK8 String Size:1000 Memory:3062 Kb 21 | JDK8 String Size:10000 Memory:4255 Kb 22 | JDK8 String Size:100000 Memory:17761 Kb 23 | JDK8 String Size:1000000 Memory:147610 Kb 24 | ----------------------------------------------------------- 25 | JDK8 String Size:1 Memory:3457 Kb 26 | JDK8 String Size:10 Memory:3458 Kb 27 | JDK8 String Size:100 Memory:3471 Kb 28 | JDK8 String Size:1000 Memory:3592 Kb 29 | JDK8 String Size:10000 Memory:4785 Kb 30 | JDK8 String Size:100000 Memory:18291 Kb 31 | JDK8 String Size:1000000 Memory:148213 Kb 32 | ----------------------------------------------------------- 33 | JDK8 String Size:1 Memory:4060 Kb 34 | JDK8 String Size:10 Memory:4061 Kb 35 | JDK8 String Size:100 Memory:4074 Kb 36 | JDK8 String Size:1000 Memory:4195 Kb 37 | JDK8 String Size:10000 Memory:5389 Kb 38 | JDK8 String Size:100000 Memory:18895 Kb 39 | JDK8 String Size:1000000 Memory:148199 Kb 40 | ----------------------------------------------------------- 41 | 42 | ----------------------------------------------------------------------------------------- 43 | 44 | JDK11 String Size:1 Memory:1216 Kb 45 | JDK11 String Size:10 Memory:1336 Kb 46 | JDK11 String Size:100 Memory:1349 Kb 47 | JDK11 String Size:1000 Memory:1470 Kb 48 | JDK11 String Size:10000 Memory:2664 Kb 49 | JDK11 String Size:100000 Memory:17914 Kb 50 | JDK11 String Size:1000000 Memory:139471 Kb 51 | ----------------------------------------------------------- 52 | JDK11 String Size:1 Memory:1336 Kb 53 | JDK11 String Size:10 Memory:1337 Kb 54 | JDK11 String Size:100 Memory:1350 Kb 55 | JDK11 String Size:1000 Memory:1471 Kb 56 | JDK11 String Size:10000 Memory:2665 Kb 57 | JDK11 String Size:100000 Memory:17914 Kb 58 | JDK11 String Size:1000000 Memory:139471 Kb 59 | ----------------------------------------------------------- 60 | JDK11 String Size:1 Memory:1336 Kb 61 | JDK11 String Size:10 Memory:1337 Kb 62 | JDK11 String Size:100 Memory:1350 Kb 63 | JDK11 String Size:1000 Memory:1471 Kb 64 | JDK11 String Size:10000 Memory:2665 Kb 65 | JDK11 String Size:100000 Memory:17914 Kb 66 | JDK11 String Size:1000000 Memory:139471 Kb 67 | ----------------------------------------------------------- 68 | JDK11 String Size:1 Memory:1336 Kb 69 | JDK11 String Size:10 Memory:1337 Kb 70 | JDK11 String Size:100 Memory:1350 Kb 71 | JDK11 String Size:1000 Memory:1471 Kb 72 | JDK11 String Size:10000 Memory:2665 Kb 73 | JDK11 String Size:100000 Memory:17914 Kb 74 | JDK11 String Size:1000000 Memory:139471 Kb 75 | ----------------------------------------------------------- 76 | JDK11 String Size:1 Memory:1336 Kb 77 | JDK11 String Size:10 Memory:1337 Kb 78 | JDK11 String Size:100 Memory:1350 Kb 79 | JDK11 String Size:1000 Memory:1471 Kb 80 | JDK11 String Size:10000 Memory:2665 Kb 81 | JDK11 String Size:100000 Memory:17914 Kb 82 | JDK11 String Size:1000000 Memory:139471 Kb 83 | ----------------------------------------------------------- 84 | 85 | ----------------------------------------------------------------------------------------- 86 | 87 | JDK17 String Size:1 Memory:1210 Kb 88 | JDK17 String Size:10 Memory:1263 Kb 89 | JDK17 String Size:100 Memory:1276 Kb 90 | JDK17 String Size:1000 Memory:1397 Kb 91 | JDK17 String Size:10000 Memory:2592 Kb 92 | JDK17 String Size:100000 Memory:17856 Kb 93 | JDK17 String Size:1000000 Memory:139472 Kb 94 | ----------------------------------------------------------- 95 | JDK17 String Size:1 Memory:1264 Kb 96 | JDK17 String Size:10 Memory:1265 Kb 97 | JDK17 String Size:100 Memory:1278 Kb 98 | JDK17 String Size:1000 Memory:1399 Kb 99 | JDK17 String Size:10000 Memory:2592 Kb 100 | JDK17 String Size:100000 Memory:17856 Kb 101 | JDK17 String Size:1000000 Memory:139583 Kb 102 | ----------------------------------------------------------- 103 | JDK17 String Size:1 Memory:1264 Kb 104 | JDK17 String Size:10 Memory:1265 Kb 105 | JDK17 String Size:100 Memory:1278 Kb 106 | JDK17 String Size:1000 Memory:1399 Kb 107 | JDK17 String Size:10000 Memory:2592 Kb 108 | JDK17 String Size:100000 Memory:17856 Kb 109 | JDK17 String Size:1000000 Memory:139546 Kb 110 | ----------------------------------------------------------- 111 | JDK17 String Size:1 Memory:1264 Kb 112 | JDK17 String Size:10 Memory:1265 Kb 113 | JDK17 String Size:100 Memory:1278 Kb 114 | JDK17 String Size:1000 Memory:1399 Kb 115 | JDK17 String Size:10000 Memory:2592 Kb 116 | JDK17 String Size:100000 Memory:17856 Kb 117 | JDK17 String Size:1000000 Memory:139564 Kb 118 | ----------------------------------------------------------- 119 | JDK17 String Size:1 Memory:1264 Kb 120 | JDK17 String Size:10 Memory:1265 Kb 121 | JDK17 String Size:100 Memory:1278 Kb 122 | JDK17 String Size:1000 Memory:1399 Kb 123 | JDK17 String Size:10000 Memory:2592 Kb 124 | JDK17 String Size:100000 Memory:17859 Kb 125 | JDK17 String Size:1000000 Memory:139550 Kb 126 | ----------------------------------------------------------- 127 | 128 | -------------------------------------------------------------------------------- /code-examples/src/test/java/nikhil/nani/code/examples/ExamplesSwitchTest.java: -------------------------------------------------------------------------------- 1 | package nikhil.nani.code.examples; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class ExamplesSwitchTest { 7 | 8 | @Test 9 | public void switchStatements() { 10 | Assertions.assertEquals("Q1", this.getQuarterOldSwitch(1)); 11 | Assertions.assertEquals("Q1", this.getQuarterOldSwitch(2)); 12 | Assertions.assertEquals("Q1", this.getQuarterOldSwitch(3)); 13 | Assertions.assertEquals("Q2", this.getQuarterOldSwitch(4)); 14 | Assertions.assertEquals("Q2", this.getQuarterOldSwitch(5)); 15 | Assertions.assertEquals("Q2", this.getQuarterOldSwitch(6)); 16 | Assertions.assertEquals("Q3", this.getQuarterOldSwitch(7)); 17 | Assertions.assertEquals("Q3", this.getQuarterOldSwitch(8)); 18 | Assertions.assertEquals("Q3", this.getQuarterOldSwitch(9)); 19 | Assertions.assertEquals("Q4", this.getQuarterOldSwitch(10)); 20 | Assertions.assertEquals("Q4", this.getQuarterOldSwitch(11)); 21 | Assertions.assertEquals("Q4", this.getQuarterOldSwitch(12)); 22 | Assertions.assertEquals("Not a month in year", this.getQuarterOldSwitch(13)); 23 | 24 | Assertions.assertEquals(this.getQuarterOldSwitch(1), this.getQuarterNewSwitch(1)); 25 | Assertions.assertEquals(this.getQuarterOldSwitch(2), this.getQuarterNewSwitch(2)); 26 | Assertions.assertEquals(this.getQuarterOldSwitch(3), this.getQuarterNewSwitch(3)); 27 | Assertions.assertEquals(this.getQuarterOldSwitch(4), this.getQuarterNewSwitch(4)); 28 | Assertions.assertEquals(this.getQuarterOldSwitch(5), this.getQuarterNewSwitch(5)); 29 | Assertions.assertEquals(this.getQuarterOldSwitch(6), this.getQuarterNewSwitch(6)); 30 | Assertions.assertEquals(this.getQuarterOldSwitch(7), this.getQuarterNewSwitch(7)); 31 | Assertions.assertEquals(this.getQuarterOldSwitch(8), this.getQuarterNewSwitch(8)); 32 | Assertions.assertEquals(this.getQuarterOldSwitch(9), this.getQuarterNewSwitch(9)); 33 | Assertions.assertEquals(this.getQuarterOldSwitch(10), this.getQuarterNewSwitch(10)); 34 | Assertions.assertEquals(this.getQuarterOldSwitch(11), this.getQuarterNewSwitch(11)); 35 | Assertions.assertEquals(this.getQuarterOldSwitch(12), this.getQuarterNewSwitch(12)); 36 | Assertions.assertEquals(this.getQuarterOldSwitch(13), this.getQuarterNewSwitch(13)); 37 | 38 | Assertions.assertEquals(this.getQuarterNewSwitch(1), this.getQuarterNewSwitchAsExpression(1)); 39 | Assertions.assertEquals(this.getQuarterNewSwitch(2), this.getQuarterNewSwitchAsExpression(2)); 40 | Assertions.assertEquals(this.getQuarterNewSwitch(3), this.getQuarterNewSwitchAsExpression(3)); 41 | Assertions.assertEquals(this.getQuarterNewSwitch(4), this.getQuarterNewSwitchAsExpression(4)); 42 | Assertions.assertEquals(this.getQuarterNewSwitch(5), this.getQuarterNewSwitchAsExpression(5)); 43 | Assertions.assertEquals(this.getQuarterNewSwitch(6), this.getQuarterNewSwitchAsExpression(6)); 44 | Assertions.assertEquals(this.getQuarterNewSwitch(7), this.getQuarterNewSwitchAsExpression(7)); 45 | Assertions.assertEquals(this.getQuarterNewSwitch(8), this.getQuarterNewSwitchAsExpression(8)); 46 | Assertions.assertEquals(this.getQuarterNewSwitch(9), this.getQuarterNewSwitchAsExpression(9)); 47 | Assertions.assertEquals(this.getQuarterNewSwitch(10), this.getQuarterNewSwitchAsExpression(10)); 48 | Assertions.assertEquals(this.getQuarterNewSwitch(11), this.getQuarterNewSwitchAsExpression(11)); 49 | Assertions.assertEquals(this.getQuarterNewSwitch(12), this.getQuarterNewSwitchAsExpression(12)); 50 | Assertions.assertEquals(this.getQuarterNewSwitch(13), this.getQuarterNewSwitchAsExpression(13)); 51 | 52 | Assertions.assertEquals(this.getQuarterNewSwitch(1), this.getQuarterYieldSyntax(1)); 53 | Assertions.assertEquals(this.getQuarterNewSwitch(2), this.getQuarterYieldSyntax(2)); 54 | Assertions.assertEquals(this.getQuarterNewSwitch(3), this.getQuarterYieldSyntax(3)); 55 | Assertions.assertEquals(this.getQuarterNewSwitch(4), this.getQuarterYieldSyntax(4)); 56 | Assertions.assertEquals(this.getQuarterNewSwitch(5), this.getQuarterYieldSyntax(5)); 57 | Assertions.assertEquals(this.getQuarterNewSwitch(6), this.getQuarterYieldSyntax(6)); 58 | Assertions.assertEquals(this.getQuarterNewSwitch(7), this.getQuarterYieldSyntax(7)); 59 | Assertions.assertEquals(this.getQuarterNewSwitch(8), this.getQuarterYieldSyntax(8)); 60 | Assertions.assertEquals(this.getQuarterNewSwitch(9), this.getQuarterYieldSyntax(9)); 61 | Assertions.assertEquals(this.getQuarterNewSwitch(10), this.getQuarterYieldSyntax(10)); 62 | Assertions.assertEquals(this.getQuarterNewSwitch(11), this.getQuarterYieldSyntax(11)); 63 | Assertions.assertEquals(this.getQuarterNewSwitch(12), this.getQuarterYieldSyntax(12)); 64 | Assertions.assertEquals(this.getQuarterNewSwitch(13), this.getQuarterYieldSyntax(13)); 65 | 66 | Assertions.assertEquals(this.getQuarterNewSwitch(1), this.getQuarterEnum(Months.JAN)); 67 | Assertions.assertEquals(this.getQuarterNewSwitch(2), this.getQuarterEnum(Months.FEB)); 68 | Assertions.assertEquals(this.getQuarterNewSwitch(3), this.getQuarterEnum(Months.MAR)); 69 | Assertions.assertEquals(this.getQuarterNewSwitch(4), this.getQuarterEnum(Months.APR)); 70 | Assertions.assertEquals(this.getQuarterNewSwitch(5), this.getQuarterEnum(Months.MAY)); 71 | Assertions.assertEquals(this.getQuarterNewSwitch(6), this.getQuarterEnum(Months.JUN)); 72 | Assertions.assertEquals(this.getQuarterNewSwitch(7), this.getQuarterEnum(Months.JUL)); 73 | Assertions.assertEquals(this.getQuarterNewSwitch(8), this.getQuarterEnum(Months.AUG)); 74 | Assertions.assertEquals(this.getQuarterNewSwitch(9), this.getQuarterEnum(Months.SEP)); 75 | Assertions.assertEquals(this.getQuarterNewSwitch(10), this.getQuarterEnum(Months.OCT)); 76 | Assertions.assertEquals(this.getQuarterNewSwitch(11), this.getQuarterEnum(Months.NOV)); 77 | Assertions.assertEquals(this.getQuarterNewSwitch(12), this.getQuarterEnum(Months.DEC)); 78 | } 79 | 80 | private String getQuarterOldSwitch(Integer monthNumber) { 81 | 82 | switch (monthNumber) { 83 | case 1: 84 | return "Q1"; 85 | case 2: 86 | return "Q1"; 87 | case 3: 88 | return "Q1"; 89 | case 4: 90 | return "Q2"; 91 | case 5: 92 | return "Q2"; 93 | case 6: 94 | return "Q2"; 95 | case 7: 96 | return "Q3"; 97 | case 8: 98 | return "Q3"; 99 | case 9: 100 | return "Q3"; 101 | case 10: 102 | return "Q4"; 103 | case 11: 104 | return "Q4"; 105 | case 12: 106 | return "Q4"; 107 | default: 108 | return "Not a month in year"; 109 | } 110 | } 111 | 112 | private String getQuarterNewSwitch(Integer monthNumber) { 113 | 114 | return switch (monthNumber) { 115 | case 1, 2, 3 -> "Q1"; 116 | case 4, 5, 6 -> "Q2"; 117 | case 7, 8, 9 -> "Q3"; 118 | case 10, 11, 12 -> "Q4"; 119 | default -> "Not a month in year"; 120 | }; 121 | } 122 | 123 | private String getQuarterNewSwitchAsExpression(Integer monthNumber) { 124 | final String quarter = switch (monthNumber) { 125 | case 1, 2, 3 -> "Q1"; 126 | case 4, 5, 6 -> "Q2"; 127 | case 7, 8, 9 -> "Q3"; 128 | case 10, 11, 12 -> "Q4"; 129 | default -> "Not a month in year"; 130 | }; 131 | return quarter; 132 | } 133 | 134 | private String getQuarterYieldSyntax(Integer monthNumber) { 135 | 136 | return switch (monthNumber) { 137 | case 1, 2, 3 -> "Q1"; 138 | case 4, 5, 6 -> "Q2"; 139 | case 7, 8, 9 -> "Q3"; 140 | case 10, 11, 12 -> { 141 | System.out.println("Yay Last Quarter"); 142 | yield "Q4"; 143 | } 144 | default -> "Not a month in year"; 145 | }; 146 | } 147 | 148 | public enum Months { 149 | JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC 150 | } 151 | 152 | private String getQuarterEnum(Months month) { 153 | 154 | return switch (month) { 155 | case JAN, FEB, MAR -> "Q1"; 156 | case APR, MAY, JUN -> "Q2"; 157 | case JUL, AUG, SEP -> "Q3"; 158 | case OCT, NOV, DEC -> "Q4"; 159 | }; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------