├── 100 ├── src_001_Installing_Script_Engines_in_Maven.xml ├── src_002_Executing_Your_First_Script.java ├── src_003_Executing_Your_First_Script.java ├── src_004_Executing_Your_First_Script.java ├── src_005_Executing_Your_First_Script.java ├── src_006_Executing_Your_First_Script.java ├── src_007_Executing_Your_First_Script.java ├── src_008_Passing_Parameters.java ├── src_009_Passing_Parameters.java ├── src_010_Passing_Parameters.java ├── src_011_Passing_Parameters.java ├── src_012_Passing_Parameters.java ├── src_013_Passing_Parameters.java ├── src_014_Advanced_Parameter_Passing_Techniques.java ├── src_015_Advanced_Parameter_Passing_Techniques.java ├── src_016_Advanced_Parameter_Passing_Techniques.java ├── src_017_Advanced_Parameter_Passing_Techniques.java ├── src_018_Advanced_Parameter_Passing_Techniques.java ├── src_019_Advanced_Parameter_Passing_Techniques.java ├── src_020_Advanced_Parameter_Passing_Techniques.java ├── src_021_Advanced_Parameter_Passing_Techniques.java ├── src_022_Advanced_Parameter_Passing_Techniques.java ├── src_023_Advanced_Parameter_Passing_Techniques.java ├── src_024_Advanced_Parameter_Passing_Techniques.java ├── src_025_Advanced_Parameter_Passing_Techniques.java ├── src_026_Advanced_Parameter_Passing_Techniques.java ├── src_027_Advanced_Parameter_Passing_Techniques.java ├── src_028_Advanced_Parameter_Passing_Techniques.java ├── src_029_Advanced_Parameter_Passing_Techniques.java ├── src_030_Advanced_Parameter_Passing_Techniques.java ├── src_031_Advanced_Parameter_Passing_Techniques.java ├── src_032_Advanced_Parameter_Passing_Techniques.java ├── src_033_Return_Value_of_the_eval___Method.java ├── src_034_Changing_the_Default_ScriptContext.java ├── src_035_Changing_the_Default_ScriptContext.java ├── src_036_Sending_Script_Output_to_a_File.java ├── src_037_Invoking_Procedures_in_Scripts.java ├── src_038_Invoking_Procedures_in_Scripts.java ├── src_039_Invoking_Procedures_in_Scripts.java ├── src_040_Invoking_Procedures_in_Scripts.java ├── src_041_Invoking_Procedures_in_Scripts.java ├── src_042_Implementing_Java_Interfaces_in_Scripts.java ├── src_043_Implementing_Java_Interfaces_in_Scripts.java ├── src_044_Implementing_Java_Interfaces_in_Scripts.java ├── src_045_Using_Compiled_Scripts.java ├── src_046_Using_Compiled_Scripts.java ├── src_047_Implementing_a_Script_Engine.java ├── src_048_Implementing_a_Script_Engine.java ├── src_049_Implementing_a_Script_Engine.java ├── src_050_Implementing_a_Script_Engine.java ├── src_051_JavaFX_in_Groovy.xml ├── src_052_JavaFX_in_Groovy.java └── src_053_JavaFX_in_Groovy.java ├── 110 ├── src_001_Knowing_the_Runtime_Environment.java ├── src_002_Knowing_the_Runtime_Environment.java ├── src_003_The_Current_Process.java ├── src_004_Creating_a_Process.sh ├── src_005_Creating_a_Process.java ├── src_006_Creating_a_Process.java ├── src_007_Creating_a_Process.java ├── src_008_Creating_a_Process.java ├── src_009_Creating_a_Process.java ├── src_010_Creating_a_Process.java ├── src_011_Creating_a_Process.java ├── src_012_Creating_a_Process.java ├── src_013_Creating_a_Process.java ├── src_014_Creating_a_Process.java ├── src_015_Creating_a_Process.java ├── src_016_Creating_a_Process.sh ├── src_017_Creating_a_Process.sh ├── src_018_Creating_a_Process.java ├── src_019_Creating_a_Process.java ├── src_020_Creating_a_Process.java ├── src_021_Creating_a_Process.java ├── src_022_Obtaining_a_Process_Handle.java ├── src_023_Obtaining_a_Process_Handle.java ├── src_024_Managing_Process_Permissions.sh └── src_025_Managing_Process_Permissions.java ├── 120 ├── src_001_The_JAR_Format.misc ├── src_002_The_JAR_Format.misc ├── src_003_The_JAR_Format.java ├── src_004_The_JAR_Format.sh ├── src_005_The_JAR_Format.misc ├── src_006_The_JAR_Format.misc ├── src_007_The_JAR_Format.misc ├── src_008_The_JAR_Format.sh ├── src_009_The_JAR_Format.sh ├── src_010_The_JAR_Format.sh ├── src_011_The_JAR_Format.sh ├── src_012_The_JAR_Format.sh ├── src_013_The_JAR_Format.sh ├── src_014_The_JAR_Format.sh ├── src_015_The_JAR_Format.sh ├── src_016_The_JAR_Format.misc ├── src_017_The_JAR_Format.misc ├── src_018_The_JAR_Format.misc ├── src_019_The_JAR_Format.misc ├── src_020_The_JMOD_Format.sh ├── src_021_The_JMOD_Format.sh ├── src_022_The_JMOD_Format.sh ├── src_023_The_JMOD_Format.sh ├── src_024_The_JMOD_Format.misc ├── src_025_The_JMOD_Format.sh ├── src_026_The_JMOD_Format.sh ├── src_027_The_JMOD_Format.sh ├── src_028_The_JMOD_Format.sh ├── src_029_The_JMOD_Format.sh ├── src_030_The_JMOD_Format.sh ├── src_031_The_JMOD_Format.sh ├── src_032_The_JMOD_Format.sh ├── src_033_The_JMOD_Format.sh ├── src_034_The_JMOD_Format.sh ├── src_035_The_JMOD_Format.sh └── src_036_The_JMOD_Format.sh ├── 130 ├── src_001_Creating_Custom_Runtime_Images.java ├── src_002_Creating_Custom_Runtime_Images.java ├── src_003_Creating_Custom_Runtime_Images.java ├── src_004_Creating_Custom_Runtime_Images.misc ├── src_005_Creating_Custom_Runtime_Images.java ├── src_006_Creating_Custom_Runtime_Images.java ├── src_007_Binding_Services.java ├── src_008_Binding_Services.java ├── src_009_Binding_Services.java ├── src_010_Binding_Services.java ├── src_011_Binding_Services.java ├── src_012_Using_Plugins_with_the_jlink_Tool.java ├── src_013_Using_Plugins_with_the_jlink_Tool.java ├── src_014_The_jimage_Tool.java ├── src_015_The_jimage_Tool.java ├── src_016_The_jimage_Tool.java ├── src_017_The_jimage_Tool.java ├── src_018_The_jimage_Tool.java └── src_019_The_jimage_Tool.java ├── 140 ├── src_001_More_JDK_17_Novelties.java ├── src_002_More_JDK_17_Novelties.java ├── src_003_More_JDK_17_Novelties.java ├── src_004_More_JDK_17_Novelties.java ├── src_005_More_JDK_17_Novelties.java ├── src_006_More_JDK_17_Novelties.java ├── src_007_More_JDK_17_Novelties.java ├── src_008_More_JDK_17_Novelties.java ├── src_009_More_JDK_17_Novelties.java ├── src_010_More_JDK_17_Novelties.misc ├── src_011_More_JDK_17_Novelties.java ├── src_012_More_JDK_17_Novelties.java ├── src_013_More_JDK_17_Novelties.java ├── src_014_More_JDK_17_Novelties.java ├── src_015_More_JDK_17_Novelties.java ├── src_016_More_JDK_17_Novelties.java ├── src_017_More_JDK_17_Novelties.java ├── src_018_More_JDK_17_Novelties.java ├── src_019_More_JDK_17_Novelties.java ├── src_020_More_JDK_17_Novelties.java └── src_021_More_JDK_17_Novelties.java ├── 990 └── src_001_Exercises_in_Chapter_1.java ├── .gitattributes ├── 010 ├── src_001_What_Are_Annotations_.java ├── src_002_What_Are_Annotations_.java ├── src_003_What_Are_Annotations_.java ├── src_004_What_Are_Annotations_.java ├── src_005_What_Are_Annotations_.misc ├── src_006_What_Are_Annotations_.java ├── src_007_What_Are_Annotations_.java ├── src_008_Declaring_an_Annotation_Type.java ├── src_009_Declaring_an_Annotation_Type.java ├── src_010_Declaring_an_Annotation_Type.java ├── src_011_Declaring_an_Annotation_Type.java ├── src_012_Declaring_an_Annotation_Type.java ├── src_013_Declaring_an_Annotation_Type.java ├── src_014_Restrictions_on_Annotation_Types.java ├── src_015_Restrictions_on_Annotation_Types.java ├── src_016_Restrictions_on_Annotation_Types.java ├── src_017_Restrictions_on_Annotation_Types.java ├── src_018_Restrictions_on_Annotation_Types.java ├── src_019_Default_Value_of_an_Annotation_Element.java ├── src_020_Default_Value_of_an_Annotation_Element.java ├── src_021_Default_Value_of_an_Annotation_Element.java ├── src_022_Default_Value_of_an_Annotation_Element.java ├── src_023_Annotation_Type_and_Its_Instances.java ├── src_024_Using_Annotations.java ├── src_025_Using_Annotations.java ├── src_026_Using_Annotations.java ├── src_027_Using_Annotations.java ├── src_028_Using_Annotations.java ├── src_029_Using_Annotations.java ├── src_030_Using_Annotations.java ├── src_031_Using_Annotations.java ├── src_032_Using_Annotations.java ├── src_033_Using_Annotations.java ├── src_034_Using_Annotations.java ├── src_035_Using_Annotations.java ├── src_036_No_Null_Value_in_an_Annotation.java ├── src_037_Shorthand_Annotation_Syntax.java ├── src_038_Shorthand_Annotation_Syntax.java ├── src_039_Shorthand_Annotation_Syntax.java ├── src_040_Shorthand_Annotation_Syntax.java ├── src_041_Shorthand_Annotation_Syntax.java ├── src_042_Shorthand_Annotation_Syntax.java ├── src_043_Shorthand_Annotation_Syntax.java ├── src_044_Shorthand_Annotation_Syntax.java ├── src_045_Marker_Annotation_Types.java ├── src_046_Marker_Annotation_Types.java ├── src_047_Meta_Annotation_Types.java ├── src_048_Meta_Annotation_Types.java ├── src_049_Meta_Annotation_Types.java ├── src_050_Meta_Annotation_Types.java ├── src_051_Meta_Annotation_Types.java ├── src_052_Meta_Annotation_Types.java ├── src_053_Meta_Annotation_Types.java ├── src_054_Meta_Annotation_Types.java ├── src_055_Meta_Annotation_Types.java ├── src_056_Commonly_Used_Standard_Annotations.misc ├── src_057_Commonly_Used_Standard_Annotations.sh ├── src_058_Commonly_Used_Standard_Annotations.sh ├── src_059_Commonly_Used_Standard_Annotations.sh ├── src_060_Commonly_Used_Standard_Annotations.sh ├── src_061_Commonly_Used_Standard_Annotations.sh ├── src_062_Commonly_Used_Standard_Annotations.sh ├── src_063_Commonly_Used_Standard_Annotations.sh ├── src_064_Commonly_Used_Standard_Annotations.java ├── src_065_Commonly_Used_Standard_Annotations.java ├── src_066_Commonly_Used_Standard_Annotations.java ├── src_067_Commonly_Used_Standard_Annotations.java ├── src_068_Commonly_Used_Standard_Annotations.java ├── src_069_Annotating_Packages.java ├── src_070_Annotating_Modules.java ├── src_071_Accessing_Annotations_at_Runtime.java ├── src_072_Accessing_Annotations_at_Runtime.java ├── src_073_Annotation_Processing_at_Source_Code_Level.sh ├── src_074_Annotation_Processing_at_Source_Code_Level.sh ├── src_075_Annotation_Processing_at_Source_Code_Level.java ├── src_076_Annotation_Processing_at_Source_Code_Level.java ├── src_077_Annotation_Processing_at_Source_Code_Level.java ├── src_078_Annotation_Processing_at_Source_Code_Level.java ├── src_079_Annotation_Processing_at_Source_Code_Level.java ├── src_080_Annotation_Processing_at_Source_Code_Level.java ├── src_081_Annotation_Processing_at_Source_Code_Level.java ├── src_082_Annotation_Processing_at_Source_Code_Level.java ├── src_083_Annotation_Processing_at_Source_Code_Level.java ├── src_084_Annotation_Processing_at_Source_Code_Level.java ├── src_085_Annotation_Processing_at_Source_Code_Level.sh ├── src_086_Exercises.java ├── src_087_Exercises.java ├── src_088_Exercises.java ├── src_089_Exercises.java ├── src_090_Exercises.java └── src_091_Exercises.java ├── 020 ├── src_001_Loading_a_Class.java ├── src_002_Loading_a_Class.java ├── src_003_Loading_a_Class.java ├── src_004_Loading_a_Class.java ├── src_005_Class_Loaders.java ├── src_006_Reflecting_on_Classes.java ├── src_007_Reflecting_on_Classes.java ├── src_008_Reflecting_on_Classes.java ├── src_009_Reflecting_on_Classes.java ├── src_010_Reflecting_on_Classes.java ├── src_011_Reflecting_on_Fields.java ├── src_012_Creating_Objects.java ├── src_013_Creating_Objects.java ├── src_014_Invoking_Methods.java ├── src_015_Invoking_Methods.java ├── src_016_Deep_Reflection.java ├── src_017_Deep_Reflection.java ├── src_018_Deep_Reflection.sh ├── src_019_Deep_Reflection.sh ├── src_020_Deep_Reflection.sh ├── src_021_Deep_Reflection.sh ├── src_022_Deep_Reflection.sh ├── src_023_Deep_Reflection.java ├── src_024_Deep_Reflection.misc ├── src_025_Deep_Reflection.java ├── src_026_Deep_Reflection.java ├── src_027_Deep_Reflection.java ├── src_028_Deep_Reflection.java ├── src_029_Deep_Reflection.java ├── src_030_Deep_Reflection.java ├── src_031_Deep_Reflection.java ├── src_032_Deep_Reflection.sh ├── src_033_Deep_Reflection.java ├── src_034_Deep_Reflection.sh ├── src_035_Deep_Reflection.sh ├── src_036_Reflecting_on_Arrays.java ├── src_037_Reflecting_on_Arrays.java ├── src_038_Reflecting_on_Arrays.java └── src_039_Exercises.java ├── 030 ├── src_001_What_Are_Generics_.java ├── src_002_What_Are_Generics_.java ├── src_003_What_Are_Generics_.java ├── src_004_What_Are_Generics_.java ├── src_005_What_Are_Generics_.java ├── src_006_What_Are_Generics_.java ├── src_007_What_Are_Generics_.java ├── src_008_What_Are_Generics_.java ├── src_009_What_Are_Generics_.misc ├── src_010_What_Are_Generics_.java ├── src_011_What_Are_Generics_.java ├── src_012_Supertype_Subtype_Relationship.java ├── src_013_Supertype_Subtype_Relationship.java ├── src_014_Supertype_Subtype_Relationship.java ├── src_015_Raw_Types.java ├── src_016_Raw_Types.misc ├── src_017_Unbounded_Wildcards.java ├── src_018_Unbounded_Wildcards.java ├── src_019_Unbounded_Wildcards.misc ├── src_020_Unbounded_Wildcards.java ├── src_021_Unbounded_Wildcards.java ├── src_022_Unbounded_Wildcards.java ├── src_023_Unbounded_Wildcards.java ├── src_024_Unbounded_Wildcards.java ├── src_025_Unbounded_Wildcards.java ├── src_026_Unbounded_Wildcards.java ├── src_027_Unbounded_Wildcards.java ├── src_028_Upper_Bounded_Wildcards.java ├── src_029_Upper_Bounded_Wildcards.java ├── src_030_Upper_Bounded_Wildcards.java ├── src_031_Upper_Bounded_Wildcards.java ├── src_032_Upper_Bounded_Wildcards.java ├── src_033_Upper_Bounded_Wildcards.java ├── src_034_Lower_Bounded_Wildcards.java ├── src_035_Lower_Bounded_Wildcards.java ├── src_036_Lower_Bounded_Wildcards.java ├── src_037_Lower_Bounded_Wildcards.java ├── src_038_Generic_Methods_and_Constructors.java ├── src_039_Generic_Methods_and_Constructors.java ├── src_040_Generic_Methods_and_Constructors.java ├── src_041_Generic_Methods_and_Constructors.java ├── src_042_Generic_Methods_and_Constructors.java ├── src_043_Generic_Methods_and_Constructors.java ├── src_044_Type_Inference_in_Generic_Object_Creation.java ├── src_045_Type_Inference_in_Generic_Object_Creation.java ├── src_046_Type_Inference_in_Generic_Object_Creation.java ├── src_047_Type_Inference_in_Generic_Object_Creation.java ├── src_048_Type_Inference_in_Generic_Object_Creation.java ├── src_049_Type_Inference_in_Generic_Object_Creation.java ├── src_050_Type_Inference_in_Generic_Object_Creation.java ├── src_051_Type_Inference_in_Generic_Object_Creation.java ├── src_052_Type_Inference_in_Generic_Object_Creation.java ├── src_053_Type_Inference_in_Generic_Object_Creation.java ├── src_054_Type_Inference_in_Generic_Object_Creation.java ├── src_055_Type_Inference_in_Generic_Object_Creation.java ├── src_056_Type_Inference_in_Generic_Object_Creation.java ├── src_057_Generics_and_Arrays.java ├── src_058_Generics_and_Arrays.java ├── src_059_Generics_and_Arrays.java ├── src_060_Generics_and_Arrays.java ├── src_061_Generics_and_Arrays.java ├── src_062_Heap_Pollution.java ├── src_063_Heap_Pollution.java ├── src_064_Varargs_Methods_and_Heap_Pollution_Warnings.java ├── src_065_Varargs_Methods_and_Heap_Pollution_Warnings.misc ├── src_066_Varargs_Methods_and_Heap_Pollution_Warnings.java ├── src_067_Varargs_Methods_and_Heap_Pollution_Warnings.misc ├── src_068_Varargs_Methods_and_Heap_Pollution_Warnings.java ├── src_069_Varargs_Methods_and_Heap_Pollution_Warnings.misc ├── src_070_Varargs_Methods_and_Heap_Pollution_Warnings.java ├── src_071_Exercises.java ├── src_072_Exercises.java ├── src_073_Exercises.java ├── src_074_Exercises.java ├── src_075_Exercises.misc ├── src_076_Exercises.java └── src_077_Exercises.java ├── 040 ├── src_001_What_Is_a_Lambda_Expression_.java ├── src_002_What_Is_a_Lambda_Expression_.java ├── src_003_What_Is_a_Lambda_Expression_.java ├── src_004_What_Is_a_Lambda_Expression_.java ├── src_005_What_Is_a_Lambda_Expression_.java ├── src_006_What_Is_a_Lambda_Expression_.java ├── src_007_Why_Do_We_Need_Lambda_Expressions_.java ├── src_008_Syntax_for_Lambda_Expressions.java ├── src_009_Syntax_for_Lambda_Expressions.java ├── src_010_Syntax_for_Lambda_Expressions.java ├── src_011_Syntax_for_Lambda_Expressions.java ├── src_012_Syntax_for_Lambda_Expressions.java ├── src_013_Syntax_for_Lambda_Expressions.java ├── src_014_Syntax_for_Lambda_Expressions.java ├── src_015_Syntax_for_Lambda_Expressions.java ├── src_016_Syntax_for_Lambda_Expressions.java ├── src_017_Syntax_for_Lambda_Expressions.java ├── src_018_Syntax_for_Lambda_Expressions.java ├── src_019_Syntax_for_Lambda_Expressions.java ├── src_020_Syntax_for_Lambda_Expressions.java ├── src_021_Syntax_for_Lambda_Expressions.java ├── src_022_Syntax_for_Lambda_Expressions.java ├── src_023_Syntax_for_Lambda_Expressions.java ├── src_024_Syntax_for_Lambda_Expressions.java ├── src_025_Syntax_for_Lambda_Expressions.java ├── src_026_Syntax_for_Lambda_Expressions.java ├── src_027_Syntax_for_Lambda_Expressions.java ├── src_028_Syntax_for_Lambda_Expressions.java ├── src_029_Syntax_for_Lambda_Expressions.java ├── src_030_Syntax_for_Lambda_Expressions.java ├── src_031_Syntax_for_Lambda_Expressions.java ├── src_032_Target_Typing.java ├── src_033_Target_Typing.java ├── src_034_Target_Typing.java ├── src_035_Target_Typing.java ├── src_036_Target_Typing.java ├── src_037_Target_Typing.java ├── src_038_Target_Typing.java ├── src_039_Target_Typing.java ├── src_040_Target_Typing.java ├── src_041_Target_Typing.java ├── src_042_Target_Typing.misc ├── src_043_Target_Typing.java ├── src_044_Target_Typing.java ├── src_045_Target_Typing.java ├── src_046_Target_Typing.java ├── src_047_Target_Typing.java ├── src_048_Target_Typing.java ├── src_049_Target_Typing.java ├── src_050_Functional_Interfaces.java ├── src_051_Functional_Interfaces.java ├── src_052_Functional_Interfaces.java ├── src_053_Functional_Interfaces.java ├── src_054_Functional_Interfaces.java ├── src_055_Functional_Interfaces.java ├── src_056_Functional_Interfaces.java ├── src_057_Functional_Interfaces.java ├── src_058_Functional_Interfaces.java ├── src_059_Functional_Interfaces.java ├── src_060_Functional_Interfaces.java ├── src_061_Functional_Interfaces.java ├── src_062_Functional_Interfaces.java ├── src_063_Method_References.java ├── src_064_Method_References.java ├── src_065_Method_References.java ├── src_066_Method_References.java ├── src_067_Method_References.java ├── src_068_Method_References.java ├── src_069_Method_References.java ├── src_070_Method_References.java ├── src_071_Method_References.java ├── src_072_Method_References.java ├── src_073_Method_References.java ├── src_074_Method_References.java ├── src_075_Method_References.java ├── src_076_Method_References.java ├── src_077_Method_References.java ├── src_078_Method_References.java ├── src_079_Method_References.java ├── src_080_Method_References.java ├── src_081_Method_References.java ├── src_082_Method_References.java ├── src_083_Method_References.java ├── src_084_Method_References.java ├── src_085_Method_References.java ├── src_086_Method_References.java ├── src_087_Method_References.java ├── src_088_Method_References.java ├── src_089_Method_References.java ├── src_090_Method_References.java ├── src_091_Method_References.java ├── src_092_Method_References.java ├── src_093_Method_References.java ├── src_094_Method_References.java ├── src_095_Method_References.java ├── src_096_Method_References.java ├── src_097_Method_References.java ├── src_098_Method_References.java ├── src_099_Method_References.java ├── src_100_Method_References.java ├── src_101_Method_References.java ├── src_102_Lexical_Scoping.java ├── src_103_Lexical_Scoping.java ├── src_104_Variable_Capture.java ├── src_105_Variable_Capture.java ├── src_106_Variable_Capture.java ├── src_107_Variable_Capture.java ├── src_108_Variable_Capture.java ├── src_109_Variable_Capture.java ├── src_110_Jumps_and_Exits.java ├── src_111_Recursive_Lambda_Expressions.java ├── src_112_Comparing_Objects.java ├── src_113_Comparing_Objects.java ├── src_114_Comparing_Objects.java ├── src_115_Exercises.java ├── src_116_Exercises.java ├── src_117_Exercises.java ├── src_118_Exercises.java ├── src_119_Exercises.java ├── src_120_Exercises.java ├── src_121_Exercises.java ├── src_122_Exercises.java ├── src_123_Exercises.java ├── src_124_Exercises.java ├── src_125_Exercises.java ├── src_126_Exercises.java ├── src_127_Exercises.java └── src_128_Exercises.java ├── 050 ├── src_001_What_Is_a_Thread_.misc ├── src_002_Creating_Threads_in_Java.java ├── src_003_Creating_Threads_in_Java.java ├── src_004_Specifying_Your_Code_for_a_Thread.java ├── src_005_Specifying_Your_Code_for_a_Thread.java ├── src_006_Specifying_Your_Code_for_a_Thread.java ├── src_007_Specifying_Your_Code_for_a_Thread.java ├── src_008_Specifying_Your_Code_for_a_Thread.java ├── src_009_Specifying_Your_Code_for_a_Thread.java ├── src_010_Specifying_Your_Code_for_a_Thread.java ├── src_011_Specifying_Your_Code_for_a_Thread.java ├── src_012_Specifying_Your_Code_for_a_Thread.java ├── src_013_Issues_in_Using_Multiple_Threads.java ├── src_014_Issues_in_Using_Multiple_Threads.java ├── src_015_Object_s_Monitor_and_Threads_Synchronization.java ├── src_016_Object_s_Monitor_and_Threads_Synchronization.java ├── src_017_Object_s_Monitor_and_Threads_Synchronization.java ├── src_018_Object_s_Monitor_and_Threads_Synchronization.java ├── src_019_Object_s_Monitor_and_Threads_Synchronization.java ├── src_020_Object_s_Monitor_and_Threads_Synchronization.java ├── src_021_Object_s_Monitor_and_Threads_Synchronization.java ├── src_022_Object_s_Monitor_and_Threads_Synchronization.java ├── src_023_Object_s_Monitor_and_Threads_Synchronization.java ├── src_024_Object_s_Monitor_and_Threads_Synchronization.java ├── src_025_Object_s_Monitor_and_Threads_Synchronization.java ├── src_026_Object_s_Monitor_and_Threads_Synchronization.java ├── src_027_Object_s_Monitor_and_Threads_Synchronization.java ├── src_028_Object_s_Monitor_and_Threads_Synchronization.java ├── src_029_The_Producer_Consumer_Synchronization_Problem.java ├── src_030_Which_Thread_Is_Executing_.java ├── src_031_I_Will_Join_You_in_Heaven.java ├── src_032_I_Will_Join_You_in_Heaven.java ├── src_033_I_Will_Join_You_in_Heaven.java ├── src_034_Be_Considerate_to_Others_and_Yield.java ├── src_035_Lifecycle_of_a_Thread.java ├── src_036_Threads_Work_in_a_Group.java ├── src_037_Volatile_Variables.java ├── src_038_Stopping__Suspending__and_Resuming_Threads.java ├── src_039_Stopping__Suspending__and_Resuming_Threads.java ├── src_040_Stopping__Suspending__and_Resuming_Threads.java ├── src_041_Stopping__Suspending__and_Resuming_Threads.java ├── src_042_Spin_Wait_Hints.java ├── src_043_Atomic_Variables.java ├── src_044_Atomic_Variables.java ├── src_045_Atomic_Variables.java ├── src_046_Explicit_Locks.java ├── src_047_Explicit_Locks.java ├── src_048_Explicit_Locks.java ├── src_049_Synchronizers.java ├── src_050_Synchronizers.java ├── src_051_Synchronizers.java ├── src_052_Synchronizers.java ├── src_053_Synchronizers.java ├── src_054_Synchronizers.java ├── src_055_Synchronizers.java ├── src_056_Synchronizers.java ├── src_057_Synchronizers.java ├── src_058_Synchronizers.java ├── src_059_Synchronizers.java ├── src_060_Synchronizers.java ├── src_061_The_Executor_Framework.java ├── src_062_The_Executor_Framework.java ├── src_063_The_Executor_Framework.java ├── src_064_The_Executor_Framework.java ├── src_065_The_Executor_Framework.java ├── src_066_The_Executor_Framework.misc ├── src_067_The_Executor_Framework.java ├── src_068_The_Executor_Framework.java ├── src_069_The_Executor_Framework.java ├── src_070_The_Executor_Framework.java ├── src_071_The_Executor_Framework.java ├── src_072_The_Executor_Framework.java ├── src_073_The_Executor_Framework.java ├── src_074_The_Executor_Framework.java ├── src_075_The_Executor_Framework.java ├── src_076_The_Executor_Framework.java ├── src_077_The_Fork_Join_Framework.java ├── src_078_The_Fork_Join_Framework.java ├── src_079_The_Fork_Join_Framework.java ├── src_080_The_Fork_Join_Framework.java ├── src_081_The_Fork_Join_Framework.java ├── src_082_The_Fork_Join_Framework.java ├── src_083_The_Fork_Join_Framework.java ├── src_084_Thread_Local_Variables.java ├── src_085_Thread_Local_Variables.java ├── src_086_Thread_Local_Variables.java ├── src_087_Setting_Stack_Size_of_a_Thread.sh └── src_088_Exercises.java ├── 060 ├── src_001_What_Are_Streams_.java ├── src_002_What_Are_Streams_.java ├── src_003_What_Are_Streams_.java ├── src_004_What_Are_Streams_.java ├── src_005_What_Are_Streams_.java ├── src_006_A_Quick_Example.java ├── src_007_A_Quick_Example.java ├── src_008_A_Quick_Example.java ├── src_009_A_Quick_Example.java ├── src_010_A_Quick_Example.java ├── src_011_A_Quick_Example.java ├── src_012_Creating_Streams.java ├── src_013_Creating_Streams.java ├── src_014_Creating_Streams.java ├── src_015_Creating_Streams.java ├── src_016_Creating_Streams.java ├── src_017_Creating_Streams.java ├── src_018_Creating_Streams.java ├── src_019_Creating_Streams.java ├── src_020_Creating_Streams.java ├── src_021_Creating_Streams.java ├── src_022_Creating_Streams.java ├── src_023_Creating_Streams.java ├── src_024_Creating_Streams.java ├── src_025_Creating_Streams.java ├── src_026_Creating_Streams.java ├── src_027_Creating_Streams.java ├── src_028_Creating_Streams.java ├── src_029_Creating_Streams.java ├── src_030_Creating_Streams.java ├── src_031_Creating_Streams.java ├── src_032_Creating_Streams.java ├── src_033_Creating_Streams.java ├── src_034_Creating_Streams.java ├── src_035_Creating_Streams.java ├── src_036_Creating_Streams.java ├── src_037_Creating_Streams.java ├── src_038_Creating_Streams.java ├── src_039_Creating_Streams.java ├── src_040_Creating_Streams.java ├── src_041_Creating_Streams.java ├── src_042_Creating_Streams.java ├── src_043_Representing_an_Optional_Value.java ├── src_044_Representing_an_Optional_Value.java ├── src_045_Representing_an_Optional_Value.java ├── src_046_Representing_an_Optional_Value.java ├── src_047_Representing_an_Optional_Value.java ├── src_048_Representing_an_Optional_Value.java ├── src_049_Representing_an_Optional_Value.java ├── src_050_Representing_an_Optional_Value.java ├── src_051_Representing_an_Optional_Value.java ├── src_052_Representing_an_Optional_Value.java ├── src_053_Applying_Operations_to_Streams.java ├── src_054_Applying_Operations_to_Streams.java ├── src_055_Applying_Operations_to_Streams.java ├── src_056_Applying_Operations_to_Streams.java ├── src_057_Applying_Operations_to_Streams.java ├── src_058_Applying_Operations_to_Streams.java ├── src_059_Applying_Operations_to_Streams.java ├── src_060_Applying_Operations_to_Streams.java ├── src_061_Applying_Operations_to_Streams.java ├── src_062_Applying_Operations_to_Streams.java ├── src_063_Applying_Operations_to_Streams.java ├── src_064_Applying_Operations_to_Streams.java ├── src_065_Applying_Operations_to_Streams.java ├── src_066_Applying_Operations_to_Streams.java ├── src_067_Applying_Operations_to_Streams.java ├── src_068_Applying_Operations_to_Streams.java ├── src_069_Applying_Operations_to_Streams.java ├── src_070_Applying_Operations_to_Streams.java ├── src_071_Applying_Operations_to_Streams.java ├── src_072_Applying_Operations_to_Streams.java ├── src_073_Applying_Operations_to_Streams.java ├── src_074_Applying_Operations_to_Streams.java ├── src_075_Applying_Operations_to_Streams.java ├── src_076_Applying_Operations_to_Streams.java ├── src_077_Applying_Operations_to_Streams.java ├── src_078_Applying_Operations_to_Streams.java ├── src_079_Applying_Operations_to_Streams.java ├── src_080_Applying_Operations_to_Streams.java ├── src_081_Applying_Operations_to_Streams.java ├── src_082_Applying_Operations_to_Streams.java ├── src_083_Applying_Operations_to_Streams.java ├── src_084_Applying_Operations_to_Streams.java ├── src_085_Applying_Operations_to_Streams.java ├── src_086_Applying_Operations_to_Streams.java ├── src_087_Applying_Operations_to_Streams.java ├── src_088_Collecting_Data_Using_Collectors.java ├── src_089_Collecting_Data_Using_Collectors.java ├── src_090_Collecting_Data_Using_Collectors.java ├── src_091_Collecting_Data_Using_Collectors.java ├── src_092_Collecting_Data_Using_Collectors.java ├── src_093_Collecting_Data_Using_Collectors.java ├── src_094_Collecting_Data_Using_Collectors.java ├── src_095_Collecting_Data_Using_Collectors.java ├── src_096_Collecting_Data_Using_Collectors.java ├── src_097_Collecting_Data_Using_Collectors.java ├── src_098_Collecting_Data_Using_Collectors.java ├── src_099_Collecting_Summary_Statistics.java ├── src_100_Collecting_Summary_Statistics.java ├── src_101_Collecting_Data_in_Maps.java ├── src_102_Collecting_Data_in_Maps.java ├── src_103_Collecting_Data_in_Maps.java ├── src_104_Collecting_Data_in_Maps.java ├── src_105_Grouping_Data.java ├── src_106_Grouping_Data.java ├── src_107_Grouping_Data.java ├── src_108_Grouping_Data.java ├── src_109_Grouping_Data.java ├── src_110_Partitioning_Data.java ├── src_111_Partitioning_Data.java ├── src_112_Adapting_the_Collector_Results.java ├── src_113_Adapting_the_Collector_Results.java ├── src_114_Adapting_the_Collector_Results.java ├── src_115_Parallel_Streams.java ├── src_116_Parallel_Streams.java ├── src_117_Parallel_Streams.java ├── src_118_Parallel_Streams.java ├── src_119_Parallel_Streams.java ├── src_120_Exercises.java ├── src_121_Exercises.misc ├── src_122_Exercises.java ├── src_123_Exercises.java ├── src_124_Exercises.java ├── src_125_Exercises.misc ├── src_126_Exercises.java ├── src_127_Exercises.java ├── src_128_Exercises.misc ├── src_129_Exercises.java ├── src_130_Exercises.misc ├── src_131_Exercises.java └── src_132_Exercises.misc ├── 070 ├── src_001_Discovering_Services.java ├── src_002_Discovering_Services.java ├── src_003_Discovering_Services.java ├── src_004_Discovering_Services.java ├── src_005_Providing_Service_Implementations.java ├── src_006_Providing_Service_Implementations.java ├── src_007_Providing_Service_Implementations.java ├── src_008_Defining_the_Service_Interface.java ├── src_009_Obtaining_Service_Provider_Instances.java ├── src_010_Obtaining_Service_Provider_Instances.java ├── src_011_Obtaining_Service_Provider_Instances.java ├── src_012_Obtaining_Service_Provider_Instances.java ├── src_013_Obtaining_Service_Provider_Instances.java ├── src_014_Obtaining_Service_Provider_Instances.java ├── src_015_Obtaining_Service_Provider_Instances.java ├── src_016_Obtaining_Service_Provider_Instances.java ├── src_017_Obtaining_Service_Provider_Instances.java ├── src_018_Defining_the_Service.java ├── src_019_Defining_Service_Providers.java ├── src_020_Defining_Service_Providers.java ├── src_021_Testing_the_Prime_Service.java ├── src_022_Testing_the_Prime_Service.java ├── src_023_Testing_the_Prime_Service.java ├── src_024_Testing_the_Prime_Service.java ├── src_025_Testing_Prime_Service_in_Legacy_Mode.java ├── src_026_Testing_Prime_Service_in_Legacy_Mode.java ├── src_027_Testing_Prime_Service_in_Legacy_Mode.java └── src_028_Testing_Prime_Service_in_Legacy_Mode.java ├── 080 ├── src_001_IP_Addressing_Scheme.misc ├── src_002_Representing_a_Machine_Address.java ├── src_003_Representing_a_Machine_Address.java ├── src_004_Representing_a_Socket_Address.java ├── src_005_Creating_a_TCP_Server_Socket.java ├── src_006_Creating_a_TCP_Server_Socket.java ├── src_007_Creating_a_TCP_Server_Socket.java ├── src_008_Creating_a_TCP_Server_Socket.java ├── src_009_Creating_a_TCP_Server_Socket.java ├── src_010_Creating_a_TCP_Server_Socket.java ├── src_011_Creating_a_TCP_Server_Socket.java ├── src_012_Creating_a_TCP_Server_Socket.java ├── src_013_Creating_a_TCP_Client_Socket.java ├── src_014_Putting_a_TCP_Server_and_Clients_Together.misc ├── src_015_Putting_a_TCP_Server_and_Clients_Together.java ├── src_016_Working_with_UDP_Sockets.java ├── src_017_Working_with_UDP_Sockets.java ├── src_018_Working_with_UDP_Sockets.java ├── src_019_Working_with_UDP_Sockets.java ├── src_020_Creating_a_UDP_Echo_Server.java ├── src_021_Creating_a_UDP_Echo_Server.misc ├── src_022_Creating_a_UDP_Echo_Server.misc ├── src_023_A_Connected_UDP_Socket.java ├── src_024_UDP_Multicast_Sockets.misc ├── src_025_UDP_Multicast_Sockets.misc ├── src_026_URI__URL__and_URN.misc ├── src_027_URI__URL__and_URN.misc ├── src_028_URI__URL__and_URN.misc ├── src_029_URI__URL__and_URN.misc ├── src_030_URI__URL__and_URN.misc ├── src_031_URI__URL__and_URN.misc ├── src_032_URI_and_URL_as_Java_Objects.java ├── src_033_URI_and_URL_as_Java_Objects.java ├── src_034_URI_and_URL_as_Java_Objects.java ├── src_035_URI_and_URL_as_Java_Objects.java ├── src_036_Accessing_the_Contents_of_a_URL.java ├── src_037_Accessing_the_Contents_of_a_URL.java ├── src_038_Accessing_the_Contents_of_a_URL.java ├── src_039_Accessing_the_Contents_of_a_URL.java ├── src_040_Accessing_the_Contents_of_a_URL.java ├── src_041_Non_Blocking_Socket_Programming.java ├── src_042_Non_Blocking_Socket_Programming.java ├── src_043_Non_Blocking_Socket_Programming.java ├── src_044_Non_Blocking_Socket_Programming.java ├── src_045_Non_Blocking_Socket_Programming.java ├── src_046_Non_Blocking_Socket_Programming.java ├── src_047_Non_Blocking_Socket_Programming.java ├── src_048_Non_Blocking_Socket_Programming.java ├── src_049_Non_Blocking_Socket_Programming.java ├── src_050_Non_Blocking_Socket_Programming.java ├── src_051_Non_Blocking_Socket_Programming.java ├── src_052_Non_Blocking_Socket_Programming.java ├── src_053_Non_Blocking_Socket_Programming.java ├── src_054_Non_Blocking_Socket_Programming.java ├── src_055_Non_Blocking_Socket_Programming.java ├── src_056_Socket_Security_Permissions.java ├── src_057_Socket_Security_Permissions.java ├── src_058_Asynchronous_Socket_Channels.java ├── src_059_Asynchronous_Socket_Channels.java ├── src_060_Asynchronous_Socket_Channels.java ├── src_061_Asynchronous_Socket_Channels.java ├── src_062_Asynchronous_Socket_Channels.java ├── src_063_Asynchronous_Socket_Channels.java ├── src_064_Asynchronous_Socket_Channels.java ├── src_065_Asynchronous_Socket_Channels.java ├── src_066_Asynchronous_Socket_Channels.java ├── src_067_Asynchronous_Socket_Channels.java ├── src_068_Asynchronous_Socket_Channels.misc ├── src_069_Asynchronous_Socket_Channels.misc ├── src_070_Asynchronous_Socket_Channels.java ├── src_071_Asynchronous_Socket_Channels.misc ├── src_072_Datagram_Oriented_Socket_Channels.java ├── src_073_Datagram_Oriented_Socket_Channels.java ├── src_074_Datagram_Oriented_Socket_Channels.java ├── src_075_Datagram_Oriented_Socket_Channels.java ├── src_076_Datagram_Oriented_Socket_Channels.java ├── src_077_Datagram_Oriented_Socket_Channels.java ├── src_078_Multicasting_Using_Datagram_Channels.java ├── src_079_Multicasting_Using_Datagram_Channels.java ├── src_080_Multicasting_Using_Datagram_Channels.java ├── src_081_Multicasting_Using_Datagram_Channels.java ├── src_082_Multicasting_Using_Datagram_Channels.java ├── src_083_Multicasting_Using_Datagram_Channels.java ├── src_084_Multicasting_Using_Datagram_Channels.java └── src_085_Multicasting_Using_Datagram_Channels.java ├── 090 ├── src_001_Developing_an_RMI_Application.java ├── src_002_Developing_an_RMI_Application.java ├── src_003_Developing_an_RMI_Application.java ├── src_004_Developing_an_RMI_Application.java ├── src_005_Developing_an_RMI_Application.java ├── src_006_Developing_an_RMI_Application.java ├── src_007_Developing_an_RMI_Application.java ├── src_008_Developing_an_RMI_Application.java ├── src_009_Developing_an_RMI_Application.java ├── src_010_Developing_an_RMI_Application.java ├── src_011_Developing_an_RMI_Application.java ├── src_012_Running_the_RMI_Application.java ├── src_013_Running_the_RMI_Application.java ├── src_014_Running_the_RMI_Application.sh ├── src_015_Running_the_RMI_Application.sh ├── src_016_Running_the_RMI_Application.sh ├── src_017_Running_the_RMI_Application.sh ├── src_018_Running_the_RMI_Application.sh ├── src_019_Running_the_RMI_Application.sh ├── src_020_Running_the_RMI_Application.sh ├── src_021_Running_the_RMI_Application.sh ├── src_022_Running_the_RMI_Application.sh ├── src_023_Running_the_RMI_Application.sh ├── src_024_Troubleshooting_an_RMI_Application.misc ├── src_025_Troubleshooting_an_RMI_Application.misc ├── src_026_Troubleshooting_an_RMI_Application.misc ├── src_027_Troubleshooting_an_RMI_Application.misc ├── src_028_Troubleshooting_an_RMI_Application.misc ├── src_029_Debugging_an_RMI_Application.sh ├── src_030_Debugging_an_RMI_Application.java ├── src_031_Debugging_an_RMI_Application.java ├── src_032_Dynamic_Class_Downloading.sh ├── src_033_Garbage_Collection_of_Remote_Objects.sh └── src_034_Garbage_Collection_of_Remote_Objects.java ├── 9781484271346.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /010/src_001_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | public class Employee { 2 |     public void setSalary(double salary) {     3 |         System.out.println("Employee.setSalary():" + 4 | salary); 5 |     } 6 | } 7 | -------------------------------------------------------------------------------- /010/src_002_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | public class Manager extends Employee { 2 |     // Override setSalary() in the Employee class 3 |     public void setSalary(int salary) { 4 |         System.out.println("Manager.setSalary():" + 5 | salary); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /010/src_003_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | Employee ken = new Manager(); 2 | int salary = 200; 3 | ken.setSalary(salary); 4 | Employee.setSalary():200.0 5 | -------------------------------------------------------------------------------- /010/src_004_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | public class Manager extends Employee { 2 |     @Override 3 |     public void setSalary(int salary) { 4 |         System.out.println("Manager.setSalary():" + 5 | salary); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /010/src_005_What_Are_Annotations_.misc: -------------------------------------------------------------------------------- 1 | Manager.java:2: error: method does not override or 2 | implement a method from a supertype 3 |         @Override 4 |         ^ 5 | 1 error 6 | -------------------------------------------------------------------------------- /010/src_006_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | public class Manager extends Employee { 2 |     @Override 3 |     public void setSalary(double salary) { 4 |         System.out.println("Manager.setSalary():" + 5 | salary); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /010/src_007_What_Are_Annotations_.java: -------------------------------------------------------------------------------- 1 | Employee ken = new Manager(); 2 | int salary = 200; 3 | ken.setSalary(salary); 4 | Manager.setSalary():200.0 5 | -------------------------------------------------------------------------------- /010/src_008_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | [modifiers] @ interface { 2 |     // Annotation type body goes here 3 | } 4 | -------------------------------------------------------------------------------- /010/src_009_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | @annotationType(name1=value1, name2=value2, name3=value3...) 2 | -------------------------------------------------------------------------------- /010/src_010_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | @Version(major=1, minor=0) 2 | -------------------------------------------------------------------------------- /010/src_011_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | @com.jdojo.annotation.Version(major=0, minor=1) 2 | -------------------------------------------------------------------------------- /010/src_012_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | @Version(major=1, minor=0) 2 | public class VersionTest { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /010/src_013_Declaring_an_Annotation_Type.java: -------------------------------------------------------------------------------- 1 | // Style #1 2 | @Version(major=1, minor=0) public class VersionTest { 3 |     // Code goes here 4 | } 5 | 6 | // Style #2 7 | public @Version(major=1, minor=0) 8 | class VersionTest { 9 |     // Code goes here 10 | } 11 | -------------------------------------------------------------------------------- /010/src_014_Restrictions_on_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | // Won't compile 2 | public @interface WrongVersion extends BasicVersion { 3 |     int extended(); 4 | } 5 | -------------------------------------------------------------------------------- /010/src_015_Restrictions_on_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | package java.lang.annotation; 2 | public interface Annotation { 3 |     boolean equals(Object obj); 4 |     int hashCode(); 5 |     String toString(); 6 |     Class annotationType(); 7 | } 8 | -------------------------------------------------------------------------------- /010/src_016_Restrictions_on_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | // Won't compile 2 | public @interface WrongVersion { 3 |     // Cannot have parameters 4 |     String concatenate(int major, int minor); 5 | } 6 | -------------------------------------------------------------------------------- /010/src_017_Restrictions_on_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | // Won't compile 2 | public @interface WrongVersion { 3 |     int major() throws Exception; // Cannot have a 4 | // throws clause 5 |     int minor(); // OK 6 | } 7 | -------------------------------------------------------------------------------- /010/src_018_Restrictions_on_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | public @interface GoodOne { 2 |     Class element1();                 3 | // <- Any Class type 4 |     Class element2();           5 | // <- Only Test class type 6 |     Class element3(); 7 | // <- Test or its subclass type 8 | } 9 | -------------------------------------------------------------------------------- /010/src_019_Default_Value_of_an_Annotation_Element.java: -------------------------------------------------------------------------------- 1 | [modifiers] @interface { 2 |      () default ; 3 | } 4 | -------------------------------------------------------------------------------- /010/src_020_Default_Value_of_an_Annotation_Element.java: -------------------------------------------------------------------------------- 1 | public @interface Version { 2 |     int major(); 3 |     int minor() default 0; // Set zero as default value 4 | // for minor 5 | } 6 | -------------------------------------------------------------------------------- /010/src_021_Default_Value_of_an_Annotation_Element.java: -------------------------------------------------------------------------------- 1 | @Version(major=1)          // minor is zero, which is 2 | // its default value 3 | @Version(major=2)          // minor is zero, which is 4 | // its default value 5 | @Version(major=2, minor=1) // minor is 1, which is the 6 | // specified value 7 | -------------------------------------------------------------------------------- /010/src_023_Annotation_Type_and_Its_Instances.java: -------------------------------------------------------------------------------- 1 | Version v = null; // Here, Version is an annotation type 2 | -------------------------------------------------------------------------------- /010/src_024_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | public @interface PrimitiveAnnTest { 2 |     byte a(); 3 |     short b(); 4 |     int c(); 5 |     long d(); 6 |     float e(); 7 |     double f(); 8 |     boolean g(); 9 |     char h(); 10 | } 11 | -------------------------------------------------------------------------------- /010/src_025_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @PrimitiveAnnTest(a=1, b=2, c=3, d=4, e=12.34F, f=1.89, 2 | g=true, h='Y') 3 | -------------------------------------------------------------------------------- /010/src_026_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Version(major=2+1, minor=(int)13.2) 2 | @Version(major=3, minor=13) 3 | -------------------------------------------------------------------------------- /010/src_027_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Name(first="John", last="Jacobs") 2 | public class NameTest { 3 |     @Name(first="Wally", last="Inman") 4 |     public void aMethod() { 5 |         // More code goes here... 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /010/src_028_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Name(first="Jo" + "hn", last="Ja" + "cobs") 2 | @Name(first="John", last="Jacobs") 3 | -------------------------------------------------------------------------------- /010/src_029_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Name(first="Mr. " + Test.UNKNWON, last=Test.UNKNOWN) 2 | -------------------------------------------------------------------------------- /010/src_030_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Name(first=new String("John"), last="Jacobs") 2 | -------------------------------------------------------------------------------- /010/src_032_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | import com.jdojo.annotation.Review.ReviewStatus; 2 | import com.jdojo.annotation.Review; 3 | @Review(status=ReviewStatus.PASSED) 4 | public class Test { 5 |     // Code goes here 6 | } 7 | -------------------------------------------------------------------------------- /010/src_033_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @Description(name=@Name(first="John", last="Jacobs"), 2 | version=@Version(major=1, minor=2), 3 |     comments="Just a test class") 4 | public class Test { 5 |     // Code goes here 6 | } 7 | -------------------------------------------------------------------------------- /010/src_034_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @ToDo(items={"Add readFile method", "Add error handling"}) 2 | public class Test { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /010/src_035_Using_Annotations.java: -------------------------------------------------------------------------------- 1 | @ToDo(items={"Add error handling"}) 2 | @ToDo(items="Add error handling") 3 | -------------------------------------------------------------------------------- /010/src_036_No_Null_Value_in_an_Annotation.java: -------------------------------------------------------------------------------- 1 | @ToDo(items=null) 2 | @Name(first=null, last="Jacobs") 3 | -------------------------------------------------------------------------------- /010/src_037_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | public @interface Enabled { 2 |     boolean status() default true; 3 | } 4 | -------------------------------------------------------------------------------- /010/src_038_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | @Enabled 2 | public class Test { 3 |     // Code goes here 4 | } 5 | 6 | @Enabled() 7 | public class Test { 8 |     // Code goes here 9 | } 10 | -------------------------------------------------------------------------------- /010/src_039_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | public @interface Company { 2 |     String value(); // the element name is value 3 | } 4 | -------------------------------------------------------------------------------- /010/src_040_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | @Company(value="Abc Inc.") 2 | @Company("Abc Inc.") 3 | public class Test { 4 |     // Code goes here 5 | } 6 | -------------------------------------------------------------------------------- /010/src_041_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | public @interface Reviewers { 2 |     String[] value(); // the element name is value 3 | } 4 | -------------------------------------------------------------------------------- /010/src_042_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | // No need to specify name of the element 2 | @Reviewers({"John Jacobs", "Wally Inman"}) 3 | public class Test { 4 |     // Code goes here 5 | } 6 | -------------------------------------------------------------------------------- /010/src_043_Shorthand_Annotation_Syntax.java: -------------------------------------------------------------------------------- 1 | @Reviewers("John Jacobs") 2 | public class Test { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /010/src_045_Marker_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | public @interface Marker { 2 |     // No element declarations 3 | } 4 | @Marker 5 | public class Test { 6 |     // Code goes here 7 | } 8 | -------------------------------------------------------------------------------- /010/src_046_Marker_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | public class Calculator { 2 | ... 3 | @Monitor 4 | public void calc() { 5 | ... 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /010/src_047_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | // Version.java 2 | package com.jdojo.annotation; 3 | import java.lang.annotation.Target; 4 | import java.lang.annotation.ElementType; 5 | @Target({ElementType.TYPE, ElementType.CONSTRUCTOR, 6 | ElementType.METHOD}) 7 | public @interface Version { 8 |     int major(); 9 |     int minor(); 10 | } 11 | -------------------------------------------------------------------------------- /010/src_048_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | public class WontCompile { 2 |     // A compile-time error. Version annotation cannot 3 | // be used on a field. 4 |     @Version(major = 1, minor = 1) 5 |     int id = 110; 6 | } 7 | -------------------------------------------------------------------------------- /010/src_052_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | public @interface Ann2 { 2 |     int id(); 3 | } 4 | @Inherited 5 | public @interface Ann3 { 6 |     int id(); 7 | } 8 | -------------------------------------------------------------------------------- /010/src_053_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | @Ann2(id=505) 2 | @Ann3(id=707) 3 | public class A { 4 |     // Code for class A goes here 5 | } 6 | // Class B inherits Ann3(id=707) annotation from the 7 | // class A 8 | public class B extends A { 9 |     // Code for class B goes here 10 | } 11 | -------------------------------------------------------------------------------- /010/src_054_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | package com.jdojo.annotation; 2 | @Version(major=1, minor=0) 3 | public class Test { 4 |     // Code for Test class goes here 5 | } 6 | -------------------------------------------------------------------------------- /010/src_055_Meta_Annotation_Types.java: -------------------------------------------------------------------------------- 1 | @ChangeLog(date="08/28/2017", 2 | comments="Declared the class") 3 | @ChangeLog(date="09/21/2017", 4 | comments="Added the process() method") 5 | public class Test { 6 |     public static void process() { 7 |         // Code goes here 8 |     }     9 | } 10 | -------------------------------------------------------------------------------- /010/src_057_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | jdeprscan [options] {dir|jar|class} 2 | -------------------------------------------------------------------------------- /010/src_058_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | C:\Java9LanguageFeatures>jdeprscan --list --release 16 2 | @Deprecated(since="16", forRemoval=true) 3 | javax.management.relation.RoleStatus() 4 | @Deprecated(since="9") interface 5 | java.beans.AppletInitializer 6 | ... 7 | -------------------------------------------------------------------------------- /010/src_059_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | C:\Java9LanguageFeatures>jdeprscan --list --for-removal ^ 2 | --release 16 3 | @Deprecated(since="16", forRemoval=true) 4 | javax.management.relation.RoleStatus() 5 | ... 6 | -------------------------------------------------------------------------------- /010/src_060_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | C:\ Java9LanguageFeatures >jdeprscan --list --release 8 2 | @Deprecated class javax.swing.text.TableView.TableCell 3 | ... 4 | -------------------------------------------------------------------------------- /010/src_061_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | C:\Java9LanguageFeatures>jdeprscan --release 16 ^ 2 | java.lang.Thread 3 | class java/lang/Thread uses deprecated method 4 | java/lang/Thread::resume()V (forRemoval=true) 5 | -------------------------------------------------------------------------------- /010/src_062_Commonly_Used_Standard_Annotations.sh: -------------------------------------------------------------------------------- 1 | C:\Java9LanguageFeatures>jdeprscan --release 16 ^ 2 | path/to/folder 3 | class com/test/Jdk17 uses deprecated method 4 | java/lang/Integer::(I)V (forRemoval=true) 5 | -------------------------------------------------------------------------------- /010/src_064_Commonly_Used_Standard_Annotations.java: -------------------------------------------------------------------------------- 1 | public class A { 2 |     public void m1() { 3 |     } 4 | } 5 | public class B extends A { 6 |     @Override 7 |     public void m1() { 8 |     } 9 | } 10 | -------------------------------------------------------------------------------- /010/src_065_Commonly_Used_Standard_Annotations.java: -------------------------------------------------------------------------------- 1 | // Won't compile because m2() does not override any method 2 | public class C extends A { 3 |     @Override 4 |     public void m2() { 5 |     } 6 | } 7 | -------------------------------------------------------------------------------- /010/src_066_Commonly_Used_Standard_Annotations.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Runner { 3 |     void run(); 4 | } 5 | -------------------------------------------------------------------------------- /010/src_067_Commonly_Used_Standard_Annotations.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Job { 3 |     void run(); 4 |     void abort(); 5 | } 6 | -------------------------------------------------------------------------------- /010/src_068_Commonly_Used_Standard_Annotations.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public class Test { 3 |     public void test() { 4 |         // Code goes here 5 |     } 6 | } 7 | -------------------------------------------------------------------------------- /010/src_069_Annotating_Packages.java: -------------------------------------------------------------------------------- 1 | // package-info.java 2 | @com.jdojo.myannotations.Author("John Jacobs") 3 | @Reviewer("Wally Inman") 4 | package com.jdojo.annotation; 5 | import com.jdojo.myannotations.Reviewer; 6 | -------------------------------------------------------------------------------- /010/src_070_Annotating_Modules.java: -------------------------------------------------------------------------------- 1 | @Deprecated(since="1.2", forRemoval=true) 2 | @SuppressWarnings("unchecked") 3 | module com.jdojo.myModule { 4 |     // Module statements go here 5 | } 6 | -------------------------------------------------------------------------------- /010/src_073_Annotation_Processing_at_Source_Code_Level.sh: -------------------------------------------------------------------------------- 1 | javac --processor-module-path MySourceFile.java 2 | -------------------------------------------------------------------------------- /010/src_074_Annotation_Processing_at_Source_Code_Level.sh: -------------------------------------------------------------------------------- 1 | javac -proc:only --processor-module-path ^ 2 | MySourceFile.java 3 | -------------------------------------------------------------------------------- /010/src_075_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | public class VersionProcessor extends AbstractProcessor { 2 |     // Code goes here 3 | } 4 | -------------------------------------------------------------------------------- /010/src_076_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | @SupportedAnnotationTypes({"com.jdojo.annotation.Version"}) 2 | public class VersionProcessor extends AbstractProcessor { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /010/src_077_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | @SupportedAnnotationTypes({"com.jdojo.Ann1", 2 | "com.jdojo.annotation.*"}) 3 | -------------------------------------------------------------------------------- /010/src_078_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | @SupportedAnnotationTypes({"com.jdojo.annotation.Version"}) 2 | @SupportedSourceVersion(SourceVersion.RELEASE_17) 3 | public class VersionProcessor extends AbstractProcessor { 4 |     // Code goes here 5 | } 6 | -------------------------------------------------------------------------------- /010/src_079_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | public boolean process(Set 2 | annotations, RoundEnvironment roundEnv) { 3 |     // The processor code goes here 4 | } 5 | -------------------------------------------------------------------------------- /010/src_080_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | for (TypeElement currentAnnotation : annotations) { 2 |     // Code to validate each Version annotation goes here 3 | } 4 | -------------------------------------------------------------------------------- /010/src_081_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | Name qualifiedName = currentAnnotation.getQualifiedName(); 2 | // Check if it is a Version annotation 3 | if (qualifiedName.contentEquals( 4 | "com.jdojo.annotation.Version")) { 5 |     // Get Version annotation values to validate 6 | } 7 | -------------------------------------------------------------------------------- /010/src_082_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | Set annotatedElements = 2 | roundEnv.getElementsAnnotatedWith(currentAnnotation); 3 | -------------------------------------------------------------------------------- /010/src_083_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | for (Element element : annotatedElements) { 2 |     Version v = element.getAnnotation(Version.class); 3 |     int major = v.major(); 4 |     int minor = v.minor(); 5 |     if (major < 0 || minor < 0) { 6 |         // Print the error message here 7 |     } 8 | } 9 | -------------------------------------------------------------------------------- /010/src_084_Annotation_Processing_at_Source_Code_Level.java: -------------------------------------------------------------------------------- 1 | String errorMsg = "Version cannot be negative. major=" + 2 | major + " minor=" + minor; 3 | Messager messager = this.processingEnv.getMessager(); 4 | messager.printMessage(Kind.ERROR, errorMsg, element); 5 | -------------------------------------------------------------------------------- /010/src_086_Exercises.java: -------------------------------------------------------------------------------- 1 | public @interface Version extends BasicVersion { 2 |     int extended(); 3 | } 4 | -------------------------------------------------------------------------------- /010/src_087_Exercises.java: -------------------------------------------------------------------------------- 1 | public @interface Author { 2 |     void name(String firstName, String lastName); 3 | } 4 | -------------------------------------------------------------------------------- /010/src_088_Exercises.java: -------------------------------------------------------------------------------- 1 | public @interface Status { 2 |     boolean approved() default false; 3 |     String approvedBy(); 4 | } 5 | -------------------------------------------------------------------------------- /010/src_089_Exercises.java: -------------------------------------------------------------------------------- 1 | public @interface LuckyNumber { 2 |     int[] value() default {19}; 3 | } 4 | -------------------------------------------------------------------------------- /010/src_090_Exercises.java: -------------------------------------------------------------------------------- 1 | LuckNumber myLuckNumber = null; 2 | -------------------------------------------------------------------------------- /010/src_091_Exercises.java: -------------------------------------------------------------------------------- 1 | module jdojo.annotation.exercises { 2 |     exports com.jdojo.annotation.exercises; 3 | } 4 | -------------------------------------------------------------------------------- /020/src_001_Loading_a_Class.java: -------------------------------------------------------------------------------- 1 | Class testClass = Test.class; 2 | -------------------------------------------------------------------------------- /020/src_002_Loading_a_Class.java: -------------------------------------------------------------------------------- 1 | Test t = new Test(); 2 | Class testClass = t.class; // A compile-time error. 3 | // Must use Test.class 4 | -------------------------------------------------------------------------------- /020/src_003_Loading_a_Class.java: -------------------------------------------------------------------------------- 1 | Test testRef = new Test(); 2 | Class testClass = testRef.getClass(); 3 | -------------------------------------------------------------------------------- /020/src_004_Loading_a_Class.java: -------------------------------------------------------------------------------- 1 | Class testClass = Class.forName("pkg1.Test"); 2 | -------------------------------------------------------------------------------- /020/src_005_Class_Loaders.java: -------------------------------------------------------------------------------- 1 | Class cls = Bulb.class; 2 | ClassLoader loader = cls.getClassLoader(); 3 | -------------------------------------------------------------------------------- /020/src_006_Reflecting_on_Classes.java: -------------------------------------------------------------------------------- 1 | String simpleName = c.getSimpleName(); 2 | -------------------------------------------------------------------------------- /020/src_007_Reflecting_on_Classes.java: -------------------------------------------------------------------------------- 1 | public abstract class MyClass { 2 |     // Code goes here   3 | } 4 | -------------------------------------------------------------------------------- /020/src_008_Reflecting_on_Classes.java: -------------------------------------------------------------------------------- 1 | // You need to AND the returned value from the 2 | // getModifiers() method with appropriate value returned 3 | // from xxxModifiers() method of the Modifiers class 4 | int mod = cls.getModifiers() & Modifier.classModifiers(); 5 | String modStr = Modifier.toString(mod); 6 | -------------------------------------------------------------------------------- /020/src_009_Reflecting_on_Classes.java: -------------------------------------------------------------------------------- 1 | Class superClass = cls.getSuperclass(); 2 | if (superClass != null) { 3 |     String superClassName = superClass.getSimpleName(); 4 | } 5 | -------------------------------------------------------------------------------- /020/src_010_Reflecting_on_Classes.java: -------------------------------------------------------------------------------- 1 | // Get all interfaces implemented by cls 2 | Class[] interfaces = cls.getInterfaces(); 3 | -------------------------------------------------------------------------------- /020/src_011_Reflecting_on_Fields.java: -------------------------------------------------------------------------------- 1 | interface IConstants { 2 |     int DAYS_IN_WEEK = 7; 3 | } 4 | class A implements IConstants { 5 |     private int aPrivate; 6 |     public int aPublic; 7 |     protected int aProtected; 8 | } 9 | class B extends A { 10 |     private int bPrivate; 11 |     public int bPublic; 12 |     protected int bProtected; 13 | } 14 | -------------------------------------------------------------------------------- /020/src_012_Creating_Objects.java: -------------------------------------------------------------------------------- 1 | public T newInstance(Object... initargs) throws 2 | InstantiationException, 3 | IllegalAccessException, 4 | IllegalArgumentException, 5 | InvocationTargetException 6 | -------------------------------------------------------------------------------- /020/src_013_Creating_Objects.java: -------------------------------------------------------------------------------- 1 | Class cls = Person.class; 2 | // Get the reference of the Person() constructor 3 | Constructor noArgsCons = cls.getConstructor(); 4 | Person p = noArgsCons.newInstance(); 5 | -------------------------------------------------------------------------------- /020/src_014_Invoking_Methods.java: -------------------------------------------------------------------------------- 1 | Class personClass = Person.class; 2 | Method setName = personClass.getMethod("setName", 3 | String.class); 4 | -------------------------------------------------------------------------------- /020/src_015_Invoking_Methods.java: -------------------------------------------------------------------------------- 1 | public Object invoke(Object obj, Object... args) 2 | throws IllegalAccessException, 3 | lllegalArgumentException, 4 | InvocationTargetException 5 | -------------------------------------------------------------------------------- /020/src_016_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | Person john = new Person(); 2 | String name = john.name; // Accessing the private field 3 | // name statically 4 | -------------------------------------------------------------------------------- /020/src_017_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | SecurityManager smgr = System.getSecurityManager(); 2 | if (smgr == null) { 3 |     System.out.println( 4 | "Security manager is not installed."); 5 | } 6 | -------------------------------------------------------------------------------- /020/src_019_Deep_Reflection.sh: -------------------------------------------------------------------------------- 1 | C:\Java17LanguageFeatures>java -Djava.security.manager ^ 2 | -Djava.security.policy=conf\myjava.policy ^ 3 | --module-path build\modules\jdojo.reflection ^ 4 | --module ^ 5 | jdojo.reflection/com.jdojo.reflection.IllegalAccess2 6 | 7 | Person: id=-1, name=Unknown 8 | name=Unknown 9 | -------------------------------------------------------------------------------- /020/src_022_Deep_Reflection.sh: -------------------------------------------------------------------------------- 1 | C:\Java17LanguageFeatures>java ^ 2 | --module-path ^ 3 | build\modules\jdojo.reflection; 4 | build\modules\jdojo.reflection.model 5 | --add-modules jdojo.reflection.model ^ 6 | --module ^ 7 | jdojo.reflection/com.jdojo.reflection.IllegalAccess3 8 | 9 | The Phone.number field is not accessible. 10 | -------------------------------------------------------------------------------- /020/src_023_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | Phone phone = new Phone(); 2 | -------------------------------------------------------------------------------- /020/src_024_Deep_Reflection.misc: -------------------------------------------------------------------------------- 1 | Exception in thread "main" java.lang.reflect. 2 | InaccessibleObjectException: Unable to make field private 3 | java.lang.String com.jdojo.reflection.model.Phone.number 4 | accessible: module jdojo.reflection.model does not "opens 5 | com.jdojo.reflection.model" to module jdojo.reflection 6 | ... 7 | -------------------------------------------------------------------------------- /020/src_025_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | opens [to ,...]; 2 | -------------------------------------------------------------------------------- /020/src_026_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | module M { 2 |     opens p to S, T; 3 | } 4 | -------------------------------------------------------------------------------- /020/src_027_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | module N { 2 |     opens q; 3 | } 4 | -------------------------------------------------------------------------------- /020/src_028_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | module J { 2 |     exports p; 3 |     opens p; 4 | } 5 | -------------------------------------------------------------------------------- /020/src_029_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | open module K { 2 |     // Other module statements go here 3 | } 4 | -------------------------------------------------------------------------------- /020/src_030_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | open module L { 2 |     opens p; // A compile-time error 3 |     // Other module statements go here 4 | } 5 | -------------------------------------------------------------------------------- /020/src_031_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | open module D { 2 |     exports p; 3 |     // Other module statements go here 4 | } 5 | -------------------------------------------------------------------------------- /020/src_032_Deep_Reflection.sh: -------------------------------------------------------------------------------- 1 | C:\Java17LanguageFeatures>java ^ 2 | --module-path build\modules\jdojo.reflection; 3 | build\modules\jdojo.reflection.model ^ 4 | --add-modules jdojo.reflection.model ^ 5 | --module ^ 6 | jdojo.reflection/com.jdojo.reflection.IllegalAccess3 7 | 8 | number=9999999999 9 | -------------------------------------------------------------------------------- /020/src_033_Deep_Reflection.java: -------------------------------------------------------------------------------- 1 | Long num = 1969L; 2 | System.out.println("#1: num = " + num); 3 | -------------------------------------------------------------------------------- /020/src_034_Deep_Reflection.sh: -------------------------------------------------------------------------------- 1 | C:\Java17LanguageFeatures>java ^ 2 | --module-path build\modules\jdojo.reflection ^ 3 | --module ^ 4 | jdojo.reflection/com.jdojo.reflection.IllegalAccessJDKType 5 | 6 | #1: num = 1969 7 | The Long.value field is not accessible. 8 | -------------------------------------------------------------------------------- /020/src_036_Reflecting_on_Arrays.java: -------------------------------------------------------------------------------- 1 | int[] ids = (int[]) Array.newInstance(int.class, 5); 2 | -------------------------------------------------------------------------------- /020/src_037_Reflecting_on_Arrays.java: -------------------------------------------------------------------------------- 1 | int[] ids = new int[5]; 2 | -------------------------------------------------------------------------------- /020/src_038_Reflecting_on_Arrays.java: -------------------------------------------------------------------------------- 1 | int[][] matrix = (int[][]) Array.newInstance( 2 | int.class, 5, 8); 3 | -------------------------------------------------------------------------------- /030/src_001_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | ObjectWrapper stringWrapper = new ObjectWrapper("Hello"); 2 | stringWrapper.set("Another string"); 3 | String myString = (String) stringWrapper.get(); 4 | -------------------------------------------------------------------------------- /030/src_002_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | ObjectWrapper stringWrapper = new ObjectWrapper("Hello"); 2 | stringWrapper.set(new Integer(101)); 3 | String myString =(String) stringWrapper.get(); 4 | -------------------------------------------------------------------------------- /030/src_003_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | public class Wrapper { 2 | } 3 | -------------------------------------------------------------------------------- /030/src_004_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | public class Wrapper { 2 | } 3 | public class Wrapper { 4 | } 5 | -------------------------------------------------------------------------------- /030/src_005_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | public class MyClass { 2 | } 3 | -------------------------------------------------------------------------------- /030/src_006_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | Wrapper greetingWrapper = 2 | new Wrapper("Hello"); 3 | -------------------------------------------------------------------------------- /030/src_007_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | greetingWrapper.set("Hi");                2 | // <- OK to pass a String 3 | String greeting = greetingWrapper.get(); 4 | // <- No need to cast 5 | -------------------------------------------------------------------------------- /030/src_008_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | // A compile-time error. You can use greetingWrapper 2 | // only to store a String. 3 | greetingWrapper.set(new Integer(101)); 4 | -------------------------------------------------------------------------------- /030/src_009_What_Are_Generics_.misc: -------------------------------------------------------------------------------- 1 | error: incompatible types: Integer cannot be converted to 2 | String 3 |         greetingWrapper.set(new Integer(101)); 4 | -------------------------------------------------------------------------------- /030/src_010_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | Wrapper idWrapper = 2 | new Wrapper(new Integer(101)); 3 | idWrapper.set(new Integer(897)); 4 | // <- OK to pass an Integer 5 | Integer id = idWrapper.get(); 6 | // A compile-time error. You can use idWrapper only 7 | // with an Integer. 8 | idWrapper.set("hello"); 9 | -------------------------------------------------------------------------------- /030/src_011_What_Are_Generics_.java: -------------------------------------------------------------------------------- 1 | Wrapper personWrapper = new Wrapper( 2 | new Person(1, "Chris")); 3 | personWrapper.set(new Person(2, "Laynie")); 4 | Person laynie = personWrapper.get(); 5 | -------------------------------------------------------------------------------- /030/src_012_Supertype_Subtype_Relationship.java: -------------------------------------------------------------------------------- 1 | Wrapper stringWrapper = 2 | new Wrapper("Hello"); 3 | stringWrapper.set("a string"); 4 | Wrapper objectWrapper = 5 | new Wrapper(new Object()); 6 | objectWrapper.set(new Object()); 7 | // Use a String object with objectWrapper 8 | objectWrapper.set("a string"); // OK 9 | -------------------------------------------------------------------------------- /030/src_013_Supertype_Subtype_Relationship.java: -------------------------------------------------------------------------------- 1 | objectWrapper = stringWrapper; 2 | -------------------------------------------------------------------------------- /030/src_014_Supertype_Subtype_Relationship.java: -------------------------------------------------------------------------------- 1 | // Now objectWrapper points to stringWrapper 2 | objectWrapper = stringWrapper; 3 | // We could store an Object in stringWrapper using 4 | // objectWrapper 5 | objectWrapper.set(new Object()); 6 | // The following statement will throw a runtime 7 | // ClassCastException 8 | String s = stringWrapper.get(); 9 | -------------------------------------------------------------------------------- /030/src_017_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | public class WrapperUtil { 2 |     public static 3 | void printDetails(Wrapper wrapper){ 4 |         // More code goes here 5 |     } 6 | } 7 | -------------------------------------------------------------------------------- /030/src_018_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Wrapper objectWrapper = 2 | new Wrapper(new Object()); 3 | WrapperUtil.printDetails(objectWrapper); // OK 4 | Wrapper stringWrapper = 5 | new Wrapper("Hello"); 6 | WrapperUtil.printDetails(stringWrapper); // A compile-time 7 | // error 8 | -------------------------------------------------------------------------------- /030/src_020_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | // Wrapper of String type 2 | Wrapper stringWrapper = new Wrapper("Hi"); 3 | // You can assign a Wrapper to Wrapper type 4 | Wrapper wildCardWrapper = stringWrapper; 5 | -------------------------------------------------------------------------------- /030/src_021_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | // wildCardWrapper has unknown type 2 | Wrapper wildCardWrapper; 3 | // Better to name it as an unknownWrapper 4 | Wrapper unknownWrapper; 5 | -------------------------------------------------------------------------------- /030/src_022_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | // Cannot use with new operator. It is a compile-time 2 | // error. 3 | new Wrapper(""); 4 | error: unexpected type 5 |         new Wrapper(""); 6 |                    ^ 7 |   required: class or interface without bounds 8 |   found:    ? 9 | 1 error 10 | -------------------------------------------------------------------------------- /030/src_023_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Wrapper unknownWrapper = new Wrapper("Hello"); 2 | -------------------------------------------------------------------------------- /030/src_024_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | String str = unknownWrapper.get(); 2 | error: incompatible types: CAP#1 cannot be converted 3 | to String 4 |         String str = unknownWrapper.get(); 5 |                                        ^ 6 |   where CAP#1 is a fresh type-variable: 7 |     CAP#1 extends Object from capture of ? 8 | 1 error 9 | -------------------------------------------------------------------------------- /030/src_025_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Object obj = unknownWrapper.get(); // OK 2 | -------------------------------------------------------------------------------- /030/src_026_Unbounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | unknownWrapper.set("Hello");        // A compile-time error 2 | unknownWrapper.set(new Integer());  // A compile-time error 3 | unknownWrapper.set(new Object());   // A compile-time error 4 | unknownWrapper.set(null);           // OK 5 | -------------------------------------------------------------------------------- /030/src_028_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | public static double sum(Wrapper n1, Wrapper n2) { 2 |     //Code goes here 3 | } 4 | -------------------------------------------------------------------------------- /030/src_029_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | // Try adding an Integer and a String 2 | sum(new Wrapper(new Integer(125)), 3 | new Wrapper("Hello")); 4 | -------------------------------------------------------------------------------- /030/src_030_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /030/src_031_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | public static double sum(Wrapper n1, 2 | Wrapper n2) { 3 |     Number num1 = n1.get(); 4 |     Number num2 = n2.get(); 5 |     double sum = num1.doubleValue() + num2.doubleValue(); 6 |     return sum; 7 | } 8 | -------------------------------------------------------------------------------- /030/src_032_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Number num1 = n1.get(); 2 | Number num2 = n2.get(); 3 | -------------------------------------------------------------------------------- /030/src_033_Upper_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Wrapper intWrapper = 2 | new Wrapper(new Integer(10)); 3 | Wrapper numberWrapper = intWrapper; 4 | // <- OK 5 | numberWrapper.set(new Integer(1220)); 6 | // <- A compile-time error 7 | numberWrapper.set(new Double(12.20)); 8 | // <- A compile-time error 9 | -------------------------------------------------------------------------------- /030/src_034_Lower_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | public class WrapperUtil { 2 |     public static void 3 | copy(Wrapper source, Wrapper dest) { 4 |         T value = source.get(); 5 |         dest.set(value); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /030/src_035_Lower_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Wrapper objectWrapper = 2 | new Wrapper(new Object()); 3 | Wrapper stringWrapper = 4 | new Wrapper("Hello"); 5 | WrapperUtil.copy(stringWrapper, objectWrapper); 6 | // <- A compile-time error 7 | -------------------------------------------------------------------------------- /030/src_036_Lower_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | public class WrapperUtil { 2 |     // New definition of the copy() method 3 |     public static void 4 | copy(Wrapper source, Wrapper dest){ 5 |         T value = source.get(); 6 |         dest.set(value); 7 |     } 8 | } 9 | -------------------------------------------------------------------------------- /030/src_037_Lower_Bounded_Wildcards.java: -------------------------------------------------------------------------------- 1 | Wrapper objectWrapper = 2 | new Wrapper(new Object()); 3 | Wrapper stringWrapper = 4 | new Wrapper("Hello"); 5 | WrapperUtil.copy(stringWrapper, objectWrapper); 6 | // <- OK with the new copy() method 7 | -------------------------------------------------------------------------------- /030/src_038_Generic_Methods_and_Constructors.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 |     public void m1(Wrapper a, Wrapper b, T c) { 3 |         // Do something 4 |     } 5 | } 6 | -------------------------------------------------------------------------------- /030/src_040_Generic_Methods_and_Constructors.java: -------------------------------------------------------------------------------- 1 | public static void copy(Wrapper source, 2 | Wrapper dest) { 3 |     T value = source.get(); 4 |     dest.set(value); 5 | } 6 | -------------------------------------------------------------------------------- /030/src_041_Generic_Methods_and_Constructors.java: -------------------------------------------------------------------------------- 1 | WrapperUtil.copy(iw1, iw2); 2 | -------------------------------------------------------------------------------- /030/src_042_Generic_Methods_and_Constructors.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 |     public Test(U k) { 3 |         // Do something 4 |     } 5 | } 6 | -------------------------------------------------------------------------------- /030/src_043_Generic_Methods_and_Constructors.java: -------------------------------------------------------------------------------- 1 | // Specify the actual type parameter for the constructor 2 | // as Double 3 | Test t1 = new Test( 4 | new Double(12.89)); 5 | // Let the compiler figure out that we are using Integer 6 | // as the actual type parameter for the constructor 7 | Test t2 = new Test(new Integer(123)); 8 | -------------------------------------------------------------------------------- /030/src_044_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | List list = new ArrayList(); 2 | -------------------------------------------------------------------------------- /030/src_045_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | List list = new ArrayList<>(); 2 | -------------------------------------------------------------------------------- /030/src_047_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | T1 var = new T3<>(constructor-arguments); 2 | -------------------------------------------------------------------------------- /030/src_048_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.List; 3 | // More code goes here... 4 | List list1 = Arrays.asList("A", "B"); 5 | List list2 = Arrays.asList(9, 19, 1969); 6 | -------------------------------------------------------------------------------- /030/src_049_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | List list3 = new ArrayList<>(list1); 2 | // <- Inferred type is String 3 | -------------------------------------------------------------------------------- /030/src_050_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | List list4 = new ArrayList<>(list2); 2 | // <- A compile-time error 3 | 4 | required: List 5 | found:    ArrayList 6 | 1 error 7 | -------------------------------------------------------------------------------- /030/src_051_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | List list5 = new ArrayList<>(); 2 | // <- Inferred type is String 3 | -------------------------------------------------------------------------------- /030/src_052_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | public static void process(List list) { 2 |     // Code goes here 3 | } 4 | -------------------------------------------------------------------------------- /030/src_053_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | // The inferred type is String 2 | process(new ArrayList<>()); 3 | -------------------------------------------------------------------------------- /030/src_054_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | // Not allowed in Java code. Cannot declare a variable 2 | // of an intersection type. 3 | Serializable & CharSequence var; 4 | // Allowed in Java code 5 | class Magic { 6 |     // More code goes here 7 | } 8 | -------------------------------------------------------------------------------- /030/src_055_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | public interface Callable { 2 |     V call() throws Exception; 3 | } 4 | -------------------------------------------------------------------------------- /030/src_056_Type_Inference_in_Generic_Object_Creation.java: -------------------------------------------------------------------------------- 1 | // A compile-time error in JDK8, but allowed in JDK9. 2 | Callable c = new Callable<>() { 3 |     @Override 4 |     public Integer call() { 5 |         return 100; 6 |     } 7 | }; 8 | -------------------------------------------------------------------------------- /030/src_057_Generics_and_Arrays.java: -------------------------------------------------------------------------------- 1 | public class GenericArrayTest { 2 |     private T[] elements; 3 |     public GenericArrayTest(int howMany) { 4 |         elements = new T[howMany]; // A compile-time error 5 |     } 6 |     // More code goes here 7 | } 8 | -------------------------------------------------------------------------------- /030/src_058_Generics_and_Arrays.java: -------------------------------------------------------------------------------- 1 | elements = new T[howMany]; // A compile-time error 2 | -------------------------------------------------------------------------------- /030/src_059_Generics_and_Arrays.java: -------------------------------------------------------------------------------- 1 | Wrapper[] gsArray = null; 2 | // Cannot create an array of generic type 3 | gsArray = new Wrapper[10]; // A compile-time error 4 | -------------------------------------------------------------------------------- /030/src_060_Generics_and_Arrays.java: -------------------------------------------------------------------------------- 1 | Wrapper[] anotherArray = new Wrapper[10]; // Ok 2 | -------------------------------------------------------------------------------- /030/src_061_Generics_and_Arrays.java: -------------------------------------------------------------------------------- 1 | Wrapper[] a = (Wrapper[]) Array. 2 | newInstance(Wrapper.class, 10); 3 | Object[] objArray = (Object[]) a; 4 | objArray[0] = new Object();   5 | // <- Will throw a java.lang. 6 | // ArrayStoreExceptionxception 7 | a[0] = new Wrapper("Hello"); 8 | // <- OK. Checked by compiler 9 | -------------------------------------------------------------------------------- /030/src_062_Heap_Pollution.java: -------------------------------------------------------------------------------- 1 | Wrapper nWrapper = new Wrapper(101);    // #1 2 | // Unchecked warning at compile-time and heap pollution 3 | // at runtime 4 | Wrapper sWrapper = nWrapper; // #2 5 | String str = sWrapper.get();         // #3 6 | // ClassCastException 7 | -------------------------------------------------------------------------------- /030/src_063_Heap_Pollution.java: -------------------------------------------------------------------------------- 1 | Wrapper nW = new Wrapper(1L); // #1 2 | // Unchecked cast and unchecked warning occurs when the 3 | // following statement #2 is compiled. Heap pollution 4 | // occurs, when it is executed. 5 | Wrapper sw = (Wrapper) nW; // #2 6 | short s = sw.get();                      // #3 7 | // ClassCastException 8 | -------------------------------------------------------------------------------- /030/src_065_Varargs_Methods_and_Heap_Pollution_Warnings.misc: -------------------------------------------------------------------------------- 1 | warning: [unchecked] Possible heap pollution from 2 | parameterized vararg type Wrapper 3 |        public static void process(Wrapper...nums) { 4 |                                                          ^ 5 | 1 warning 6 | -------------------------------------------------------------------------------- /030/src_066_Varargs_Methods_and_Heap_Pollution_Warnings.java: -------------------------------------------------------------------------------- 1 | Wrapper v1 = new Wrapper<>(10L); 2 | Wrapper v2 = new Wrapper<>(11L); 3 | process(v1, v2); // An unchecked warning 4 | -------------------------------------------------------------------------------- /030/src_067_Varargs_Methods_and_Heap_Pollution_Warnings.misc: -------------------------------------------------------------------------------- 1 | warning: [unchecked] unchecked generic array creation for 2 | varargs parameter of type 3 | Wrapper[] 4 |                 process(v1, v2); 5 |                        ^ 6 | 1 warning 7 | -------------------------------------------------------------------------------- /030/src_069_Varargs_Methods_and_Heap_Pollution_Warnings.misc: -------------------------------------------------------------------------------- 1 | warning: [varargs] Varargs method could cause heap 2 | pollution from non-reifiable varargs 3 | parameter nums 4 |                 Object[] obj = nums; 5 |                                ^ 6 | 1 warning 7 | -------------------------------------------------------------------------------- /030/src_070_Varargs_Methods_and_Heap_Pollution_Warnings.java: -------------------------------------------------------------------------------- 1 | @SuppressWarnings({"unchecked", "varargs"}) 2 | public static void process(Wrapper...nums) { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /030/src_071_Exercises.java: -------------------------------------------------------------------------------- 1 | List list1= new ArrayList<>(); 2 | List list2= new ArrayList<>(); 3 | list1 = list2;  // A compile-time error 4 | -------------------------------------------------------------------------------- /030/src_073_Exercises.java: -------------------------------------------------------------------------------- 1 | List list = new ArrayList<>(); 2 | list.add("Hello"); // A compile-time error 3 | -------------------------------------------------------------------------------- /030/src_075_Exercises.misc: -------------------------------------------------------------------------------- 1 | [1, 2, 3, 4] 2 | [one, two, three, four] 3 | [10, 20, 30, 40.5, 50.9] 4 | -------------------------------------------------------------------------------- /030/src_077_Exercises.java: -------------------------------------------------------------------------------- 1 | public class Test {     2 |    public void test(T t) {   3 |       // More code goes here     4 |    } 5 |    public void test(U u) { 6 |       // More code goes here 7 |    } 8 | } 9 | -------------------------------------------------------------------------------- /040/src_002_What_Is_a_Lambda_Expression_.java: -------------------------------------------------------------------------------- 1 | (String str) -> str.length() 2 | -------------------------------------------------------------------------------- /040/src_003_What_Is_a_Lambda_Expression_.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | interface StringToIntMapper { 3 |     int map(String str); 4 | } 5 | -------------------------------------------------------------------------------- /040/src_004_What_Is_a_Lambda_Expression_.java: -------------------------------------------------------------------------------- 1 | StringToIntMapper mapper = 2 | (String str) -> str.length(); 3 | -------------------------------------------------------------------------------- /040/src_005_What_Is_a_Lambda_Expression_.java: -------------------------------------------------------------------------------- 1 | StringToIntMapper mapper = (String str) -> str.length(); 2 | String name = "Kristy"; 3 | int mappedValue = mapper.map(name); 4 | System.out.println("name=" + name + 5 | ", mapped value=" + mappedValue); 6 | 7 | name=Kristy, mapped value=6 8 | -------------------------------------------------------------------------------- /040/src_007_Why_Do_We_Need_Lambda_Expressions_.java: -------------------------------------------------------------------------------- 1 | // Using an anonymous class 2 | StringToIntMapper mapper = new StringToIntMapper() { 3 |     @Override 4 |     public int map(String str) { 5 |         return str.length(); 6 |     } 7 | }; 8 | // Using a lambda expression 9 | StringToIntMapper mapper = (String str) -> str.length(); 10 | -------------------------------------------------------------------------------- /040/src_008_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | () -> { } 2 | -------------------------------------------------------------------------------- /040/src_009_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (int x, int y) -> { 2 |     return x + y; 3 | } 4 | -------------------------------------------------------------------------------- /040/src_010_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | int sum(int x, int y) { 2 |     return x + y; 3 | } 4 | -------------------------------------------------------------------------------- /040/src_011_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (Object x) -> { 2 |     return x; 3 | } 4 | -------------------------------------------------------------------------------- /040/src_012_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | Object identity(Object x) 2 |     return x; 3 | } 4 | -------------------------------------------------------------------------------- /040/src_013_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (int x, int y) -> { 2 |     if (x > y) 3 |         return x; 4 |     } else { 5 |         return y; 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /040/src_014_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | int getMax(int x, int y) { 2 |     if (x > y) 3 |         return x; 4 |     } else { 5 |         return y; 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /040/src_015_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (String msg) -> { 2 |     System.out.println(msg); 3 | } 4 | -------------------------------------------------------------------------------- /040/src_016_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | void print(String msg) { 2 |     System.out.println(msg); 3 | } 4 | -------------------------------------------------------------------------------- /040/src_017_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | () -> { 2 |     System.out.println(LocalDate.now()); 3 | } 4 | -------------------------------------------------------------------------------- /040/src_018_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | void printCurrentDate() { 2 |     System.out.println(LocalDate.now()); 3 | } 4 | -------------------------------------------------------------------------------- /040/src_019_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | () -> { 2 |     // No code goes here 3 | } 4 | -------------------------------------------------------------------------------- /040/src_020_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | void doNothing() { 2 |     // No code goes here 3 | } 4 | -------------------------------------------------------------------------------- /040/src_021_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Types of parameters are declared 2 | (int x, int y) -> { return x + y; } 3 | // Types of parameters are omitted 4 | (x, y) -> { return x + y; } 5 | -------------------------------------------------------------------------------- /040/src_022_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // A compile-time error 2 | (int x, y) -> { return x + y; } 3 | -------------------------------------------------------------------------------- /040/src_023_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // A compile-time error 2 | (var x, var y) -> { return x + y; } 3 | -------------------------------------------------------------------------------- /040/src_024_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Declares the parameter type 2 | (String msg) -> { System.out.println(msg); } 3 | // Omits the parameter type 4 | (msg) -> { System.out.println(msg); } 5 | // Omits the parameter type and parentheses 6 | msg -> { System.out.println(msg); } 7 | -------------------------------------------------------------------------------- /040/src_025_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Omits parentheses, but not the parameter type, which is not allowed. 2 | String msg -> { System.out.println(msg); } 3 | -------------------------------------------------------------------------------- /040/src_026_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Takes no parameters 2 | () -> { System.out.println("Hello"); } 3 | -------------------------------------------------------------------------------- /040/src_027_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | -> { System.out.println("Hello"); } 2 | -------------------------------------------------------------------------------- /040/src_028_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (final int x, final int y) -> { return x + y; } 2 | (int x, final int y) -> { return x + y; } 3 | -------------------------------------------------------------------------------- /040/src_029_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | (final x, final y) -> { return x + y; } 2 | -------------------------------------------------------------------------------- /040/src_030_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Uses a block statement. Takes two int parameters and 2 | // returns their sum. 3 | (int x, int y) -> { return x + y; } 4 | // Uses an expression. Takes two int parameters and 5 | // returns their sum. 6 | (int x, int y) -> x + y 7 | -------------------------------------------------------------------------------- /040/src_031_Syntax_for_Lambda_Expressions.java: -------------------------------------------------------------------------------- 1 | // Uses a block statement 2 | (String msg) -> { System.out.println(msg); } 3 | // Uses an expression 4 | (String msg) -> System.out.println(msg) 5 | -------------------------------------------------------------------------------- /040/src_032_Target_Typing.java: -------------------------------------------------------------------------------- 1 | (x, y) -> x + y 2 | -------------------------------------------------------------------------------- /040/src_033_Target_Typing.java: -------------------------------------------------------------------------------- 1 | // The type of expression is String 2 | new String("Hello") 3 | // The type of expression is String (a String literal 4 | // is also an expression) 5 | "Hello" 6 | // The type of expression is ArrayList 7 | new ArrayList() 8 | -------------------------------------------------------------------------------- /040/src_034_Target_Typing.java: -------------------------------------------------------------------------------- 1 | // The type of new ArrayList<>() is ArrayList 2 | ArrayList idList = new ArrayList<>(); 3 | // The type of new ArrayList<>() is ArrayList 4 | ArrayList nameList = new ArrayList<>(); 5 | -------------------------------------------------------------------------------- /040/src_035_Target_Typing.java: -------------------------------------------------------------------------------- 1 | T t = ; 2 | -------------------------------------------------------------------------------- /040/src_036_Target_Typing.java: -------------------------------------------------------------------------------- 1 | Adder adder = (x, y) -> x + y; 2 | -------------------------------------------------------------------------------- /040/src_037_Target_Typing.java: -------------------------------------------------------------------------------- 1 | Adder adder = (double x, double y) -> x + y; 2 | -------------------------------------------------------------------------------- /040/src_038_Target_Typing.java: -------------------------------------------------------------------------------- 1 | Adder adder = (var x, var y) -> x + y; 2 | -------------------------------------------------------------------------------- /040/src_039_Target_Typing.java: -------------------------------------------------------------------------------- 1 | Joiner joiner = (x, y) -> x + y; 2 | -------------------------------------------------------------------------------- /040/src_040_Target_Typing.java: -------------------------------------------------------------------------------- 1 | LambdaUtil util = new LambdaUtil(); 2 | util.testAdder((x, y) -> x + y); 3 | -------------------------------------------------------------------------------- /040/src_041_Target_Typing.java: -------------------------------------------------------------------------------- 1 | LambdaUtil2 util = new LambdaUtil2(); 2 | util.test((x, y) -> x + y); // A compile-time error 3 | -------------------------------------------------------------------------------- /040/src_042_Target_Typing.misc: -------------------------------------------------------------------------------- 1 | Reference to test is ambiguous. Both method test(Adder) in 2 | LambdaUtil2 and method test(Joiner) in LambdaUtil2 match. 3 | -------------------------------------------------------------------------------- /040/src_043_Target_Typing.java: -------------------------------------------------------------------------------- 1 | LambdaUtil2 util = new LambdaUtil2(); 2 | util.test((double x, double y) -> x + y); 3 | // <- OK. Will call test(Adder adder) 4 | -------------------------------------------------------------------------------- /040/src_044_Target_Typing.java: -------------------------------------------------------------------------------- 1 | LambdaUtil2 util = new LambdaUtil2();         2 | util.test((Adder)(x, y) -> x + y); 3 | // <- OK. Will call test(Adder adder) 4 | -------------------------------------------------------------------------------- /040/src_045_Target_Typing.java: -------------------------------------------------------------------------------- 1 | LambdaUtil2 util = new LambdaUtil2(); 2 | Adder adder = (x, y) -> x + y; 3 | util.test(adder); 4 | // <- OK. Will call test(Adder adder) 5 | -------------------------------------------------------------------------------- /040/src_046_Target_Typing.java: -------------------------------------------------------------------------------- 1 | ReferenceType variable1 = LambdaExpression; 2 | -------------------------------------------------------------------------------- /040/src_047_Target_Typing.java: -------------------------------------------------------------------------------- 1 | util.testJoiner(LambdaExpression); 2 | -------------------------------------------------------------------------------- /040/src_048_Target_Typing.java: -------------------------------------------------------------------------------- 1 | return LambdaExpression; 2 | -------------------------------------------------------------------------------- /040/src_049_Target_Typing.java: -------------------------------------------------------------------------------- 1 | (Joiner) LambdaExpression; 2 | -------------------------------------------------------------------------------- /040/src_051_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Operations { 3 |     double add(double n1, double n2); 4 |     double mult(double n1, double n2); 5 | } 6 | -------------------------------------------------------------------------------- /040/src_052_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public class Test { 3 |     // Code goes here 4 | } 5 | -------------------------------------------------------------------------------- /040/src_053_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Comparator { 3 |     int compare(T o1, T o2);     4 | } 5 | -------------------------------------------------------------------------------- /040/src_054_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Processor { 3 |      void process(T[] list);     4 | } 5 | -------------------------------------------------------------------------------- /040/src_055_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | Sensitive sen = (x, y) -> x + y; 2 | // <- A compile-time error 3 | -------------------------------------------------------------------------------- /040/src_056_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | Sensitive sen = (Sensitive & Adder) (x, y) -> x + y; 2 | // <- OK 3 | -------------------------------------------------------------------------------- /040/src_057_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | Serializable ser = (Serializable & Adder) (x, y) -> x + y; 2 | -------------------------------------------------------------------------------- /040/src_060_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | // Square the input, add one to the result, and square 2 | // the result 3 | Function chainedFunction = 4 | ((Function)(x -> x * x)) 5 |                         .andThen(x -> x + 1) 6 |                         .andThen(x -> x * x); 7 | System.out.println(chainedFunction.apply(3L)); 8 | 9 | 100 10 | -------------------------------------------------------------------------------- /040/src_062_Functional_Interfaces.java: -------------------------------------------------------------------------------- 1 | FunctionUtil.forEach(list, 2 | p -> System.out.println(p)); 3 | -------------------------------------------------------------------------------- /040/src_063_Method_References.java: -------------------------------------------------------------------------------- 1 | import java.util.function.ToIntFunction; 2 | ... 3 | ToIntFunction lengthFunction = str -> 4 | str.length(); 5 | String name = "Ellen"; 6 | int len = lengthFunction.applyAsInt(name); 7 | System.out.println("Name = " + name + 8 | ", length = " + len); 9 | 10 | Name = Ellen, length = 5 11 | -------------------------------------------------------------------------------- /040/src_064_Method_References.java: -------------------------------------------------------------------------------- 1 | import java.util.function.ToIntFunction; 2 | ... 3 | ToIntFunction lengthFunction = String::length; 4 | String name = "Ellen"; 5 | int len = lengthFunction.applyAsInt(name); 6 | System.out.println("Name = " + name + 7 | ", length = " + len); 8 | Name = Ellen, length = 5 9 | -------------------------------------------------------------------------------- /040/src_065_Method_References.java: -------------------------------------------------------------------------------- 1 | :: 2 | -------------------------------------------------------------------------------- /040/src_066_Method_References.java: -------------------------------------------------------------------------------- 1 | static String toBinaryString(int i) 2 | -------------------------------------------------------------------------------- /040/src_067_Method_References.java: -------------------------------------------------------------------------------- 1 | // Using a lambda expression 2 | Function func1 = 3 | x -> Integer.toBinaryString(x); 4 | System.out.println(func1.apply(17)); 5 | 6 | 10001 7 | -------------------------------------------------------------------------------- /040/src_068_Method_References.java: -------------------------------------------------------------------------------- 1 | // Using a method reference 2 | Function func2 = 3 | Integer::toBinaryString; 4 | System.out.println(func2.apply(17)); 5 | 6 | 10001 7 | -------------------------------------------------------------------------------- /040/src_069_Method_References.java: -------------------------------------------------------------------------------- 1 | static int sum(int a, int b) 2 | -------------------------------------------------------------------------------- /040/src_071_Method_References.java: -------------------------------------------------------------------------------- 1 | // Uses a lambda expression 2 | BiFunction func1 = 3 | (x, y) -> Integer.sum(x, y); 4 | System.out.println(func1.apply(17, 15)); 5 | // Uses a method reference 6 | BiFunction func2 = 7 | Integer::sum; 8 | System.out.println(func2.apply(17, 15)); 9 | 10 | 32 11 | 32 12 | -------------------------------------------------------------------------------- /040/src_073_Method_References.java: -------------------------------------------------------------------------------- 1 | static List getPersons() 2 | -------------------------------------------------------------------------------- /040/src_074_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier> supplier = Person::getPersons; 2 | List personList = supplier.get(); 3 | FunctionUtil.forEach(personList, 4 | p -> System.out.println(p)); 5 | 6 | John Jacobs, MALE, 1975-01-20 7 | Wally Inman, MALE, 1965-09-12 8 | Donna Jacobs, FEMALE, 1970-09-12 9 | -------------------------------------------------------------------------------- /040/src_075_Method_References.java: -------------------------------------------------------------------------------- 1 | String name = "Kannan"; 2 | // name is the receiver of the length() method 3 | int len1 = name.length(); 4 | // "Hello" is the receiver of the length() method 5 | int len2 = "Hello".length(); 6 | // (new String("Kannan")) is the receiver of the length() 7 | // method 8 | int len3 = (new String("Kannan")).length(); 9 | -------------------------------------------------------------------------------- /040/src_076_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier supplier = () -> "Ellen".length(); 2 | System.out.println(supplier.get()); 3 | 4 | 5 5 | -------------------------------------------------------------------------------- /040/src_077_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier supplier = "Ellen"::length; 2 | System.out.println(supplier.get()); 3 | 4 | 5 5 | -------------------------------------------------------------------------------- /040/src_078_Method_References.java: -------------------------------------------------------------------------------- 1 | Consumer consumer = str -> System.out.println(str); 2 | consumer.accept("Hello"); 3 | 4 | Hello 5 | -------------------------------------------------------------------------------- /040/src_079_Method_References.java: -------------------------------------------------------------------------------- 1 | Consumer consumer = System.out::println; 2 | consumer.accept("Hello"); 3 | 4 | Hello 5 | -------------------------------------------------------------------------------- /040/src_080_Method_References.java: -------------------------------------------------------------------------------- 1 | List list = Person.getPersons(); 2 | FunctionUtil.forEach(list, System.out::println); 3 | 4 | John Jacobs, MALE, 1975-01-20 5 | Wally Inman, MALE, 1965-09-12 6 | Donna Jacobs, FEMALE, 1970-09-12 7 | -------------------------------------------------------------------------------- /040/src_081_Method_References.java: -------------------------------------------------------------------------------- 1 | Function fNameFunc = 2 | (Person p) -> p.getFirstName(); 3 | -------------------------------------------------------------------------------- /040/src_082_Method_References.java: -------------------------------------------------------------------------------- 1 | Function fNameFunc = Person::getFirstName; 2 | -------------------------------------------------------------------------------- /040/src_083_Method_References.java: -------------------------------------------------------------------------------- 1 | Function strLengthFunc = String::length; 2 | String name = "Ellen"; 3 | // name is the receiver of String::length 4 | int len = strLengthFunc.apply(name); 5 | System.out.println("name = " + name + 6 | ", length = " + len); 7 | name = Ellen, length = 5 8 | -------------------------------------------------------------------------------- /040/src_084_Method_References.java: -------------------------------------------------------------------------------- 1 | String concat(String str) 2 | -------------------------------------------------------------------------------- /040/src_086_Method_References.java: -------------------------------------------------------------------------------- 1 | List personList = Person.getPersons(); 2 | // Maps each Person object to its first name 3 | List firstNameList = FunctionUtil.map(personList, 4 | Person::getFirstName); 5 | // Prints the first name list 6 | FunctionUtil.forEach(firstNameList, System.out::println); 7 | 8 | John 9 | Wally 10 | Donna 11 | -------------------------------------------------------------------------------- /040/src_087_Method_References.java: -------------------------------------------------------------------------------- 1 | TypeName.super::instanceMethod 2 | -------------------------------------------------------------------------------- /040/src_088_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier func1 = () -> new String(); 2 | Function func2 = str -> new String(str); 3 | -------------------------------------------------------------------------------- /040/src_089_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier func1 = String::new; 2 | Function func2 = String::new; 3 | -------------------------------------------------------------------------------- /040/src_092_Method_References.java: -------------------------------------------------------------------------------- 1 | Supplier func1 = Item::new; 2 | -------------------------------------------------------------------------------- /040/src_093_Method_References.java: -------------------------------------------------------------------------------- 1 | Function func2 = Item::new; 2 | -------------------------------------------------------------------------------- /040/src_094_Method_References.java: -------------------------------------------------------------------------------- 1 | BiFunction func3 = Item::new; 2 | -------------------------------------------------------------------------------- /040/src_095_Method_References.java: -------------------------------------------------------------------------------- 1 | Function func4 = Item::new; 2 | // <- A compile-time error 3 | -------------------------------------------------------------------------------- /040/src_097_Method_References.java: -------------------------------------------------------------------------------- 1 | // Uses an array constructor reference 2 | Function arrayCreator3 = int[]::new;         3 | int[] empIds3 = arrayCreator3.apply(5); 4 | // <- Creates an int array of five elements 5 | -------------------------------------------------------------------------------- /040/src_098_Method_References.java: -------------------------------------------------------------------------------- 1 | // Uses an array constructor reference 2 | IntFunction TwoDimArrayCreator = int[][]::new;         3 | int[][] matrix = TwoDimArrayCreator.apply(5); 4 | // <- Creates an int[5][] array 5 | -------------------------------------------------------------------------------- /040/src_099_Method_References.java: -------------------------------------------------------------------------------- 1 | BiFunction arrayCreator = 2 | int[][]::new; 3 | -------------------------------------------------------------------------------- /040/src_100_Method_References.java: -------------------------------------------------------------------------------- 1 | static List asList(T... a) 2 | -------------------------------------------------------------------------------- /040/src_103_Lexical_Scoping.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 |     public static void main(String[] args) { 3 |         String msg = "Hello"; 4 |         Printer printer = msg1 -> { 5 |             String msg = "Hi"; // A compile-time error 6 |             System.out.println(msg1); 7 |         };         8 |     }     9 | } 10 | -------------------------------------------------------------------------------- /040/src_104_Variable_Capture.java: -------------------------------------------------------------------------------- 1 | public Printer test() { 2 |     final String msg = "Hello"; // msg is effectively final 3 |     Printer printer = msg1 -> System.out.println(msg + 4 | " " + msg1);         5 |     return printer; 6 | } 7 | -------------------------------------------------------------------------------- /040/src_105_Variable_Capture.java: -------------------------------------------------------------------------------- 1 | public Printer test() { 2 |     String msg = "Hello"; // msg is effectively final 3 |     Printer printer = msg1 -> 4 | System.out.println(msg + " " + msg1);         5 |     return printer; 6 | } 7 | -------------------------------------------------------------------------------- /040/src_106_Variable_Capture.java: -------------------------------------------------------------------------------- 1 | public Printer test() {         2 |     String msg; 3 |     msg = "Hello"; // msg is effectively final 4 |     Printer printer = msg1 -> 5 | System.out.println(msg + " " + msg1);         6 |     return printer; 7 | } 8 | -------------------------------------------------------------------------------- /040/src_108_Variable_Capture.java: -------------------------------------------------------------------------------- 1 | public Printer test() { 2 |     // A compile-time error. The msg variable is not 3 | // declared yet. 4 |     Printer printer = msg1 -> 5 | System.out.println(msg + " " + msg1); 6 |     String msg = "Hello";  // msg is effectively final 7 |     return printer; 8 | } 9 | -------------------------------------------------------------------------------- /040/src_109_Variable_Capture.java: -------------------------------------------------------------------------------- 1 | public Printer test() {     2 |     String msg = "Hello"; 3 |     Printer printer = msg1 ->  { 4 |         msg = "Hi " + msg1; // A compile-time error. 5 | // Attempting to modify msg. 6 |         System.out.println(msg); 7 |     }; 8 |     return printer; 9 | } 10 | -------------------------------------------------------------------------------- /040/src_110_Jumps_and_Exits.java: -------------------------------------------------------------------------------- 1 | public void test() { 2 |     for(int i = 0; i < 5; i++) { 3 |         Consumer evenIdPrinter = id -> { 4 |             if (id < 0) { 5 |                 // A compile-time error. Attempting to 6 | // break out of the lambda body 7 |                 break; 8 |             } 9 |         }; 10 |     } 11 | } 12 | -------------------------------------------------------------------------------- /040/src_112_Comparing_Objects.java: -------------------------------------------------------------------------------- 1 | package java.util; 2 | @FunctionalInterface 3 | public interface Comparator { 4 |     int compare(T o1, T o2); 5 |     /* Other methods are not shown. */ 6 | } 7 | -------------------------------------------------------------------------------- /040/src_113_Comparing_Objects.java: -------------------------------------------------------------------------------- 1 | Comparator firstNameComp = 2 | Comparator.comparing(Person::getFirstName); 3 | -------------------------------------------------------------------------------- /040/src_114_Comparing_Objects.java: -------------------------------------------------------------------------------- 1 | Comparator lastFirstDobComp = 2 |     Comparator.comparing(Person::getLastName) 3 |               .thenComparing(Person::getFirstName) 4 |               .thenComparing(Person::getDob); 5 | -------------------------------------------------------------------------------- /040/src_115_Exercises.java: -------------------------------------------------------------------------------- 1 | a. (int x, int y) -> { return x + y; } 2 | b. (int x, int y) -> x + y 3 | -------------------------------------------------------------------------------- /040/src_116_Exercises.java: -------------------------------------------------------------------------------- 1 | a. (int x, int y) -> x + y \\ 2 | b. (x, y) -> x + y \\ 3 | c. (String msg) -> { System.out.println(msg); }\\ 4 | d. () -> {} 5 | -------------------------------------------------------------------------------- /040/src_117_Exercises.java: -------------------------------------------------------------------------------- 1 | x -> x; 2 | -------------------------------------------------------------------------------- /040/src_118_Exercises.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Operations { 3 |     int factorial(int n); 4 |     int abs(int n); 5 | } 6 | -------------------------------------------------------------------------------- /040/src_119_Exercises.java: -------------------------------------------------------------------------------- 1 | Object obj = x -> x + 1; 2 | -------------------------------------------------------------------------------- /040/src_120_Exercises.java: -------------------------------------------------------------------------------- 1 | Function f = x -> x + 1; 2 | Object obj = f; 3 | -------------------------------------------------------------------------------- /040/src_122_Exercises.java: -------------------------------------------------------------------------------- 1 | public static void test() { 2 |     int n = 100; 3 |     Function f = n -> n + 1; 4 |     System.out.println(f.apply(100)); 5 | } 6 | -------------------------------------------------------------------------------- /040/src_124_Exercises.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface FeelingLucky { 3 |     void gamble(); 4 |     public static void hitJackpot() { 5 |         System.out.println("You have won 80M dollars."); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /040/src_125_Exercises.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Mystery { 3 |     @Override 4 |     String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /050/src_001_What_Is_a_Thread_.misc: -------------------------------------------------------------------------------- 1 | Instruction-1 2 | Instruction-2 3 | Instruction-3 4 | Instruction-4 5 | Instruction-5 6 | Instruction-6 7 | -------------------------------------------------------------------------------- /050/src_002_Creating_Threads_in_Java.java: -------------------------------------------------------------------------------- 1 | // Creates a thread object 2 | Thread simplestThread = new Thread(); 3 | -------------------------------------------------------------------------------- /050/src_003_Creating_Threads_in_Java.java: -------------------------------------------------------------------------------- 1 | // Starts the thread 2 | simplestThread.start(); 3 | -------------------------------------------------------------------------------- /050/src_004_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | public class MyThreadClass extends Thread { 2 |     @Override 3 |     public void run() { 4 |         System.out.println("Hello Java threads!"); 5 |     } 6 |     // More code goes here 7 | } 8 | -------------------------------------------------------------------------------- /050/src_005_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | MyThreadClass myThread = new MyThreadClass(); 2 | myThread.start(); 3 | -------------------------------------------------------------------------------- /050/src_006_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface Runnable { 3 |     void run(); 4 | } 5 | -------------------------------------------------------------------------------- /050/src_007_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | public class HelloRunnable implements Runnable { 2 | @Override 3 |     public void run() { 4 | System.out.println("Hello Java threads!"); 5 | } 6 | } 7 | 8 | // Creating an instance: 9 | Runnable aRunnableObject = new HelloRunnable(); 10 | -------------------------------------------------------------------------------- /050/src_008_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | Runnable aRunnableObject = () -> 2 | System.out.println("Hello Java threads!"); 3 | -------------------------------------------------------------------------------- /050/src_009_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | Thread myThread = new Thread(aRunnableObject); 2 | -------------------------------------------------------------------------------- /050/src_010_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | myThread.start(); 2 | -------------------------------------------------------------------------------- /050/src_011_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | public class ThreadTest { 2 |     public static void execute() { 3 |         System.out.println("Hello Java threads!"); 4 |     } 5 | } 6 | -------------------------------------------------------------------------------- /050/src_012_Specifying_Your_Code_for_a_Thread.java: -------------------------------------------------------------------------------- 1 | Thread myThread = new Thread(ThreadTest::execute); 2 | myThread.start(); 3 | -------------------------------------------------------------------------------- /050/src_013_Issues_in_Using_Multiple_Threads.java: -------------------------------------------------------------------------------- 1 | register-1 = balance; 2 | register-1 = register-1 + 10; 3 | balance = register-1; 4 | -------------------------------------------------------------------------------- /050/src_014_Issues_in_Using_Multiple_Threads.java: -------------------------------------------------------------------------------- 1 | register-2 = balance; 2 | register-2 = register-2 - 10; 3 | balance = register-2; 4 | -------------------------------------------------------------------------------- /050/src_015_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | public class CriticalSection { 2 |     public synchronized void someMethod_1() { 3 |         // Method code goes here   4 |     } 5 |     public static synchronized void someMethod_2() { 6 |         // Method code goes here   7 |     } 8 | } 9 | -------------------------------------------------------------------------------- /050/src_016_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | // Create an object called cs1 2 | CriticalSection cs1 = new CriticalSection(); 3 | // Execute the synchronized instance method. Before this 4 | // method execution starts, the thread that is executing 5 | // this statement must acquire the monitor lock of the cs1 6 | // object 7 | cs1.someMethod_1(); 8 | -------------------------------------------------------------------------------- /050/src_017_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | // Execute the synchronized static method. Before this 2 | // method execution starts, the thread that is executing 3 | // this statement must acquire the monitor lock of the 4 | // CriticalSection.class object   5 | CriticalSection.someMethod_2(); 6 | -------------------------------------------------------------------------------- /050/src_018_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | synchronized() { 2 |     // one or more statements of the critical section   3 | } 4 | -------------------------------------------------------------------------------- /050/src_021_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | public class MultiLocks { 2 |     public synchronized void method1() { 3 |         // Some statements go here   4 |         this.method2();     5 |         MultiLocks.method3(); 6 |         // Some statements go here   7 |     } 8 |     // Rest of the code remains the same as shown before   9 | } 10 | -------------------------------------------------------------------------------- /050/src_022_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | MultiLocks ml = new MultiLocks(); 2 | ml.method1(); 3 | -------------------------------------------------------------------------------- /050/src_023_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | synchronized (objectReference) {     2 |     // Trying to synchronize again on the same object is ok   3 |     synchronized(objectReference) { 4 |         // Some statements go here   5 |     } 6 | } 7 | -------------------------------------------------------------------------------- /050/src_027_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | if (buffer is empty) { 2 |     buffer.wait(); 3 | } 4 | buffer.consume(); 5 | -------------------------------------------------------------------------------- /050/src_028_Object_s_Monitor_and_Threads_Synchronization.java: -------------------------------------------------------------------------------- 1 | while (buffer is empty) { 2 |     buffer.wait(); 3 | } 4 | buffer.consume(); 5 | -------------------------------------------------------------------------------- /050/src_029_The_Producer_Consumer_Synchronization_Problem.java: -------------------------------------------------------------------------------- 1 | Produced: XXX 2 | Consumed: XXX 3 | -------------------------------------------------------------------------------- /050/src_030_Which_Thread_Is_Executing_.java: -------------------------------------------------------------------------------- 1 | Thread t = Thread.currentThread(); 2 | -------------------------------------------------------------------------------- /050/src_031_I_Will_Join_You_in_Heaven.java: -------------------------------------------------------------------------------- 1 | Thread t1 =  new Thread(JoinWrong::print); 2 | t1.start(); 3 | System.out.println("We are done."); 4 | -------------------------------------------------------------------------------- /050/src_032_I_Will_Join_You_in_Heaven.java: -------------------------------------------------------------------------------- 1 | t1.join(); // Join t1   2 | t2.join(); // Join t2   3 | t3.join(); // Join t3   4 | -------------------------------------------------------------------------------- /050/src_033_I_Will_Join_You_in_Heaven.java: -------------------------------------------------------------------------------- 1 | // "Bad" call (not if you know what you are doing) to 2 | // join. It waits forever until another thread interrupts 3 | // it.   4 | Thread.currentThread().join(); 5 | -------------------------------------------------------------------------------- /050/src_034_Be_Considerate_to_Others_and_Yield.java: -------------------------------------------------------------------------------- 1 | // The run() method of a thread class   2 | public void run() { 3 |     while(true) { 4 |         // do some processing here...   5 |         Thread.yield(); // Let's yield to other threads   6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /050/src_035_Lifecycle_of_a_Thread.java: -------------------------------------------------------------------------------- 1 | Thread t = new SomeThreadClass(); 2 | // <- t is in the new state 3 | -------------------------------------------------------------------------------- /050/src_036_Threads_Work_in_a_Group.java: -------------------------------------------------------------------------------- 1 | // Create a new ThreadGroup   2 | ThreadGroup myGroup = new ThreadGroup("My Thread Group"); 3 | // Make the new thread a member of the myGroup thread group 4 | Thread t = new Thread(myGroup, "myThreadName"); 5 | -------------------------------------------------------------------------------- /050/src_037_Volatile_Variables.java: -------------------------------------------------------------------------------- 1 | volatile boolean flag = true; 2 | -------------------------------------------------------------------------------- /050/src_038_Stopping__Suspending__and_Resuming_Threads.java: -------------------------------------------------------------------------------- 1 | synchronized (this) { 2 |     while (suspended) { 3 |         System.out.println("Suspended..."); 4 |         this.wait(); 5 |         System.out.println("Resumed..."); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /050/src_039_Stopping__Suspending__and_Resuming_Threads.java: -------------------------------------------------------------------------------- 1 | while (keepRunning) { 2 |     try { 3 |         someBlockingMethodCall(); 4 |     } catch (InterruptedException e) { 5 |         e.printStackTrace(); 6 |     } 7 | } 8 | -------------------------------------------------------------------------------- /050/src_040_Stopping__Suspending__and_Resuming_Threads.java: -------------------------------------------------------------------------------- 1 | public void stopThread() { 2 |     // interrupt this thread   3 |     this.interrupt(); 4 | } 5 | -------------------------------------------------------------------------------- /050/src_041_Stopping__Suspending__and_Resuming_Threads.java: -------------------------------------------------------------------------------- 1 | public void run() { 2 |     while (Thread.currentThread().isInterrupted())) { 3 |         try { 4 |             // Do the processing   5 |         } catch (InterruptedException e) { 6 |             // Stop the thread by exiting the loop   7 |             break; 8 |         } 9 |     } 10 | } 11 | -------------------------------------------------------------------------------- /050/src_042_Spin_Wait_Hints.java: -------------------------------------------------------------------------------- 1 | ... 2 | @Override 3 | public void run() { 4 |     // Wait in a loop until data is ready 5 |     while (!dataReady) { 6 |         // No code 7 |     } 8 |     processData(); 9 | } 10 | private void processData() { 11 |     // Data processing logic goes here 12 | } 13 | -------------------------------------------------------------------------------- /050/src_043_Atomic_Variables.java: -------------------------------------------------------------------------------- 1 | CAS(M, V, N) { 2 |     currentValueAtM = get the value at Location M; 3 |     if (currentValueAtM == V) { 4 |         set value at M to N; 5 |     } 6 |     return currentValueAtM; 7 | } 8 | -------------------------------------------------------------------------------- /050/src_044_Atomic_Variables.java: -------------------------------------------------------------------------------- 1 | AtomicLong aCounter = new AtomicLong(0L); 2 | -------------------------------------------------------------------------------- /050/src_045_Atomic_Variables.java: -------------------------------------------------------------------------------- 1 | compareAndSet(M, V, N) { 2 |     // Call CAS (see CAS pseudocode) if CAS succeeded, 3 | // return true; otherwise, return false. 4 |     return (CAS(M, V, N) == V) 5 | } 6 | -------------------------------------------------------------------------------- /050/src_046_Explicit_Locks.java: -------------------------------------------------------------------------------- 1 | public void updateResource() { 2 |     // Acquire the lock and the lock will be released 3 | // automatically by the JVM when your code exits the 4 | // block   5 |     synchronized (this) { 6 |         // Logic for updating/reading the shared 7 | // resource goes here 8 |     } 9 | } 10 | -------------------------------------------------------------------------------- /050/src_047_Explicit_Locks.java: -------------------------------------------------------------------------------- 1 | Lock nonFairLock1 = new ReentrantLock();        2 | // <- A non-fair lock (Default is non-fair) 3 | Lock nonFairLock2 = new ReentrantLock(false);   4 | // <- A non-fair lock   5 | Lock fairLock2 = new ReentrantLock(true);       6 | // <- A fair lock   7 | -------------------------------------------------------------------------------- /050/src_048_Explicit_Locks.java: -------------------------------------------------------------------------------- 1 | public interface ReadWriteLock { 2 |     Lock readLock(); 3 |     Lock writeLock(); 4 | } 5 | -------------------------------------------------------------------------------- /050/src_049_Synchronizers.java: -------------------------------------------------------------------------------- 1 | final int MAX_PERMITS = 3; 2 | Semaphore s = new Semaphores(MAX_PERMITS); 3 | -------------------------------------------------------------------------------- /050/src_050_Synchronizers.java: -------------------------------------------------------------------------------- 1 | final int MAX_PERMITS = 3; 2 | Semaphore s = new Semaphores(MAX_PERMITS, true); 3 | // <- A fair semaphore 4 | -------------------------------------------------------------------------------- /050/src_051_Synchronizers.java: -------------------------------------------------------------------------------- 1 | CyclicBarrier barrier = new CyclicBarrier(5); 2 | // <- 5 threads 3 | -------------------------------------------------------------------------------- /050/src_052_Synchronizers.java: -------------------------------------------------------------------------------- 1 | // Assuming a BarrierAction class implements the 2 | // Runnable interface 3 | Runnable barrierAction = new BarrierAction(); 4 | CyclicBarrier barrier = new CyclicBarrier( 5 | 5, barrierAction); 6 | -------------------------------------------------------------------------------- /050/src_053_Synchronizers.java: -------------------------------------------------------------------------------- 1 | // A phaser with no registered parties 2 | Phaser phaser = new Phaser(); 3 | -------------------------------------------------------------------------------- /050/src_054_Synchronizers.java: -------------------------------------------------------------------------------- 1 | // A phaser with 5 registered parties 2 | Phaser phaser = new Phaser(5); 3 | -------------------------------------------------------------------------------- /050/src_055_Synchronizers.java: -------------------------------------------------------------------------------- 1 | // Start with 1 registered party   2 | Phaser phaser = new Phaser(1); 3 | -------------------------------------------------------------------------------- /050/src_057_Synchronizers.java: -------------------------------------------------------------------------------- 1 | Exchanger exchanger = new Exchanger<>(); 2 | -------------------------------------------------------------------------------- /050/src_058_Synchronizers.java: -------------------------------------------------------------------------------- 1 | objectReceived = exchanger.exchange(objectedPassed); 2 | -------------------------------------------------------------------------------- /050/src_059_Synchronizers.java: -------------------------------------------------------------------------------- 1 | ArrayList buffer = new ArrayList(); 2 | -------------------------------------------------------------------------------- /050/src_060_Synchronizers.java: -------------------------------------------------------------------------------- 1 | Exchanger> exchanger = 2 | new Exchanger>(); 3 | -------------------------------------------------------------------------------- /050/src_061_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | public class MyTask implements Runnable { 2 |     public void run() { 3 |         // Task processing logic goes here   4 |     } 5 | } 6 | -------------------------------------------------------------------------------- /050/src_062_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | MyTask task1 = new MyTask(); 2 | MyTask task2 = new MyTask(); 3 | MyTask task3 = new MyTask(); 4 | -------------------------------------------------------------------------------- /050/src_063_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | Thread t1 = new Thread(task1); 2 | Thread t2 = new Thread(task2); 3 | Thread t3 = new Thread(task3); 4 | t1.start(); 5 | t2.start(); 6 | t3.start(); 7 | -------------------------------------------------------------------------------- /050/src_064_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | public interface Executor { 2 |     void execute (Runnable command); 3 | } 4 | -------------------------------------------------------------------------------- /050/src_065_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | // Get an executor instance. 2 | Executor executor = Executors.newCachedThreadPool(); 3 | // Submit three tasks to the executor   4 | executor.execute(task1); 5 | executor.execute(task2); 6 | executor.execute(task3); 7 | -------------------------------------------------------------------------------- /050/src_066_The_Executor_Framework.misc: -------------------------------------------------------------------------------- 1 | Task #1 - Iteration #1 is going to sleep for 7 seconds. 2 | Task #2 - Iteration #1 is going to sleep for 10 seconds. 3 | Task #3 - Iteration #1 is going to sleep for 9 seconds. 4 | Task #2 has been interrupted. 5 | Task #3 has been interrupted. 6 | Task #1 has been interrupted. 7 | -------------------------------------------------------------------------------- /050/src_067_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | public interface Callable { 2 |     V call() throws Exception; 3 | } 4 | -------------------------------------------------------------------------------- /050/src_068_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | Future submittedTask = exec.submit(task); 2 | -------------------------------------------------------------------------------- /050/src_069_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | Integer result = submittedTask.get(); 2 | -------------------------------------------------------------------------------- /050/src_070_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | // Get scheduled executor service with 3 threads   2 | ScheduledExecutorService sexec = 3 | Executors.newScheduledThreadPool(3); 4 | -------------------------------------------------------------------------------- /050/src_071_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | sexec.schedule(task1, 10, TimeUnit.SECONDS); 2 | -------------------------------------------------------------------------------- /050/src_072_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | sexec.scheduleAtFixedRate(task2, 10, 25, 2 | TimeUnit.SECONDS); 3 | -------------------------------------------------------------------------------- /050/src_073_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | sexec.scheduleWithFixedDelay(task3, 40, 60, 2 | TimeUnit.SECONDS); 3 | -------------------------------------------------------------------------------- /050/src_075_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | ExecutorService exec = Executors. 2 | newScheduledThreadPool(3); 3 | -------------------------------------------------------------------------------- /050/src_076_The_Executor_Framework.java: -------------------------------------------------------------------------------- 1 | ExecutorCompletionService CompletionService = 2 | new ExecutorCompletionService(exec); 3 | -------------------------------------------------------------------------------- /050/src_077_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | if (Task is small) { 2 |     Solve the task directly. 3 | } else { 4 |     Divide the task into subtasks. 5 |     Launch the subtasks asynchronously (the fork stage). 6 |     Wait for the subtasks to finish (the join stage). 7 |     Combine the results of all subtasks. 8 | } 9 | -------------------------------------------------------------------------------- /050/src_078_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | public class MyTask extends RecursiveTask { 2 |     // Code for your task goes here   3 | } 4 | -------------------------------------------------------------------------------- /050/src_079_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | public class MyTask extends RecursiveTask { 2 |     public Long compute() { 3 |         // Logic for the task goes here   4 |     } 5 | } 6 | -------------------------------------------------------------------------------- /050/src_080_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | ForkJoinPool pool = new ForkJoinPool(); 2 | -------------------------------------------------------------------------------- /050/src_081_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | long result = pool.invoke(task); 2 | -------------------------------------------------------------------------------- /050/src_082_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | List> forks = new ArrayList<>(); 2 | for(int i = 0; i < this.count; i++) { 3 |     RandomIntSum subTask = new RandomIntSum(1); 4 |     subTask.fork(); // Launch the subtask 5 |     // Keep the subTask references to combine the 6 | // results at the end   7 |     forks.add(subTask); 8 | } 9 | -------------------------------------------------------------------------------- /050/src_083_The_Fork_Join_Framework.java: -------------------------------------------------------------------------------- 1 | for(RecursiveTask subTask : forks) { 2 |     result = result + subTask.join(); 3 | } 4 | -------------------------------------------------------------------------------- /050/src_085_Thread_Local_Variables.java: -------------------------------------------------------------------------------- 1 | // Create a ThreadLocal with an initial value of 1000 2 | ThreadLocal threadLocal = T 3 | hreadLocal.withInitial(() -> 1000); 4 | -------------------------------------------------------------------------------- /050/src_086_Thread_Local_Variables.java: -------------------------------------------------------------------------------- 1 | // Return the second part of the current time as the 2 | // initial value 3 | ThreadLocal threadLocal = 4 | ThreadLocal.withInitial(() -> 5 | LocalTime.now().getSecond() 6 | ); 7 | -------------------------------------------------------------------------------- /050/src_087_Setting_Stack_Size_of_a_Thread.sh: -------------------------------------------------------------------------------- 1 | java –Xss512k 2 | -------------------------------------------------------------------------------- /050/src_088_Exercises.java: -------------------------------------------------------------------------------- 1 | Thread t = new Thread(); 2 | -------------------------------------------------------------------------------- /060/src_001_What_Are_Streams_.java: -------------------------------------------------------------------------------- 1 | List numbers = List.of(1, 2, 3, 4, 5); 2 | int sum = 0; 3 | for (int n : numbers) { 4 |     if (n % 2 == 1) { 5 |         int square = n * n; 6 |         sum = sum + square; 7 |     } 8 | } 9 | -------------------------------------------------------------------------------- /060/src_002_What_Are_Streams_.java: -------------------------------------------------------------------------------- 1 | int sum = numbers.stream() 2 |                  .filter(n -> n % 2 == 1) 3 |                  .map(n -> n * n) 4 |                  .reduce(0, Integer::sum); 5 | -------------------------------------------------------------------------------- /060/src_003_What_Are_Streams_.java: -------------------------------------------------------------------------------- 1 | int sum = numbers.parallelStream() 2 |                  .filter(n -> n % 2 == 1) 3 |                  .map(n -> n * n) 4 |                  .reduce(0, Integer::sum); 5 | -------------------------------------------------------------------------------- /060/src_005_What_Are_Streams_.java: -------------------------------------------------------------------------------- 1 | List numbers = List.of(1, 2, 3, 4, 5); 2 | int sum = numbers.stream() 3 |                  .filter(n -> n % 2 == 1) 4 |                  .map(n -> n * n) 5 |                  .reduce(0, Integer::sum); 6 | -------------------------------------------------------------------------------- /060/src_006_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Get a list of integers from 1 to 5 2 | List numbersList = List.of(1, 2, 3, 4, 5); 3 | // Get a stream from the list 4 | Stream numbersStream = numbersList.stream(); 5 | -------------------------------------------------------------------------------- /060/src_007_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Get a stream of odd integers 2 | Stream oddNumbersStream = 3 | numbersStream.filter(n -> n % 2 == 1); 4 | -------------------------------------------------------------------------------- /060/src_008_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Get a stream of the squares of odd integers 2 | Stream squaredNumbersStream = 3 | oddNumbersStream.map(n -> n * n); 4 | -------------------------------------------------------------------------------- /060/src_009_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Sum all integers in the stream 2 | int sum = squaredNumbersStream. 3 | reduce(0, (n1, n2) -> n1 + n2); 4 | -------------------------------------------------------------------------------- /060/src_010_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Sum all integers in the stream 2 | int sum = squaredNumbersStream. 3 | reduce(0, Integer::sum); 4 | -------------------------------------------------------------------------------- /060/src_011_A_Quick_Example.java: -------------------------------------------------------------------------------- 1 | // Sum the squares of all odd integers in the numbers list 2 | int sum = numbersList.stream() 3 |                      .filter(n -> n % 2 == 1) 4 |                      .map(n -> n * n) 5 |                      .reduce(0, Integer::sum); 6 | -------------------------------------------------------------------------------- /060/src_012_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates a stream with one string element 2 | Stream stream = Stream.of("Hello"); 3 | // Creates a stream with four string elements 4 | Stream stream = Stream.of( 5 | "Ken", "Jeff", "Chris", "Ellen"); 6 | -------------------------------------------------------------------------------- /060/src_013_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | String str = "Hello"; 2 | // Stream s1 will have one element "Hello" 3 | Stream s1 = Stream.ofNullable(str); 4 | str = null; 5 | // Stream s2 is an empty stream because str is null 6 | Stream s2 = Stream.ofNullable(str); 7 | -------------------------------------------------------------------------------- /060/src_015_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | String[] names  = {"Ken", "Jeff", "Chris", "Ellen"}; 2 | // Creates a stream of four strings in the names array 3 | Stream stream = Stream.of(names); 4 | -------------------------------------------------------------------------------- /060/src_016_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | String str  = "Ken,Jeff,Chris,Ellen"; 2 | // The stream will contain 4 elements: 3 | // "Ken", "Jeff", "Chris", and "Ellen" 4 | Stream stream = Stream.of(str.split(",")); 5 | -------------------------------------------------------------------------------- /060/src_017_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Gets a stream builder 2 | Stream.Builder builder = Stream.builder(); 3 | -------------------------------------------------------------------------------- /060/src_018_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream stream = Stream.builder() 2 |     .add("Ken") 3 |     .add("Jeff") 4 |     .add("Chris") 5 |     .add("Ellen") 6 | .build(); 7 | -------------------------------------------------------------------------------- /060/src_019_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Obtain a builder 2 | Stream.Builder builder = Stream.builder(); 3 | // Add elements and build the stream 4 | Stream stream = builder.add("Ken") 5 | .add("Jeff") 6 |     .add("Chris") 7 |     .add("Ellen") 8 | .build(); 9 | -------------------------------------------------------------------------------- /060/src_020_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Create an IntStream containing 1, 2, 3, 4, and 5 2 | IntStream oneToFive = IntStream.range(1, 6); 3 | // Create an IntStream containing 1, 2, 3, 4, and 5 4 | IntStream oneToFive = IntStream.rangeClosed(1, 5); 5 | -------------------------------------------------------------------------------- /060/src_021_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates an empty stream of strings 2 | Stream stream = Stream.empty(); 3 | -------------------------------------------------------------------------------- /060/src_022_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates an empty stream of integers 2 | IntStream numbers = IntStream.empty(); 3 | -------------------------------------------------------------------------------- /060/src_023_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | static Stream iterate( 2 | T seed, 3 | Predicate hasNext, 4 | UnaryOperator next) 5 | -------------------------------------------------------------------------------- /060/src_024_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | for (int index = seed; 2 | hasNext.test(index); 3 | index = next.applyAsInt(index)) { 4 |     // index is the next element in the stream 5 | } 6 | -------------------------------------------------------------------------------- /060/src_025_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream nums = 2 | Stream.iterate(1, n -> n <= 10, n -> n + 1); 3 | -------------------------------------------------------------------------------- /060/src_026_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | static Stream iterate(T seed, UnaryOperator f) 2 | -------------------------------------------------------------------------------- /060/src_027_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates a stream of natural numbers 2 | Stream naturalNumbers = 3 | Stream.iterate(1L, n -> n + 1); 4 | // Creates a stream of odd natural numbers 5 | Stream oddNaturalNumbers = 6 | Stream.iterate(1L, n -> n + 2); 7 | -------------------------------------------------------------------------------- /060/src_028_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates a stream of the first 10 natural numbers 2 | Stream tenNaturalNumbers = 3 | Stream.iterate(1L, n -> n + 1). 4 | limit(10); 5 | -------------------------------------------------------------------------------- /060/src_029_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.iterate(1L, n -> n + 2) 2 |       .limit(5) 3 |       .forEach(System.out::println); 4 | 5 | 1 6 | 3 7 | 5 8 | 7 9 | 9 10 | -------------------------------------------------------------------------------- /060/src_030_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.iterate(2L, PrimeUtil::next) 2 |       .limit(5) 3 |       .forEach(System.out::println); 4 | 5 | 2 6 | 3 7 | 5 8 | 7 9 | 11 10 | -------------------------------------------------------------------------------- /060/src_031_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Print the first 5 prime numbers 2 | Stream.iterate(2L, n -> n + 1) 3 |       .filter(PrimeUtil::isPrime) 4 |       .limit(5) 5 |       .forEach(System.out::println); 6 | 7 | 2 8 | 3 9 | 5 10 | 7 11 | 11 12 | -------------------------------------------------------------------------------- /060/src_032_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.iterate(2L, PrimeUtil::next) 2 |       .skip(100) 3 |       .limit(5) 4 |       .forEach(System.out::println); 5 | 6 | 547 7 | 557 8 | 563 9 | 569 10 | 571 11 | -------------------------------------------------------------------------------- /060/src_033_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.generate(Math::random) 2 |       .limit(5) 3 |       .forEach(System.out::println); 4 | 5 | 0.05958352209327644 6 | 0.8122226657626394 7 | 0.5073323815997652 8 | 0.9327951597282766 9 | 0.4314430923877808 10 | -------------------------------------------------------------------------------- /060/src_034_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.generate(new PrimeUtil()::next) 2 |       .skip(100) 3 |       .limit(5) 4 |       .forEach(System.out::println); 5 | 6 | 547 7 | 557 8 | 563 9 | 569 10 | 571 11 | -------------------------------------------------------------------------------- /060/src_035_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Print five random integers 2 | new Random().ints() 3 |             .limit(5) 4 |             .forEach(System.out::println); 5 | 6 | -1147567659 7 | 285663603 8 | -412283607 9 | 412487893 10 | -22795557 11 | -------------------------------------------------------------------------------- /060/src_036_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Print five random integers 2 | Stream.generate(new Random()::nextInt) 3 |       .limit(5) 4 |       .forEach(System.out::println); 5 | -------------------------------------------------------------------------------- /060/src_037_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | IntStream.generate(new Random()::nextInt) 2 |          .limit(5) 3 |          .forEach(System.out::println); 4 | -------------------------------------------------------------------------------- /060/src_038_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | IntStream zeroes = IntStream.generate(() -> 0); 2 | -------------------------------------------------------------------------------- /060/src_039_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | // Creates a stream from an int array with elements 2 | // 1, 2, and 3 3 | IntStream numbers = Arrays.stream(new int[]{1, 2, 3}); 4 | // Creates a stream from a String array with elements 5 | // "Ken", and "Jeff" 6 | Stream names = Arrays.stream( 7 | new String[] {"Ken", "Jeff"}); 8 | -------------------------------------------------------------------------------- /060/src_041_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | String str = "5 apples and 25 oranges"; 2 | str.chars() 3 |    .filter(n -> !Character.isDigit((char)n) 4 | && !Character.isWhitespace((char)n)) 5 |    .forEach(n -> System.out.print((char)n)); 6 | 7 | applesandoranges 8 | -------------------------------------------------------------------------------- /060/src_042_Creating_Streams.java: -------------------------------------------------------------------------------- 1 | String str = "Ken,Jeff,Lee"; 2 | Pattern.compile(",") 3 |        .splitAsStream(str) 4 |        .forEach(System.out::println); 5 | 6 | Ken 7 | Jeff 8 | Lee 9 | -------------------------------------------------------------------------------- /060/src_043_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | Person ken = new Person(1, "Ken", Person.Gender.MALE, 2 | null, 6000.0); 3 | int year = ken.getDob().getYear(); 4 | // <- Throws a NullPointerException 5 | System.out.println("Ken was born in the year " + year); 6 | -------------------------------------------------------------------------------- /060/src_046_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | // Create an Optional for the string "Hello" 2 | Optional str = Optional.of("Hello"); 3 | // Print the value in the Optional, if present 4 | str.ifPresent(value -> 5 | System.out.println("Optional contains " + value)); 6 | 7 | Optional contains Hello 8 | -------------------------------------------------------------------------------- /060/src_048_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | List> optionalList = List.of( 2 | Optional.of(1), 3 |     Optional.empty(), 4 |     Optional.of(2), 5 |     Optional.empty(), 6 |     Optional.of(3)); 7 | -------------------------------------------------------------------------------- /060/src_049_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | optionalList.stream() 2 |         .forEach(p -> p.ifPresentOrElse( 3 | System.out::println, 4 |          () -> System.out.println("Empty"))); 5 | 6 | 1 7 | Empty 8 | 2 9 | Empty 10 | 3 11 | -------------------------------------------------------------------------------- /060/src_050_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | optionalList.stream() 2 |             .map(p -> p.or(() -> Optional.of(0))) 3 |             .forEach(System.out::println); 4 | 5 | Optional[1] 6 | Optional[0] 7 | Optional[2] 8 | Optional[0] 9 | Optional[3] 10 | -------------------------------------------------------------------------------- /060/src_051_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | // Print the values in all non-empty Optionals 2 | optionalList.stream() 3 |             .filter(Optional::isPresent) 4 |             .map(Optional::get) 5 |             .forEach(System.out::println); 6 | 7 | 1 8 | 2 9 | 3 10 | -------------------------------------------------------------------------------- /060/src_052_Representing_an_Optional_Value.java: -------------------------------------------------------------------------------- 1 | // Print the values in all non-empty Optionals 2 | optionalList.stream() 3 |             .flatMap(Optional::stream) 4 |             .forEach(System.out::println); 5 | 6 | 1 7 | 2 8 | 3 9 | -------------------------------------------------------------------------------- /060/src_054_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Person.persons() 2 |       .stream() 3 |       .filter(Person::isFemale) 4 |       .forEach(System.out::println); 5 | 6 | (3, Donna, FEMALE, 1962-07-29, 8700.00) 7 | (5, Laynie, FEMALE, 2012-12-13, 0.00) 8 | -------------------------------------------------------------------------------- /060/src_055_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | IntStream.rangeClosed(1, 5) 2 |          .map(n -> n * n) 3 |          .forEach(System.out::println); 4 | 5 | 1 6 | 4 7 | 9 8 | 16 9 | 25 10 | -------------------------------------------------------------------------------- /060/src_056_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Person.persons() 2 |       .stream() 3 |       .map(Person::getName) 4 |       .forEach(System.out::println); 5 | 6 | Ken 7 | Jeff 8 | Donna 9 | Chris 10 | Laynie 11 | Li 12 | -------------------------------------------------------------------------------- /060/src_057_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.of(1, 2, 3) 2 |       .map(n -> Stream.of(n, n * n)) 3 |       .forEach(System.out::println); 4 | 5 | java.util.stream.ReferencePipeline$Head@372f7a8d 6 | java.util.stream.ReferencePipeline$Head@2f92e0f4 7 | java.util.stream.ReferencePipeline\$Head@28a418fc 8 | -------------------------------------------------------------------------------- /060/src_058_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.of(1, 2, 3) 2 |       .map(n -> Stream.of(n, n * n)) 3 |       .forEach(e -> e.forEach(System.out::println)); 4 | 5 | 1 6 | 1 7 | 2 8 | 4 9 | 3 10 | 9 11 | -------------------------------------------------------------------------------- /060/src_059_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.of(1, 2, 3) 2 |       .flatMap(n -> Stream.of(n, n * n)) 3 |       .forEach(System.out::println); 4 | 5 | 1 6 | 1 7 | 2 8 | 4 9 | 3 10 | 9 11 | -------------------------------------------------------------------------------- /060/src_060_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long count = Stream.of("Ken", "Jeff", "Ellen") 2 |            .map(name -> name.chars()) 3 |            .flatMap(intStream -> intStream. 4 | mapToObj(n -> (char)n)) 5 |            .filter(ch -> ch == 'e' || ch == 'E') 6 |            .count(); 7 | System.out.println("Es count: " + count); 8 | 9 | Es count: 4 10 | -------------------------------------------------------------------------------- /060/src_061_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long count = Stream.of("Ken", "Jeff", "Ellen") 2 |            .flatMap(name -> 3 | IntStream.range(0, name.length()) 4 |                .mapToObj(name::charAt)) 5 |            .filter(ch -> ch == 'e' || ch == 'E') 6 |            .count(); 7 | -------------------------------------------------------------------------------- /060/src_062_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Person.persons() 2 |       .stream() 3 |       .filter(Person::isFemale) 4 |       .map(Person::getName) 5 |       .forEach(System.out::println); 6 | 7 | Donna 8 | Laynie 9 | -------------------------------------------------------------------------------- /060/src_063_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Person.persons() 2 |       .stream() 3 |       .filter(Person::isMale) 4 |       .filter(p -> p.getIncome() > 5000.0) 5 |       .map(Person::getName) 6 |       .forEach(System.out::println); 7 | 8 | Ken 9 | Jeff 10 | -------------------------------------------------------------------------------- /060/src_064_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Person.persons() 2 |       .stream() 3 |       .filter(p -> p.isMale() && p.getIncome() > 5000.0) 4 |       .map(Person::getName) 5 |       .forEach(System.out::println); 6 | 7 | Ken 8 | Jeff 9 | -------------------------------------------------------------------------------- /060/src_065_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | 1, 2, 3, 4, 5, 6, 7 2 | -------------------------------------------------------------------------------- /060/src_066_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.of(1, 2, 3, 4, 5, 6, 7) 2 |              .dropWhile(e -> e < 5) 3 |              .forEach(System.out::println); 4 | 5 | 5 6 | 6 7 | 7 8 | -------------------------------------------------------------------------------- /060/src_067_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Stream.of(1, 5, 6, 2, 3, 4, 7) 2 |       .dropWhile(e -> e < 5) 3 |       .forEach(System.out::println); 4 | 5 | 5 6 | 6 7 | 2 8 | 3 9 | 4 10 | 7 11 | -------------------------------------------------------------------------------- /060/src_070_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | List numbers = List.of(1, 2, 3, 4, 5); 2 | int sum = numbers.stream() 3 |                  .reduce(0, Integer::sum); 4 | System.out.println(sum); 5 | 6 | 15 7 | -------------------------------------------------------------------------------- /060/src_072_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | double sum = Person.persons() 2 |                    .stream() 3 |                    .map(Person::getIncome) 4 |                    .reduce(0.0, Double::sum); 5 | System.out.println(sum); 6 | 7 | 26000.0 8 | -------------------------------------------------------------------------------- /060/src_073_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | U reduce(U identity, 2 | BiFunction accumulator, 3 | BinaryOperator combiner) 4 | -------------------------------------------------------------------------------- /060/src_074_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | double sum = Person.persons() 2 |     .stream() 3 |     .reduce(0.0, (partialSum, person) -> 4 | partialSum + person.getIncome(), Double::sum); 5 | System.out.println(sum); 6 | 7 | 26000.0 8 | -------------------------------------------------------------------------------- /060/src_078_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Optional max = Stream.of(1, 2, 3, 4, 5) 2 |                               .reduce(Integer::max); 3 | if (max.isPresent()) { 4 |     System.out.println("max = " + max.get()); 5 | } else { 6 |     System.out.println("max is not defined."); 7 | } 8 | 9 | max = 5 10 | -------------------------------------------------------------------------------- /060/src_079_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | Optional max = Stream.empty() 2 |                               .reduce(Integer::max); 3 | if (max.isPresent()) { 4 |     System.out.println("max = " + max.get()); 5 | } else { 6 |     System.out.println("max is not defined."); 7 | } 8 | 9 | max is not defined. 10 | -------------------------------------------------------------------------------- /060/src_081_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | double totalIncome = Person.persons() 2 | .stream() 3 |     .mapToDouble(Person::getIncome) 4 |     .sum(); 5 | System.out.println("Total Income: " + totalIncome); 6 | 7 | Total Income : 26000.0 8 | -------------------------------------------------------------------------------- /060/src_084_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long personCount = Person.persons() 2 | .stream() 3 |     .count(); 4 | System.out.println("Person count: " + personCount); 5 | 6 | Person count: 6 7 | -------------------------------------------------------------------------------- /060/src_085_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long personCount = Person.persons() 2 | .stream() 3 |     .mapToLong(p -> 1L) 4 |     .sum(); 5 | -------------------------------------------------------------------------------- /060/src_086_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long personCount = Person.persons() 2 |     .stream() 3 |     .map(p -> 1L) 4 |     .reduce(0L, Long::sum); 5 | -------------------------------------------------------------------------------- /060/src_087_Applying_Operations_to_Streams.java: -------------------------------------------------------------------------------- 1 | long personCount = Person.persons() 2 |     .stream() 3 |     .reduce(0L, (partialCount, person) -> 4 | partialCount + 1L, 5 | Long::sum); 6 | -------------------------------------------------------------------------------- /060/src_088_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | // Using a lambda expression 2 | Supplier> supplier = 3 | () -> new ArrayList<>(); 4 | // Using a constructor reference 5 | Supplier> supplier = 6 | ArrayList::new; 7 | -------------------------------------------------------------------------------- /060/src_089_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | // Using a lambda expression 2 | BiConsumer, String> accumulator = 3 | (list, name) -> list.add(name); 4 | // Using a method reference 5 | BiConsumer, String> accumulator = 6 | ArrayList::add; 7 | -------------------------------------------------------------------------------- /060/src_090_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | // Using a lambda expression 2 | BiConsumer, 3 | ArrayList> combiner = 4 |     (list1, list2) -> list1.addAll(list2); 5 | // Using a method reference 6 | BiConsumer, 7 | ArrayList> combiner = 8 | ArrayList::addAll; 9 | -------------------------------------------------------------------------------- /060/src_091_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | List names = Person.persons() 2 |     .stream() 3 |     .map(Person::getName) 4 |     .collect(ArrayList::new, 5 | ArrayList::add, 6 | ArrayList::addAll); 7 | System.out.println(names); 8 | 9 | [Ken, Jeff, Donna, Chris, Laynie, Li] 10 | -------------------------------------------------------------------------------- /060/src_092_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | public interface Collector { 2 |     Supplier supplier(); 3 |     BiConsumer accumulator(); 4 |     BinaryOperator combiner(); 5 |     Function finisher(); 6 |     Set characteristics(); 7 | } 8 | -------------------------------------------------------------------------------- /060/src_093_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | List names = Person.persons() 2 |     .stream() 3 |     .map(Person::getName) 4 |     .collect(Collectors.toList()); 5 | System.out.println(names); 6 | 7 | [Ken, Jeff, Donna, Chris, Laynie, Li] 8 | -------------------------------------------------------------------------------- /060/src_094_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | Set uniqueNames = Person.persons() 2 |     .stream() 3 |     .map(Person::getName) 4 |     .collect(Collectors.toSet()); 5 | System.out.println(uniqueNames); 6 | 7 | [Donna, Ken, Chris, Jeff, Laynie, Li] 8 | -------------------------------------------------------------------------------- /060/src_095_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | SortedSet uniqueSortedNames= Person.persons() 2 |     .stream() 3 |     .map(Person::getName) 4 |     .collect(Collectors.toCollection(TreeSet::new)); 5 | System.out.println(uniqueSortedNames); 6 | 7 | [Chris, Donna, Jeff, Ken, Laynie, Li] 8 | -------------------------------------------------------------------------------- /060/src_096_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | List sortedName = Person.persons() 2 |     .stream() 3 |     .map(Person::getName) 4 |     .sorted() 5 |     .collect(Collectors.toList()); 6 | System.out.println(sortedName); 7 | 8 | [Chris, Donna, Jeff, Ken, Laynie, Li] 9 | -------------------------------------------------------------------------------- /060/src_097_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | long count = Person.persons() 2 |     .stream() 3 |     .collect(Collectors.counting()); 4 | System.out.println("Person count: " + count); 5 | 6 | Person count: 6 7 | -------------------------------------------------------------------------------- /060/src_098_Collecting_Data_Using_Collectors.java: -------------------------------------------------------------------------------- 1 | long count = Person.persons() 2 |     .stream() 3 |     .count(); 4 | System.out.println("Persons count: " + count); 5 | 6 | Persons count: 6 7 | -------------------------------------------------------------------------------- /060/src_100_Collecting_Summary_Statistics.java: -------------------------------------------------------------------------------- 1 | DoubleSummaryStatistics incomeStats = 2 |     Person.persons() 3 |           .stream() 4 |           .collect(Collectors.summarizingDouble(Person::getIncome)); 5 | System.out.println(incomeStats); 6 | 7 | DoubleSummaryStatistics{count=6, sum=26000.000000, min=0.000000, average=4333.333333, 8 | max=8700.000000} 9 | -------------------------------------------------------------------------------- /060/src_101_Collecting_Data_in_Maps.java: -------------------------------------------------------------------------------- 1 | Map idToNameMap = Person.persons() 2 |     .stream() 3 |     .collect(Collectors.toMap(Person::getId, 4 | Person::getName)); 5 | System.out.println(idToNameMap); 6 | 7 | {1=Ken, 2=Jeff, 3=Donna, 4=Chris, 5=Laynie, 6=Li} 8 | -------------------------------------------------------------------------------- /060/src_102_Collecting_Data_in_Maps.java: -------------------------------------------------------------------------------- 1 | Map genderToNamesMap = 2 | Person.persons() 3 |     .stream() 4 |     .collect(Collectors.toMap(Person::getGender, 5 | Person::getName)); 6 | 7 | Exception in thread "main" 8 | java.lang.IllegalStateException: Duplicate key Ken ... 9 | -------------------------------------------------------------------------------- /060/src_104_Collecting_Data_in_Maps.java: -------------------------------------------------------------------------------- 1 | Map countByGender = Person.persons() 2 |     .stream() 3 |     .collect(Collectors.toMap( 4 | Person::getGender, 5 | p -> 1L, 6 |             (oldCount, newCount) -> oldCount + 1)); 7 | System.out.println(countByGender); 8 | 9 | {MALE=4, FEMALE=2} 10 | -------------------------------------------------------------------------------- /060/src_106_Grouping_Data.java: -------------------------------------------------------------------------------- 1 | Map countByGender = 2 |     Person.persons() 3 |           .stream() 4 |           .collect(Collectors.groupingBy( 5 | Person::getGender, 6 | Collectors.counting())); 7 | System.out.println(countByGender); 8 | 9 | {MALE=4, FEMALE=2} 10 | -------------------------------------------------------------------------------- /060/src_107_Grouping_Data.java: -------------------------------------------------------------------------------- 1 | mapping(Function mapper, 2 | Collector downstream) 3 | -------------------------------------------------------------------------------- /060/src_113_Adapting_the_Collector_Results.java: -------------------------------------------------------------------------------- 1 | Map dobCalendar = Person.persons() 2 |     .stream() 3 |     .collect(groupingBy(p -> p.getDob().getMonth(), 4 |              mapping(Person::getName, joining(", ")))); 5 | dobCalendar.entrySet().forEach(System.out::println); 6 | 7 | MAY=Ken, Li 8 | DECEMBER=Chris, Laynie 9 | JULY=Jeff, Donna 10 | -------------------------------------------------------------------------------- /060/src_115_Parallel_Streams.java: -------------------------------------------------------------------------------- 1 | String names = Person 2 | .persons()              // The data source 3 |     .stream()               // Produces a sequential stream 4 |     .filter(Person::isMale) // Processed in serial 5 |     .map(Person::getName)   // Processed in serial 6 |     .collect(Collectors. 7 | joining(", ")); // Processed in serial 8 | -------------------------------------------------------------------------------- /060/src_116_Parallel_Streams.java: -------------------------------------------------------------------------------- 1 | String names = Person 2 | .persons()            // The data source 3 |     .parallelStream()       // Produces a parallel stream 4 |     .filter(Person::isMale) // Processed in parallel 5 |     .map(Person::getName)   // Processed in parallel 6 |     .collect(Collectors. 7 | joining(", ")); // Processed in parallel 8 | -------------------------------------------------------------------------------- /060/src_118_Parallel_Streams.java: -------------------------------------------------------------------------------- 1 | // Process the stream in serial 2 | long count = IntStream.rangeClosed(2, Integer.MAX_VALUE/10) 3 |                       .filter(PrimeUtil::isPrime) 4 |                       .count(); 5 | -------------------------------------------------------------------------------- /060/src_119_Parallel_Streams.java: -------------------------------------------------------------------------------- 1 | // Process the stream in parallel 2 | long count = IntStream.rangeClosed(2, Integer.MAX_VALUE/10) 3 |                       .parallel() 4 |                       .filter(PrimeUtil::isPrime) 5 |                       .count(); 6 | -------------------------------------------------------------------------------- /060/src_120_Exercises.java: -------------------------------------------------------------------------------- 1 | List names = List.of( 2 | "Mo", "Jeff", "Li", "Dola"); 3 | int sum = names.stream() 4 |     ./* your code goes here */; 5 | System.out.println("Total characters: " + sum); 6 | -------------------------------------------------------------------------------- /060/src_121_Exercises.misc: -------------------------------------------------------------------------------- 1 | Total characters: 12 2 | -------------------------------------------------------------------------------- /060/src_122_Exercises.java: -------------------------------------------------------------------------------- 1 | Stream noNames1 = Stream. 2 | /* Your code goes here */; 3 | Stream noNames2 = Stream. 4 | /* Your code goes here */; 5 | -------------------------------------------------------------------------------- /060/src_123_Exercises.java: -------------------------------------------------------------------------------- 1 | Stream stream = Stream. 2 | generate(() -> 1969); 3 | -------------------------------------------------------------------------------- /060/src_124_Exercises.java: -------------------------------------------------------------------------------- 1 | List> names = List.of( 2 | Optional.of("Ken"), 3 |     Optional.empty(), 4 |     Optional.of("Li"), 5 |     Optional.empty(), 6 |     Optional.of("Toto")); 7 | names.stream() 8 |      .flatMap(/* Your code goes here */) 9 |      .forEach(/* Your code goes here */); 10 | -------------------------------------------------------------------------------- /060/src_125_Exercises.misc: -------------------------------------------------------------------------------- 1 | Ken: 3 2 | Li: 2 3 | Toto: 4 4 | -------------------------------------------------------------------------------- /060/src_126_Exercises.java: -------------------------------------------------------------------------------- 1 | int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 2 | -------------------------------------------------------------------------------- /060/src_127_Exercises.java: -------------------------------------------------------------------------------- 1 | Map map = new HashMap<>(); 2 | map.put(1, "One"); 3 | map.put(2, "One"); 4 | map.put(3, null); 5 | map.put(4, "Two"); 6 | map.entrySet() 7 |    .stream() 8 |    .flatMap(/* Your code goes here */) 9 |    ./* Your code goes here */ 10 |    .forEach(System.out::println); 11 | -------------------------------------------------------------------------------- /060/src_128_Exercises.misc: -------------------------------------------------------------------------------- 1 | One 2 | Two 3 | -------------------------------------------------------------------------------- /060/src_129_Exercises.java: -------------------------------------------------------------------------------- 1 | List list = List.of( 2 | 10, 19, 20, 40, 45, 50); 3 | Map oddEvenCounts = list.stream() 4 |         .map(/* Your code goes here */) 5 |         .collect(/* Your code goes here */); 6 | System.out.println(oddEvenCounts); 7 | -------------------------------------------------------------------------------- /060/src_130_Exercises.misc: -------------------------------------------------------------------------------- 1 | {Even=4, Odd=2} 2 | -------------------------------------------------------------------------------- /060/src_131_Exercises.java: -------------------------------------------------------------------------------- 1 | List list = List.of(5, 1, 2, 7, 3, 4, 8); 2 | String str = list.stream() 3 |              ./* Multiple method calls go here */; 4 | System.out.println(str); 5 | -------------------------------------------------------------------------------- /060/src_132_Exercises.misc: -------------------------------------------------------------------------------- 1 | 1:3:5:7 2 | -------------------------------------------------------------------------------- /070/src_001_Discovering_Services.java: -------------------------------------------------------------------------------- 1 | uses ; 2 | -------------------------------------------------------------------------------- /070/src_002_Discovering_Services.java: -------------------------------------------------------------------------------- 1 | uses S; 2 | -------------------------------------------------------------------------------- /070/src_003_Discovering_Services.java: -------------------------------------------------------------------------------- 1 | module jdojo.loader { 2 | uses com.jdojo.PrimeChecker; 3 | uses com.jdojo.CsvParser: 4 | // Other module statements go here 5 | } 6 | -------------------------------------------------------------------------------- /070/src_004_Discovering_Services.java: -------------------------------------------------------------------------------- 1 | // Import types from other packages 2 | import com.jdojo.PrimeChecker; 3 | import com.jdojo.CsvParser: 4 | module jdojo.loader { 5 | uses PrimeChecker; 6 | uses CsvParser: 7 | // Other module statements go here 8 | } 9 | -------------------------------------------------------------------------------- /070/src_005_Providing_Service_Implementations.java: -------------------------------------------------------------------------------- 1 | provides with 2 | ; 3 | -------------------------------------------------------------------------------- /070/src_006_Providing_Service_Implementations.java: -------------------------------------------------------------------------------- 1 | module com.jdojo.provider { 2 | provides com.jdojo.PrimeChecker with 3 | com.jdojo.impl.PrimeCheckerFactory; 4 | provides com.jdojo.CsvParser with 5 | com.jdojo.impl.CsvFastParser; 6 | // Other module statements go here 7 | } 8 | -------------------------------------------------------------------------------- /070/src_007_Providing_Service_Implementations.java: -------------------------------------------------------------------------------- 1 | module com.jdojo.parser { 2 | uses com.jdojo.XmlParser; 3 | provides com.jdojo.XmlParser with 4 | com.jdojo.xml.impl.XmlParserFactory; 5 | // Other module statements go here 6 | } 7 | -------------------------------------------------------------------------------- /070/src_008_Defining_the_Service_Interface.java: -------------------------------------------------------------------------------- 1 | public interface PrimeChecker { 2 | boolean isPrime(long n); 3 | } 4 | -------------------------------------------------------------------------------- /070/src_010_Obtaining_Service_Provider_Instances.java: -------------------------------------------------------------------------------- 1 | ServiceLoader loader = 2 | ServiceLoader.load(PrimeChecker.class); 3 | for (PrimeChecker checker : loader) { 4 | // checker is your service provider instance 5 | } 6 | -------------------------------------------------------------------------------- /070/src_011_Obtaining_Service_Provider_Instances.java: -------------------------------------------------------------------------------- 1 | public Stream> stream() 2 | -------------------------------------------------------------------------------- /070/src_012_Obtaining_Service_Provider_Instances.java: -------------------------------------------------------------------------------- 1 | public static interface Provider extends Supplier { 2 | // Returns a Class reference of the class of the 3 | // service provider 4 | Class type(); 5 | @Override 6 | S get(); 7 | } 8 | -------------------------------------------------------------------------------- /070/src_016_Obtaining_Service_Provider_Instances.java: -------------------------------------------------------------------------------- 1 | String providerName = 2 | "com.jdojo.prime.faster.FasterPrimeChecker"; 3 | Optional> optional = 4 | ServiceLoader.load(PrimeChecker.class) 5 | .stream() 6 | .filter((Provider p) -> 7 | p.type().getName().equals(providerName)) 8 | .findFirst(); 9 | -------------------------------------------------------------------------------- /070/src_019_Defining_Service_Providers.java: -------------------------------------------------------------------------------- 1 | public static FasterPrimeChecker provider() { /*...*/ } 2 | -------------------------------------------------------------------------------- /070/src_020_Defining_Service_Providers.java: -------------------------------------------------------------------------------- 1 | public static ProbablePrimeChecker provider() {/*...*/} 2 | -------------------------------------------------------------------------------- /070/src_026_Testing_Prime_Service_in_Legacy_Mode.java: -------------------------------------------------------------------------------- 1 | META-INF/services/ 2 | -------------------------------------------------------------------------------- /070/src_027_Testing_Prime_Service_in_Legacy_Mode.java: -------------------------------------------------------------------------------- 1 | META-INF/services/com.jdojo.prime.PrimeChecker 2 | -------------------------------------------------------------------------------- /080/src_001_IP_Addressing_Scheme.misc: -------------------------------------------------------------------------------- 1 | IP Address: 10001100 00001010 00001011 00001001 2 | Subnet Mask: 11111111 11111111 11111100 00000000 3 | ------------------------------------------------ 4 | Logical AND: 10001100 00001010 00001000 00000000 5 | (140) (10) (8) (0) 6 | -------------------------------------------------------------------------------- /080/src_004_Representing_a_Socket_Address.java: -------------------------------------------------------------------------------- 1 | static InetSocketAddress createUnresolved( 2 | String host, int port) 3 | -------------------------------------------------------------------------------- /080/src_006_Creating_a_TCP_Server_Socket.java: -------------------------------------------------------------------------------- 1 | // Create a server socket at port 12900, with 100 as the 2 | // wait queue size at the localhost loopback address 3 | ServerSocket serverSocket = 4 | new ServerSocket(12900, 100, InetAddress. 5 | getByName("localhost")); 6 | -------------------------------------------------------------------------------- /080/src_007_Creating_a_TCP_Server_Socket.java: -------------------------------------------------------------------------------- 1 | // Wait for a new remote connection request 2 | Socket activeSocket = serverSocket.accept(); 3 | -------------------------------------------------------------------------------- /080/src_009_Creating_a_TCP_Server_Socket.java: -------------------------------------------------------------------------------- 1 | // Read one line of text from the connection 2 | String inMsg = br.readLine(); 3 | // Write some text to the output buffer 4 | bw.write("Hello from server"); 5 | bw.flush(); 6 | -------------------------------------------------------------------------------- /080/src_010_Creating_a_TCP_Server_Socket.java: -------------------------------------------------------------------------------- 1 | // Close the socket 2 | activeSocket.close(); 3 | -------------------------------------------------------------------------------- /080/src_011_Creating_a_TCP_Server_Socket.java: -------------------------------------------------------------------------------- 1 | ServerSocket serverSocket = ...; 2 | // <- create a server socket here; 3 | while(true) { 4 | Socket activeSocket = serverSocket.accept(); 5 | // Handle the client request on activeSocket here 6 | } 7 | -------------------------------------------------------------------------------- /080/src_014_Putting_a_TCP_Server_and_Clients_Together.misc: -------------------------------------------------------------------------------- 1 | Server started at: ServerSocket[addr=localhost/ 2 | 127.0.0.1,port=0,localport=12900] 3 | Waiting for a connection ... 4 | Received a connection from Socket[addr=/127.0.0.1, 5 | port=1698,localport=12900] 6 | Waiting for a connection ... 7 | Received from client: Hello 8 | -------------------------------------------------------------------------------- /080/src_015_Putting_a_TCP_Server_and_Clients_Together.java: -------------------------------------------------------------------------------- 1 | Started client socket at /127.0.0.1:53498 2 | Please enter a message (Bye to quit):Hello 3 | Server: Hello 4 | Please enter a message (Bye to quit):Bye 5 | -------------------------------------------------------------------------------- /080/src_016_Working_with_UDP_Sockets.java: -------------------------------------------------------------------------------- 1 | // Create a UDP Socket bound to a port number 15900 2 | // at localhost 3 | DatagramSocket udpSocket = 4 | new DatagramSocket(15900, "localhost"); 5 | -------------------------------------------------------------------------------- /080/src_020_Creating_a_UDP_Echo_Server.java: -------------------------------------------------------------------------------- 1 | DatagramSocket socket = 2 | new DatagramSocket(15900); 3 | DatagramPacket packet = 4 | new DatagramPacket(new byte[1024], 1024); 5 | while(true) { 6 | // Receive the packet 7 | socket.receive(packet); 8 | // Send back the same packet to the sender 9 | socket.send(packet); 10 | } 11 | -------------------------------------------------------------------------------- /080/src_024_UDP_Multicast_Sockets.misc: -------------------------------------------------------------------------------- 1 | Multicast Receiver running at: 2 | 0.0.0.0/0.0.0.0:18777 3 | Waiting for a multicast message... 4 | [Multicast Receiver] Received: 5 | Hello multicast socket 6 | Waiting for a multicast message... 7 | -------------------------------------------------------------------------------- /080/src_025_UDP_Multicast_Sockets.misc: -------------------------------------------------------------------------------- 1 | Sent a multicast message. 2 | Exiting application 3 | -------------------------------------------------------------------------------- /080/src_026_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | : 2 | -------------------------------------------------------------------------------- /080/src_027_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | ://?# 2 | -------------------------------------------------------------------------------- /080/src_028_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | URI: http://www.jdojo.com/java/intro.html? 2 | id=123#conclusion 3 | Scheme: http 4 | Authority: www.jdojo.com 5 | Path: /java/intro.html 6 | Query: id=123 7 | Fragment: conclusion 8 | -------------------------------------------------------------------------------- /080/src_029_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | http://www.jdojo.com/details?rate=5.2% 2 | -------------------------------------------------------------------------------- /080/src_030_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | http://www.jdojo.com/details?rate=5.2%25 2 | -------------------------------------------------------------------------------- /080/src_031_URI__URL__and_URN.misc: -------------------------------------------------------------------------------- 1 | http://www.jdojo.com/java/intro.html 2 | -------------------------------------------------------------------------------- /080/src_032_URI_and_URL_as_Java_Objects.java: -------------------------------------------------------------------------------- 1 | // Create a URI object 2 | URI baseURI = new URI("http://www.yahoo.com"); 3 | // Create a URI with relative URI string and resolve it 4 | // using baseURI 5 | URI relativeURI = new URI("welcome.html"); 6 | URI resolvedRelativeURI = baseURI.resolve(relativeURI); 7 | -------------------------------------------------------------------------------- /080/src_033_URI_and_URL_as_Java_Objects.java: -------------------------------------------------------------------------------- 1 | URL baseURL = baseURI.toURL(); 2 | -------------------------------------------------------------------------------- /080/src_034_URI_and_URL_as_Java_Objects.java: -------------------------------------------------------------------------------- 1 | URI uri2 = URI.create("http://www.yahoo.com"); 2 | -------------------------------------------------------------------------------- /080/src_036_Accessing_the_Contents_of_a_URL.java: -------------------------------------------------------------------------------- 1 | URL url = new URL("your URL string goes here"); 2 | InputStream ins = url.openStream(); 3 | -------------------------------------------------------------------------------- /080/src_040_Accessing_the_Contents_of_a_URL.java: -------------------------------------------------------------------------------- 1 | String str = 2 | "jar:http://www.abc.com/myclasses.jar!/myfolder/" + 3 | "Abc.class"; 4 | URL url = new URL(str); 5 | JarURLConnection connection = (JarURLConnection) 6 | url.openConnection(); 7 | // Use the connection object to access any jar related 8 | // data. 9 | -------------------------------------------------------------------------------- /080/src_041_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Get a selector object 2 | Selector selector = Selector.open(); 3 | -------------------------------------------------------------------------------- /080/src_042_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Get a server socket channel 2 | ServerSocketChannel ssChannel = ServerSocketChannel.open(); 3 | -------------------------------------------------------------------------------- /080/src_043_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Configure the server socket channel to be non-blocking 2 | ssChannel.configureBlocking(false); 3 | -------------------------------------------------------------------------------- /080/src_044_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | InetAddress hostIPAddress = 2 | InetAddress.getByName("localhost"); 3 | int port = 19000; 4 | ssChannel.bind(new InetSocketAddress(hostIPAddress, port)); 5 | -------------------------------------------------------------------------------- /080/src_045_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Register the server socket channel with the selector 2 | // for accept operation 3 | ssChannel.register(selector, SelectionKey.OP_ACCEPT); 4 | -------------------------------------------------------------------------------- /080/src_046_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Get the key count in the ready set 2 | int readyCount = selector.select(); 3 | -------------------------------------------------------------------------------- /080/src_047_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Get the set of ready keys 2 | Set readySet = selector.selectedKeys(); 3 | -------------------------------------------------------------------------------- /080/src_048_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | while(true) { 2 | // Get the count of keys in the ready set. If ready 3 | // key count is greater than zero, process each key 4 | // in the ready set. 5 | } 6 | -------------------------------------------------------------------------------- /080/src_051_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | // Register for read and write 2 | sChannel.register(key.selector(), 3 | SelectionKey.OP_READ | SelectionKey.OP_WRITE); 4 | -------------------------------------------------------------------------------- /080/src_053_Non_Blocking_Socket_Programming.java: -------------------------------------------------------------------------------- 1 | SocketChannel sChannel = (SocketChannel)key.channel(); 2 | String msg = 3 | "message to be sent to remote client goes here"; 4 | ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); 5 | sChannel.write(buffer); 6 | -------------------------------------------------------------------------------- /080/src_056_Socket_Security_Permissions.java: -------------------------------------------------------------------------------- 1 | grant { 2 | permission java.net.SocketPermission 3 | "target", "actions"; 4 | }; 5 | -------------------------------------------------------------------------------- /080/src_061_Asynchronous_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | // Prepare the attachment 2 | Attachment attach = new Attachment(); 3 | attach.server = server; 4 | // Accept new connections 5 | server.accept(attach, new ConnectionHandler()); 6 | -------------------------------------------------------------------------------- /080/src_062_Asynchronous_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | try { 2 | // Wait indefinitely until someone interrupts the 3 | // main thread 4 | Thread.currentThread().join(); 5 | } catch (InterruptedException e) { 6 | e.printStackTrace(); 7 | } 8 | -------------------------------------------------------------------------------- /080/src_063_Asynchronous_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | class Attachment { 2 | AsynchronousServerSocketChannel server; 3 | AsynchronousSocketChannel client; 4 | ByteBuffer buffer; 5 | SocketAddress clientAddr; 6 | boolean isRead; 7 | } 8 | -------------------------------------------------------------------------------- /080/src_067_Asynchronous_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | class Attachment { 2 | AsynchronousSocketChannel channel; 3 | ByteBuffer buffer; 4 | Thread mainThread; 5 | boolean isRead; 6 | } 7 | -------------------------------------------------------------------------------- /080/src_068_Asynchronous_Socket_Channels.misc: -------------------------------------------------------------------------------- 1 | Server is listening at localhost/127.0.0.1:8989 2 | -------------------------------------------------------------------------------- /080/src_069_Asynchronous_Socket_Channels.misc: -------------------------------------------------------------------------------- 1 | java.net.BindException: Address already in use: bind 2 | -------------------------------------------------------------------------------- /080/src_070_Asynchronous_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | Connecting to the server... 2 | Connected to the server... 3 | Server Responded: Hello 4 | Please enter a message (Bye to quit): 5 | -------------------------------------------------------------------------------- /080/src_071_Asynchronous_Socket_Channels.misc: -------------------------------------------------------------------------------- 1 | Connecting to the server... 2 | java.util.concurrent.ExecutionException: 3 | java.io.IOException: The remote system refused 4 | the network connection. 5 | -------------------------------------------------------------------------------- /080/src_073_Datagram_Oriented_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | channel.setOption(StandardSocketOptions.SO_REUSEADDR, true) 2 | -------------------------------------------------------------------------------- /080/src_074_Datagram_Oriented_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | // Bind the channel to any available address automatically 2 | channel.bind(null); 3 | // Bind the channel to "localhost" and port 8989 4 | InetSocketAddress sAddr = 5 | new InetSocketAddress("localhost", 8989); 6 | channel.bind(sAddr); 7 | -------------------------------------------------------------------------------- /080/src_076_Datagram_Oriented_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | // Prepare a ByteBufer to receive data 2 | ByteBuffer buffer = ByteBuffer.allocate(1024); 3 | // Wait to receive data from a remote host 4 | SocketAddress remoteAddress = channel.receive(buffer); 5 | -------------------------------------------------------------------------------- /080/src_077_Datagram_Oriented_Socket_Channels.java: -------------------------------------------------------------------------------- 1 | // Close the channel 2 | channel.close(); 3 | -------------------------------------------------------------------------------- /080/src_079_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | // Let other sockets reuse the same address 2 | client.setOption(StandardSocketOptions.SO_REUSEADDR, true); 3 | -------------------------------------------------------------------------------- /080/src_080_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | int MULTICAST_PORT = 8989; 2 | client.bind(new InetSocketAddress(MULTICAST_PORT)); 3 | -------------------------------------------------------------------------------- /080/src_081_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | // Get the reference of a network interface named "eth1" 2 | NetworkInterface interf = 3 | NetworkInterface.getByName("eth1"); 4 | // Set the IP_MULTICAST_IF option 5 | client.setOption(StandardSocketOptions.IP_MULTICAST_IF, 6 | interf); 7 | -------------------------------------------------------------------------------- /080/src_082_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | String MULTICAST_IP = "239.1.1.1"; 2 | // Join the multicast group on interf interface 3 | InetAddress group = InetAddress.getByName(MULTICAST_IP); 4 | MembershipKey key = client.join(group, interf); 5 | -------------------------------------------------------------------------------- /080/src_083_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | // Prepare a buffer to receive the message from the 2 | // multicast group 3 | ByteBuffer buffer = ByteBuffer.allocate(1048); 4 | // Wait to receive a message from the multicast group 5 | client.receive(buffer); 6 | -------------------------------------------------------------------------------- /080/src_084_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | // We are no longer interested in receiving multicast 2 | // message from the group. So, we need to drop the 3 | // channel's membership from the group 4 | key.drop(); 5 | -------------------------------------------------------------------------------- /080/src_085_Multicasting_Using_Datagram_Channels.java: -------------------------------------------------------------------------------- 1 | // Close the channel 2 | client.close(); 3 | -------------------------------------------------------------------------------- /090/src_001_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | SecurityManager secManager = System.getSecurityManager(); 2 | if (secManager == null) { 3 | System.setSecurityManager(new SecurityManager()); 4 | } 5 | -------------------------------------------------------------------------------- /090/src_002_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | grant { 2 | permission java.security.AllPermission; 3 | }; 4 | -------------------------------------------------------------------------------- /090/src_003_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | RemoteUtilityImpl remoteUtility = new RemoteUtilityImpl(); 2 | -------------------------------------------------------------------------------- /090/src_004_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | int port = 0; 2 | RemoteUtility remoteUtilityStub = (RemoteUtility) 3 | UnicastRemoteObject.exportObject(remoteUtility, port); 4 | -------------------------------------------------------------------------------- /090/src_005_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | Registry registry = 2 | LocateRegistry.getRegistry("localhost", 1099); 3 | String name = "MyRemoteUtility"; 4 | registry.rebind(name, remoteUtilityStub); 5 | -------------------------------------------------------------------------------- /090/src_007_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | // Get a registry stub for a local machine at port 1099 2 | Registry registry = LocateRegistry.getRegistry(); 3 | -------------------------------------------------------------------------------- /090/src_008_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | SecurityManager secManager = 2 | System.getSecurityManager(); 3 | if (secManager == null) { 4 | System.setSecurityManager( 5 | new SecurityManager()); 6 | -------------------------------------------------------------------------------- /090/src_009_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | // Locate the registry 2 | Registry registry = 3 | LocateRegistry.getRegistry( 4 | "localhost", 1099); 5 | -------------------------------------------------------------------------------- /090/src_010_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | String name = "MyRemoteUtility"; 2 | RemoteUtility remoteUtilStub = 3 | (RemoteUtility) registry. 4 | lookup(name); 5 | -------------------------------------------------------------------------------- /090/src_011_Developing_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | // Call the echo() method 2 | String reply = remoteUtilStub.echo( 3 | "Hello from the RMI client."); 4 | ... 5 | -------------------------------------------------------------------------------- /090/src_012_Running_the_RMI_Application.java: -------------------------------------------------------------------------------- 1 | grant { 2 | permission java.security.AllPermission; 3 | }; 4 | -------------------------------------------------------------------------------- /090/src_013_Running_the_RMI_Application.java: -------------------------------------------------------------------------------- 1 | java -Djava.security.policy=^ 2 | file:///C:/mypolicy/rmi.policy 3 | -------------------------------------------------------------------------------- /090/src_014_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | C:\java9\bin> rmiregistry 2 | -------------------------------------------------------------------------------- /090/src_015_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | C:\java9\bin> rmiregistry 8967 2 | -------------------------------------------------------------------------------- /090/src_016_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | C:\java9\bin> SET CLASSPATH=^ 2 | C:\Java9APIsAndModules\dist\jdojo.rmi.common.jar;^ 3 | C:\Java9APIsAndModules\dist\jdojo.rmi.server.jar 4 | C:\java9\bin> rmiregistry 5 | -------------------------------------------------------------------------------- /090/src_017_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | C:\java9\bin> rmiregistry ^ 2 | -J-Djava.rmi.server.codebase=^ 3 | file:///C:/Java9APIsAndModules/dist/jdojo.rmi.common.jar ^ 4 | file:///C:/Java9APIsAndModules/dist/jdojo.rmi.server.jar 5 | -------------------------------------------------------------------------------- /090/src_018_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | C:\java9\bin> SET CLASSPATH= 2 | C:\java9\bin> rmiregistry ^ 3 | -J-Djava.rmi.server.useCodebaseOnly=false 4 | -------------------------------------------------------------------------------- /090/src_020_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | java -Djava.rmi.server.codebase=^ 2 | file:///C:/myrmi/classes/ 3 | -------------------------------------------------------------------------------- /090/src_021_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | java -Djava.rmi.server.codebase=^ 2 | http://www.jdojo.com/rmi/classes/ 3 | -------------------------------------------------------------------------------- /090/src_022_Running_the_RMI_Application.sh: -------------------------------------------------------------------------------- 1 | java -Djava.rmi.server.codebase=^ 2 | "http://www.jdojo.com/rmi/classes/ 3 | ftp://www.jdojo.com/rmi/some/classes/c.jar" ^ 4 | 5 | -------------------------------------------------------------------------------- /090/src_024_Troubleshooting_an_RMI_Application.misc: -------------------------------------------------------------------------------- 1 | java.rmi.server.ExportException: 2 | Port already in use: 1099; nested exception is: 3 | java.net.BindException: Address already in use: 4 | JVM_Bind... 5 | -------------------------------------------------------------------------------- /090/src_025_Troubleshooting_an_RMI_Application.misc: -------------------------------------------------------------------------------- 1 | java.rmi.server.ExportException: 2 | object already exported 3 | at sun.rmi.transport.ObjectTable.putTarget( 4 | ObjectTable.java:189) 5 | at sun.rmi.transport.Transport.exportObject( 6 | Transport.java:92)... 7 | -------------------------------------------------------------------------------- /090/src_026_Troubleshooting_an_RMI_Application.misc: -------------------------------------------------------------------------------- 1 | java.security.AccessControlException: 2 | access denied (java.net.SocketPermission 3 | 127.0.0.1:1099 connect,resolve)... 4 | -------------------------------------------------------------------------------- /090/src_031_Debugging_an_RMI_Application.java: -------------------------------------------------------------------------------- 1 | grant { 2 | permission java.io.FilePermission 3 | "c:\\rmilogs\\rmi.log", "write"; 4 | permission java.util.logging.LoggingPermission 5 | "control"; 6 | }; 7 | -------------------------------------------------------------------------------- /090/src_032_Dynamic_Class_Downloading.sh: -------------------------------------------------------------------------------- 1 | java –Djava.rmi.server.codebase=^ 2 | "http://www.myurl.com/rmiclasses" ^ 3 | -Djava.rmi.server.useCodebaseOnly=true ^ 4 | ^ 5 | com.jdojo.rmi.RemoteServer 6 | -------------------------------------------------------------------------------- /090/src_034_Garbage_Collection_of_Remote_Objects.java: -------------------------------------------------------------------------------- 1 | public interface Unreferenced { 2 | void unreferenced() 3 | } 4 | -------------------------------------------------------------------------------- /100/src_002_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | // Create an script engine manager 2 | ScriptEngineManager manager = new ScriptEngineManager(); 3 | -------------------------------------------------------------------------------- /100/src_003_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | // Get the reference of the Groovy engine 2 | ScriptEngine engine = 3 | manager.getEngineByName("Groovy"); 4 | -------------------------------------------------------------------------------- /100/src_004_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | // Store a Groovy script in a String 2 | String script = "println('Hello Scripting!')"; 3 | -------------------------------------------------------------------------------- /100/src_005_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | // Store a Groovy script in a String 2 | String script = "println(\"Hello Scripting!\")"; 3 | -------------------------------------------------------------------------------- /100/src_006_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | // Store a Groovy script in a String 2 | String script = """println("Hello Scripting!")"""; 3 | -------------------------------------------------------------------------------- /100/src_007_Executing_Your_First_Script.java: -------------------------------------------------------------------------------- 1 | try { 2 | engine.eval(script); 3 | } catch (ScriptException e) { 4 | e.printStackTrace(); 5 | } 6 | -------------------------------------------------------------------------------- /100/src_008_Passing_Parameters.java: -------------------------------------------------------------------------------- 1 | // Store the script in a String 2 | String script = "println(msg)"; 3 | -------------------------------------------------------------------------------- /100/src_009_Passing_Parameters.java: -------------------------------------------------------------------------------- 1 | // Store the value of the msg parameter in the engine 2 | engine.put("msg", "Hello from Java program"); 3 | -------------------------------------------------------------------------------- /100/src_011_Passing_Parameters.java: -------------------------------------------------------------------------------- 1 | String script = "println(msg.toString())"; 2 | -------------------------------------------------------------------------------- /100/src_012_Passing_Parameters.java: -------------------------------------------------------------------------------- 1 | String script = "year = 1969"; 2 | -------------------------------------------------------------------------------- /100/src_013_Passing_Parameters.java: -------------------------------------------------------------------------------- 1 | Object year = engine.get("year"); 2 | -------------------------------------------------------------------------------- /100/src_015_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Create a script context 2 | ScriptContext ctx = new SimpleScriptContext(); 3 | -------------------------------------------------------------------------------- /100/src_016_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Get the reader and writers from the script context 2 | Reader inputReader = ctx.getReader(); 3 | Writer outputWriter = ctx.getWriter(); 4 | Writer errWriter = ctx.getErrorWriter(); 5 | // Write all script outputs to an out.txt file 6 | Writer fileWriter = new FileWriter("out.txt"); 7 | ctx.setWriter(fileWriter); 8 | -------------------------------------------------------------------------------- /100/src_017_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Add three key-value pairs to the engine scope bindings 2 | ctx.setAttribute("year", 1969, ScriptContext.ENGINE_SCOPE); 3 | ctx.setAttribute("month", 9, ScriptContext.ENGINE_SCOPE); 4 | ctx.setAttribute("day", 19, ScriptContext.ENGINE_SCOPE); 5 | -------------------------------------------------------------------------------- /100/src_018_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Add a global scope Bindings to the context 2 | Bindings globalBindings = new SimpleBindings(); 3 | ctx.setBindings(globalBindings, 4 | ScriptContext.GLOBAL_SCOPE); 5 | -------------------------------------------------------------------------------- /100/src_019_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Add two key-value pairs to the global scope bindings 2 | ctx.setAttribute("year", 1982, 3 | ScriptContext.GLOBAL_SCOPE); 4 | ctx.setAttribute("name", "Boni", 5 | ScriptContext.GLOBAL_SCOPE); 6 | -------------------------------------------------------------------------------- /100/src_020_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Get the value of the key year without specifying the 2 | // scope. It returns 1969 from the Bindings in the engine 3 | // scope. 4 | int yearValue = (Integer) ctx.getAttribute("year"); 5 | -------------------------------------------------------------------------------- /100/src_021_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Get the value of the key named name without specifying 2 | // the scope. 3 | // It returns "Boni" from the Bindings in the global scope. 4 | String nameValue = (String) ctx.getAttribute("name"); 5 | -------------------------------------------------------------------------------- /100/src_022_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Assigns 1969 to engineScopeYear and 1982 to 2 | // globalScopeYear 3 | int engineScopeYear = (Integer) ctx.getAttribute( 4 | "year", ScriptContext.ENGINE_SCOPE); 5 | int globalScopeYear = (Integer) ctx.getAttribute( 6 | "year", ScriptContext.GLOBAL_SCOPE); 7 | -------------------------------------------------------------------------------- /100/src_025_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | ScriptEngine engine1 = null; // get an engine 2 | // Add an "engineName" key with its value as "Engine-1" 3 | // to the engine scope Bindings of the default context 4 | // of engine1 5 | engine1.put("engineName", "Engine-1"); 6 | -------------------------------------------------------------------------------- /100/src_026_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | String eName = (String) engine1.get("engineName"); 2 | -------------------------------------------------------------------------------- /100/src_027_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | Bindings e1Global = 2 | engine1.getBindings(ScriptContext.GLOBAL_SCOPE); 3 | -------------------------------------------------------------------------------- /100/src_028_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | e1Global.put("id", 89999); 2 | -------------------------------------------------------------------------------- /100/src_030_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Create a Bindings, add two key-value pairs to it, and 2 | // set it as the new Bindings for the manager 3 | Bindings newGlobal = new SimpleBindings(); 4 | newGlobal.put("n3", 300); 5 | newGlobal.put("n4", 400); 6 | manager.setBindings(newGlobal); 7 | -------------------------------------------------------------------------------- /100/src_031_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Create a new ScriptEngine 2 | ScriptEngine engine3 = manager.getEngineByName( 3 | "Groovy"); 4 | -------------------------------------------------------------------------------- /100/src_032_Advanced_Parameter_Passing_Techniques.java: -------------------------------------------------------------------------------- 1 | // Set a new Bindings for the global scope of engine1 2 | Bindings newGlobalEngine1 = new SimpleBindings(); 3 | newGlobalEngine1.put("n5", 500); 4 | newGlobalEngine1.put("n6", 600); 5 | engine1.setBindings(newGlobalEngine1, 6 | ScriptContext.GLOBAL_SCOPE); 7 | -------------------------------------------------------------------------------- /100/src_036_Sending_Script_Output_to_a_File.java: -------------------------------------------------------------------------------- 1 | // Create a FileWriter 2 | FileWriter writer = new FileWriter("output.txt"); 3 | // Get the default context of the engine 4 | ScriptContext defaultCtx = engine.getContext(); 5 | // Set the output writer for the default context of the 6 | // engine 7 | defaultCtx.setWriter(writer); 8 | -------------------------------------------------------------------------------- /100/src_038_Invoking_Procedures_in_Scripts.java: -------------------------------------------------------------------------------- 1 | Invocable inv = (Invocable) engine; 2 | -------------------------------------------------------------------------------- /100/src_039_Invoking_Procedures_in_Scripts.java: -------------------------------------------------------------------------------- 1 | // Declare a function named add that adds two numbers 2 | String script = "def add(n1, n2) { n1 + n2 }"; 3 | // Evaluate the function. Call to eval() does not invoke 4 | // the function. It just compiles it. 5 | engine.eval(script); 6 | -------------------------------------------------------------------------------- /100/src_040_Invoking_Procedures_in_Scripts.java: -------------------------------------------------------------------------------- 1 | // Invoke the add function with 30 and 40 as the function's 2 | // arguments. It is as if you called add(30, 40) in the 3 | // script. 4 | Object result = inv.invokeFunction("add", 30, 40); 5 | -------------------------------------------------------------------------------- /100/src_041_Invoking_Procedures_in_Scripts.java: -------------------------------------------------------------------------------- 1 | Object invokeMethod(Object objectRef, String name, 2 | Object... args) 3 | -------------------------------------------------------------------------------- /100/src_043_Implementing_Java_Interfaces_in_Scripts.java: -------------------------------------------------------------------------------- 1 | int sum = calc.add(15, 10); 2 | -------------------------------------------------------------------------------- /100/src_044_Implementing_Java_Interfaces_in_Scripts.java: -------------------------------------------------------------------------------- 1 | // Get the reference of the global script object calc 2 | Object calc = engine.get("calculator"); 3 | // Get the implementation of the Calculator interface 4 | Calculator calculator = 5 | inv.getInterface(calc, Calculator.class); 6 | -------------------------------------------------------------------------------- /100/src_046_Using_Compiled_Scripts.java: -------------------------------------------------------------------------------- 1 | // Cast the engine reference to the Compilable type 2 | Compilable comp = (Compilable) engine; 3 | -------------------------------------------------------------------------------- /100/src_047_Implementing_a_Script_Engine.java: -------------------------------------------------------------------------------- 1 | op1 operation op2 2 | -------------------------------------------------------------------------------- /100/src_048_Implementing_a_Script_Engine.java: -------------------------------------------------------------------------------- 1 | Expression exp = new Expression(expression, scriptContext); 2 | Double value = exp.eval(); 3 | -------------------------------------------------------------------------------- /100/src_049_Implementing_a_Script_Engine.java: -------------------------------------------------------------------------------- 1 | Expression exp = new Expression(script, context); 2 | Object result = exp.eval(); 3 | -------------------------------------------------------------------------------- /100/src_052_JavaFX_in_Groovy.java: -------------------------------------------------------------------------------- 1 | btn.setText("Say 'Hello World'"); 2 | -------------------------------------------------------------------------------- /100/src_053_JavaFX_in_Groovy.java: -------------------------------------------------------------------------------- 1 | btn.onAction = { def event -> 2 | println("Hello World!") 3 | } 4 | -------------------------------------------------------------------------------- /110/src_001_Knowing_the_Runtime_Environment.java: -------------------------------------------------------------------------------- 1 | // Get the instance of the Runtime 2 | Runtime runtime = Runtime.getRuntime(); 3 | -------------------------------------------------------------------------------- /110/src_002_Knowing_the_Runtime_Environment.java: -------------------------------------------------------------------------------- 1 | Used Memory = Total Memory - Free Memory 2 | -------------------------------------------------------------------------------- /110/src_003_The_Current_Process.java: -------------------------------------------------------------------------------- 1 | // Get the handle of the current process 2 | ProcessHandle current = ProcessHandle.current(); 3 | -------------------------------------------------------------------------------- /110/src_004_Creating_a_Process.sh: -------------------------------------------------------------------------------- 1 | /opt/jdk17/bin/java --version 2 | -------------------------------------------------------------------------------- /110/src_005_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | ProcessBuilder pb = new ProcessBuilder( 2 | "/opt/jdk17/bin/java", "--version"); 3 | -------------------------------------------------------------------------------- /110/src_006_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | ProcessBuilder pb = new ProcessBuilder() 2 | .command("/opt/jdk17/bin/java", 3 | "--module-path", 4 | "myModulePath", 5 | "--module", 6 | "myModule/className"); 7 | 8 | -------------------------------------------------------------------------------- /110/src_007_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | ProcessBuilder pb = new ProcessBuilder("mycommand"); 2 | Map env = pb.environment(); 3 | env.put("arg1", "value1"); 4 | env.put("arg2", "value2"); 5 | -------------------------------------------------------------------------------- /110/src_008_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | ProcessBuilder pb = new ProcessBuilder("myCommand") 2 | .directory(new File("/home/USER/mydir")); 3 | -------------------------------------------------------------------------------- /110/src_009_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Start a new process 2 | Process newProcess = pb.start(); 3 | -------------------------------------------------------------------------------- /110/src_010_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Get the process handle 2 | ProcessHandle handle = newProcess.toHandle(); 3 | -------------------------------------------------------------------------------- /110/src_012_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Configure the ProcessBuilder 2 | ProcessBuilder pb = 3 | new ProcessBuilder(javaPath, "-version") 4 | .redirectErrorStream(true); 5 | 6 | -------------------------------------------------------------------------------- /110/src_013_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Configure the ProcessBuilder inheriting parent's I/O 2 | ProcessBuilder pb = 3 | new ProcessBuilder(javaPath, "--version") 4 | .inheritIO(); 5 | -------------------------------------------------------------------------------- /110/src_014_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Configure the ProcessBuilder inheriting parent's I/O 2 | ProcessBuilder pb = 3 | new ProcessBuilder(javaPath, "--version") 4 | .redirectInput(ProcessBuilder.Redirect.INHERIT) 5 | .redirectOutput(ProcessBuilder.Redirect.INHERIT) 6 | .redirectError(ProcessBuilder.Redirect.INHERIT); 7 | 8 | -------------------------------------------------------------------------------- /110/src_015_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Configure the ProcessBuilder 2 | ProcessBuilder pb = 3 | new ProcessBuilder(javaPath, "--version") 4 | .redirectOutput( 5 | ProcessBuilder.Redirect.to( 6 | new File("java_product_details.txt"))); 7 | -------------------------------------------------------------------------------- /110/src_016_Creating_a_Process.sh: -------------------------------------------------------------------------------- 1 | JDK_HOME/bin/java --module-path \ 2 | --module jdojo.process/com.jdojo.process.Job \ 3 | 4 | -------------------------------------------------------------------------------- /110/src_017_Creating_a_Process.sh: -------------------------------------------------------------------------------- 1 | JDK_HOME/bin/java \ 2 | -class-path \ 3 | com.jdojo.process.Job \ 4 | 5 | -------------------------------------------------------------------------------- /110/src_018_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Start a process that runs for 15 seconds 2 | Process p = Job.startProcess(5, 15); 3 | -------------------------------------------------------------------------------- /110/src_019_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Get the handle of the current process 2 | ProcessHandle handle = p.toHandle(); 3 | // Print the process details 4 | CurrentProcessInfo.printInfo(handle); 5 | -------------------------------------------------------------------------------- /110/src_020_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | CompletableFuture future = handle.onExit(); 2 | // Print a message when process terminates 3 | future.thenAccept((ProcessHandle ph) -> { 4 | System.out.printf( 5 | "Job (pid=%d) terminated.%n", ph.pid()); 6 | }); 7 | -------------------------------------------------------------------------------- /110/src_021_Creating_a_Process.java: -------------------------------------------------------------------------------- 1 | // Wait for the process to terminate 2 | future.get(); 3 | -------------------------------------------------------------------------------- /110/src_022_Obtaining_a_Process_Handle.java: -------------------------------------------------------------------------------- 1 | // Get the process handle of the process with the pid 2 | // of 1234 3 | Optional handle = ProcessHandle.of(1234L); 4 | -------------------------------------------------------------------------------- /110/src_023_Obtaining_a_Process_Handle.java: -------------------------------------------------------------------------------- 1 | System.out.printf("All processes PIDs:%n"); 2 | ProcessHandle.allProcesses() 3 | .map(ph -> ph.pid()) 4 | .sorted() 5 | .forEach(System.out::println); 6 | -------------------------------------------------------------------------------- /120/src_001_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | - jar-root 2 | - C1.class 3 | - C2.class 4 | - C3.class 5 | - C4.class 6 | - META-INF 7 | - MANIFEST.MF 8 | -------------------------------------------------------------------------------- /120/src_002_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | - jar-root 2 | - C1.class 3 | - C2.class 4 | - C3.class 5 | - C4.class 6 | - META-INF 7 | - MANIFEST.MF 8 | - versions 9 | - 16 10 | - C2.class 11 | - C5.class 12 | - 17 13 | - C1.class 14 | - C2.class 15 | - C6.class 16 | -------------------------------------------------------------------------------- /120/src_003_The_JAR_Format.java: -------------------------------------------------------------------------------- 1 | jar --release N 2 | -------------------------------------------------------------------------------- /120/src_004_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --create --file mrjars\jdojo.mrjar.jar ^ 2 | -C jdojo.mrjar.jdk8\build\classes . ^ 3 | --release 17 -C build\modules\jdojo.mrjar . 4 | -------------------------------------------------------------------------------- /120/src_007_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --create --verbose ^ 2 | --file mrjars\jdojo.mrjar.jar ^ 3 | -C jdojo.mrjar.jdk8\build\classes . ^ 4 | --release 17 -C build\modules\jdojo.mrjar . ^ 5 | --release 16 -C jdojo.mrjar.jdk16\modules\jdojo.mrjar . 6 | -------------------------------------------------------------------------------- /120/src_009_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --create --file mrjars\jdojo.mrjar.jar ^ 2 | -C jdojo.mrjar.jdk8\build\classes . 3 | -------------------------------------------------------------------------------- /120/src_010_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --update ^ 2 | --file mrjars\com.jdojo.mrjar.jar ^ 3 | --release 17 -C com.jdojo.mrjar.jdk17\build\classes . 4 | C:\jdk17book>jar --update ^ 5 | --file mrjars\jdojo.mrjar.jar ^ 6 | --release 17 -C build\modules\jdojo.mrjar . 7 | -------------------------------------------------------------------------------- /120/src_011_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>C:\java8\bin\java ^ 2 | -classpath mrjars\jdojo.mrjar.jar ^ 3 | com.jdojo.mrjar.Main 4 | 5 | Inside JDK 8 version of Main.main()... 6 | Creating JDK 8 version of TimeUtil... 7 | Local Date: 2021-09-22 8 | -------------------------------------------------------------------------------- /120/src_012_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>C:\java17\bin\java ^ 2 | --module-path mrjars\jdojo.mrjar.jar ^ 3 | --module jdojo.mrjar/com.jdojo.mrjar.Main 4 | 5 | Inside JDK 17 version of Main.main()... 6 | Creating JDK 17 version of TimeUtil... 7 | Local Date: 2021-09-22 8 | -------------------------------------------------------------------------------- /120/src_013_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --create ^ 2 | --file mrjars\jdojo.mrjar2.jar ^ 3 | -C jdojo.mrjar.jdk8\build\classes . ^ 4 | --release 17 ^ 5 | -C build\modules\jdojo.mrjar ^ 6 | module-info.class ^ 7 | -C build\modules\jdojo.mrjar ^ 8 | com\jdojo\mrjar\TimeUtil.class 9 | -------------------------------------------------------------------------------- /120/src_014_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jar --list --file mrjars\jdojo.mrjar2.jar 2 | 3 | META-INF/ 4 | META-INF/MANIFEST.MF 5 | META-INF/versions/17/module-info.class 6 | com/ 7 | com/jdojo/ 8 | com/jdojo/mrjar/ 9 | com/jdojo/mrjar/Main.class 10 | com/jdojo/mrjar/TimeUtil.class 11 | META-INF/versions/17/com/jdojo/mrjar/TimeUtil.class 12 | -------------------------------------------------------------------------------- /120/src_015_The_JAR_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>C:\java17\bin\java ^ 2 | --module-path mrjars\jdojo.mrjar2.jar ^ 3 | --module jdojo.mrjar/com.jdojo.mrjar.Main 4 | 5 | Inside JDK 8 version of Main.main()... 6 | Creating JDK 17 version of TimeUtil... 7 | Local Date: 2021-09-22 8 | -------------------------------------------------------------------------------- /120/src_016_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | entry: META-INF/versions/17/com/jdojo/test/Test.class, 2 | contains a new public class not found 3 | in base entries 4 | invalid multi-release jar file mrjars\jdojo.mrjar.jar 5 | deleted 6 | -------------------------------------------------------------------------------- /120/src_017_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | jar:file:/C:/jdk17book/mrjars/jdojo.mrjar.jar! 2 | com/jdojo/mrjar/TimeUtil.class 3 | -------------------------------------------------------------------------------- /120/src_018_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | jar:file:/C:/jdk17book/mrjars/jdojo.mrjar.jar! 2 | /META-INF/versions/17/com/jdojo/mrjar/TimeUtil.class 3 | -------------------------------------------------------------------------------- /120/src_019_The_JAR_Format.misc: -------------------------------------------------------------------------------- 1 | Multi-Release: true 2 | -------------------------------------------------------------------------------- /120/src_020_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | jmod 2 | -------------------------------------------------------------------------------- /120/src_021_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book\jmods 2 | C:\jdk17book\dist\jdojo.javafx.jar 3 | -------------------------------------------------------------------------------- /120/src_022_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod create ^ 2 | --class-path dist\jdojo.javafx.jar ^ 3 | jmods\jdojo.javafx.jmod 4 | -------------------------------------------------------------------------------- /120/src_023_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod create --module-version 1.0 ^ 2 | --class-path build\modules\jdojo.javafx ^ 3 | jmods\jdojo.javafx.jmod 4 | -------------------------------------------------------------------------------- /120/src_024_The_JMOD_Format.misc: -------------------------------------------------------------------------------- 1 | Error occurred during initialization of VM 2 | java.lang.module.ResolutionException: 3 | JMOD files not supported: jmods\jdojo.javafx.jmod 4 | ... 5 | Extracting JMOD File Contents 6 | -------------------------------------------------------------------------------- /120/src_025_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod extract --dir extracted ^ 2 | jmods\jdojo.javafx.jmod 3 | -------------------------------------------------------------------------------- /120/src_026_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod list jmods\jdojo.javafx.jmod 2 | 3 | classes/module-info.class 4 | classes/com/jdojo/javafx/BindingTest.class 5 | ... 6 | classes/resources/fxml/sayhello.fxml 7 | -------------------------------------------------------------------------------- /120/src_028_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod describe jmods\jdojo.javafx.jmod 2 | 3 | jdojo.javafx@1.0 4 | exports com.jdojo.javafx 5 | requires java.base mandated 6 | requires javafx.controls 7 | requires javafx.fxml 8 | contains resources.fxml 9 | -------------------------------------------------------------------------------- /120/src_029_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod describe C:\java17\jmods\java.sql.jmod 2 | 3 | java.sql@9.0.1 4 | exports java.sql 5 | exports javax.sql 6 | exports javax.transaction.xa 7 | requires java.base mandated 8 | requires java.logging transitive 9 | requires java.xml transitive 10 | uses java.sql.Driver 11 | platform windows-amd64 12 | -------------------------------------------------------------------------------- /120/src_031_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod hash ^ 2 | --module-path jmods ^ 3 | --hash-modules jdojo.prime.? jmods\jdojo.prime.jmod 4 | 5 | Hashes are recorded in module jdojo.prime 6 | -------------------------------------------------------------------------------- /120/src_033_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod create --module-version 1.0 ^ 2 | --module-path jmods ^ 3 | --hash-modules jdojo.prime.? ^ 4 | --class-path build\modules\jdojo.prime ^ 5 | jmods\jdojo.prime.jmod 6 | -------------------------------------------------------------------------------- /120/src_034_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod create --module-version 1.0 ^ 2 | --module-path jmods ^ 3 | --class-path build\modules\jdojo.prime ^ 4 | jmods\jdojo.prime.jmod 5 | -------------------------------------------------------------------------------- /120/src_036_The_JMOD_Format.sh: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jmod describe jmods\jdojo.prime.jmod 2 | 3 | jdojo.prime@1.0 4 | exports com.jdojo.prime 5 | requires java.base mandated 6 | uses com.jdojo.prime.PrimeChecker 7 | provides com.jdojo.prime.PrimeChecker with 8 | com.jdojo.prime.impl.genericprimechecker 9 | contains com.jdojo.prime.impl 10 | -------------------------------------------------------------------------------- /130/src_001_Creating_Custom_Runtime_Images.java: -------------------------------------------------------------------------------- 1 | jlink --module-path ^ 2 | --add-modules --output 3 | -------------------------------------------------------------------------------- /130/src_002_Creating_Custom_Runtime_Images.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jlink ^ 2 | --module-path jmods;C:\java17\jmods ^ 3 | --add-modules ^ 4 | jdojo.prime.client,jdojo.prime.faster, 5 | jdojo.prime.probable ^ 6 | --launcher runprimechecker= 7 | jdojo.prime.client/com.jdojo.prime.client.Main ^ 8 | --output image\primechecker 9 | -------------------------------------------------------------------------------- /130/src_003_Creating_Custom_Runtime_Images.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>image\primechecker\bin\java ^ 2 | --list-modules 3 | 4 | java.base@9 5 | jdojo.prime@1.0 6 | jdojo.prime.client@1.0 7 | jdojo.prime.faster@1.0 8 | jdojo.prime.probable@1.0 9 | -------------------------------------------------------------------------------- /130/src_004_Creating_Custom_Runtime_Images.misc: -------------------------------------------------------------------------------- 1 | Error: Module java.base not found, 2 | required by jdojo.prime.probable 3 | -------------------------------------------------------------------------------- /130/src_006_Creating_Custom_Runtime_Images.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>image\primechecker\bin\java ^ 2 | --module jdojo.prime.client/com.jdojo.prime.client.Main 3 | -------------------------------------------------------------------------------- /130/src_009_Binding_Services.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jlink ^ 2 | --module-path jmods;C:\java17\jmods ^ 3 | --add-modules jdojo.prime.client ^ 4 | --launcher runprimechecker= 5 | jdojo.prime.client/com.jdojo.prime.client.Main ^ 6 | --bind-services 7 | --output image\primecheckerservice 8 | -------------------------------------------------------------------------------- /130/src_012_Using_Plugins_with_the_jlink_Tool.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jlink --list-plugins 2 | 3 | ... 4 | -------------------------------------------------------------------------------- /130/src_014_The_jimage_Tool.java: -------------------------------------------------------------------------------- 1 | jimage 2 | -------------------------------------------------------------------------------- /130/src_015_The_jimage_Tool.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jimage extract ^ 2 | --dir extracted_jdk C:\java17\lib\modules 3 | -------------------------------------------------------------------------------- /130/src_016_The_jimage_Tool.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jimage extract ^ 2 | --include regex:.+\.png ^ 3 | --dir extracted_images ^ 4 | C:\java17\lib\modules 5 | -------------------------------------------------------------------------------- /130/src_017_The_jimage_Tool.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jimage list C:\java17\lib\modules 2 | 3 | jimage: C:\java17\lib\modules 4 | Module: java.activation 5 | META-INF/mailcap.default 6 | META-INF/mimetypes.default 7 | ... 8 | Module: java.annotations.common 9 | javax/annotation/Generated.class 10 | ... 11 | -------------------------------------------------------------------------------- /130/src_019_The_jimage_Tool.java: -------------------------------------------------------------------------------- 1 | C:\jdk17book>jimage verify ^ 2 | image\primechecker\lib\modules 3 | 4 | jimage: primechecker\lib\modules 5 | Error(s) in Class: /jdojo.prime.client/com/jdojo/prime/ 6 | client/Main2.class 7 | -------------------------------------------------------------------------------- /140/src_001_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | // This is JavaScript 2 | var a = "Hello"; 3 | var b = 3; 4 | var c = 5.9; 5 | -------------------------------------------------------------------------------- /140/src_002_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | // This is Java, inside a method 2 | var a = "Hello"; 3 | var b = 3; 4 | var c = 5.9; 5 | -------------------------------------------------------------------------------- /140/src_003_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | public class Car { 2 | private var a = "Hello"; 3 | // <- Won't compile 4 | } 5 | 6 | ... 7 | 8 | var a; 9 | a = 3; 10 | // <- Won't compile 11 | 12 | ... 13 | 14 | var b = 3; 15 | b = 5.9; 16 | // <- Won't compile 17 | -------------------------------------------------------------------------------- /140/src_004_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | var x = new ArrayList(); 2 | // <- x has type ArrayList 3 | 4 | Function fsi = (var s) -> s.length(); 5 | -------------------------------------------------------------------------------- /140/src_005_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | java HelloTest.java 2 | # or, if we need args 3 | java HelloTest.java arg1 arg2 ... 4 | -------------------------------------------------------------------------------- /140/src_006_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | int x = ...; 2 | switch(x) { 3 | case 1: 4 | case 2: 5 | System.out.println("1 or 2"); 6 | break; 7 | case 3: 8 | System.out.println("3"); 9 | break; 10 | default: 11 | System.out.println("default"); 12 | } 13 | -------------------------------------------------------------------------------- /140/src_008_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | String s = "This is the first line\n" + 2 | "This is the second line\n" + 3 | "This is the third line"; 4 | -------------------------------------------------------------------------------- /140/src_009_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | String s = """ 2 | This is the first line 3 | This is the second line 4 | This is the third line 5 | """; 6 | -------------------------------------------------------------------------------- /140/src_010_More_JDK_17_Novelties.misc: -------------------------------------------------------------------------------- 1 | This is the first line 2 | This is the second line 3 | This is the third line 4 | -------------------------------------------------------------------------------- /140/src_011_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | String s = """ 2 | This is the first line 3 | This is the second line 4 | This is the third line 5 | """; 6 | -------------------------------------------------------------------------------- /140/src_012_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | String s = """ 2 | This is the first line 3 | This is the second line 4 | This is the third line 5 | """.stripIndent(); 6 | 7 | Output: 8 | This is the first line 9 | This is the second line 10 | This is the third line 11 | -------------------------------------------------------------------------------- /140/src_013_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | String s = """ 2 | This is the first line \ 3 | Still inside the same line \ 4 | Still inside the same line 5 | """; 6 | -------------------------------------------------------------------------------- /140/src_014_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | Object s = "Hello"; 2 | ... 3 | if(s instanceof String) { 4 | String str = (String) s; 5 | if(str.equalsIgnoreCase("hello")) { 6 | System.out.println("Hello String!"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /140/src_015_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | Object s = "Hello"; 2 | ... 3 | if(s instanceof String str) { 4 | // use local variable 'str', which 5 | // is of type String 6 | } 7 | ... 8 | if(s instanceof String str && 9 | str.equalsIgnoreCase("hello")) { 10 | System.out.println("Hello String!"); 11 | } 12 | -------------------------------------------------------------------------------- /140/src_017_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | private String firstName; 2 | private String lastName; 3 | private LocalDate birthDay; 4 | private String socialSecurityNumber; 5 | -------------------------------------------------------------------------------- /140/src_018_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | // File Person.java 2 | record Person( 3 | String firstName, 4 | String lastName, 5 | LocalDate birthDay, 6 | String socialSecurityNumber) {} 7 | -------------------------------------------------------------------------------- /140/src_019_More_JDK_17_Novelties.java: -------------------------------------------------------------------------------- 1 | Person p1 = new Person( 2 | "John", 3 | "Smith", 4 | LocalDate.of(1997,Month.DECEMBER,30), 5 | "000-00-1234"); 6 | System.out.println("Name: " + p1.firstName + " " + p1.lastName); 7 | -------------------------------------------------------------------------------- /9781484271346.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/more-java-17/20b2e8a6e371fffe58c97f3d02d48f98506c8315/9781484271346.jpg -------------------------------------------------------------------------------- /990/src_001_Exercises_in_Chapter_1.java: -------------------------------------------------------------------------------- 1 | [modifiers] @ interface { 2 |     // Annotation type body goes here 3 | } 4 | -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *More Java 17* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** --------------------------------------------------------------------------------