@@ -86,3 +86,335 @@ Path is an environment variable.
8686- back up of the content of your path.
8787
8888Restart command prompt
89+
90+
91+ ## Minor Things
92+ ```
93+ // Java 10
94+ // List.copyOf, Set.copyOf, and Map.copyOf methods
95+ var list = new ArrayList<>(List.of("One", "Two", "Three"));
96+ var immutableList = List.copyOf(list);
97+ System.out.println(immutableList);
98+
99+ // JDK 11 - Files class - readString and writeString
100+ Path pathFileToRead = Paths.get("./resources/data.txt");
101+ String readString = Files.readString(pathFileToRead);
102+ System.out.println(readString);
103+
104+ // JDK 11 - Predicate.not
105+ Predicate<Integer> isEven = x -> (x % 2 == 0);
106+ List<Integer> myNumbers = List.of(1, 5, 8, 9);
107+ myNumbers.stream().filter(isEven.negate()).forEach(System.out::println);
108+ myNumbers.stream().filter(Java10::isEven).forEach(System.out::println);
109+ myNumbers.stream().filter(Predicate.not(Java10::isEven)).forEach(System.out::println);
110+
111+ // JDK 11 - Directly run Java file without compiling - java HelloWorld.java
112+
113+ // JDK 12
114+ // Compact Number Formatting
115+ // NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US,
116+ // NumberFormat.Style.SHORT);
117+ // String result = fmt.format(1000);
118+
119+ // JDK 11
120+
121+ // String methods - isBlank, lines, strip, stripLeading, stripTrailing, and
122+ // repeat
123+ // "".isBlank(),"abc".isBlank()
124+ // "1\n2\n3\n".lines().forEach(System.out::println);//Split to streams
125+ // System.out.println(" 1 2 3 ".strip());
126+ // System.out.println(" 1 2 3 ".stripLeading());
127+ // System.out.println(" 1 2 3 ".stripTrailing());
128+ // System.out.println("abc".repeat(10));
129+
130+ // JDK 12
131+ // String methods - indent and transform
132+ System.out.println("sdfalkjfl".transform(s -> s.substring(2)));
133+
134+ // JDK 13
135+ // java.lang.String - formatted
136+ System.out.println("My name is %s".formatted("Ranga"));
137+
138+ // JDK 14
139+ // Helpful NullPointerExceptions (JEP 358)
140+ ```
141+
142+ ## Modularization
143+
144+ ```
145+ Package com.in28minutes.sorting.util
146+
147+ public class MySortingUtil {
148+ public List<String> sort(List<String> values){
149+ BubbleSort sort = new BubbleSort();
150+ return sort.sort(values);
151+ }
152+
153+ }
154+
155+
156+ Package com.in28minutes.sorting.algorithm
157+ public class BubbleSort {
158+
159+ public List<String> sort(List<String> values) {
160+ return values;
161+ }
162+
163+ }
164+
165+
166+ Package com.in28minutes.consumer
167+ public class SortingUtilConsumer {
168+
169+ private static Logger logger = Logger.getLogger(SortingUtilConsumer.class.getName());
170+
171+ public static void main(String[] args) {
172+ MySortingUtil util = new MySortingUtil();
173+ List<String> sorted = util.sort(Arrays.asList("Apple", "Bat", "Cat"));
174+ System.out.println(sorted);
175+ logger.info(sorted.toString());
176+ }
177+
178+ }
179+
180+ package com.in28minutes.consumer
181+
182+ public class DirectConsumer {
183+
184+ public static void main(String[] args) {
185+ BubbleSort util = new BubbleSort();
186+ List<String> sorted = util.sort(Arrays.asList("Apple", "Bat", "Cat"));
187+ System.out.println(sorted);
188+ }
189+
190+ }
191+
192+
193+ JAR Approach
194+ sorting-jar
195+ consumer-jar
196+
197+ vs
198+
199+ Modularization Approach
200+ sorting-module
201+ consumer-module
202+
203+ module consumer.module {
204+ requires sorting.module;
205+ requires java.logging;
206+ }
207+
208+ module sorting.module {
209+ exports first.module.util;
210+ }
211+ ```
212+
213+
214+ ## Type Inference
215+
216+ ```
217+ // Type Inference - Java 10
218+
219+ // List<String> numbers = new ArrayList<String>(list);
220+ // List<String> numbers = new ArrayList<>(list);
221+ var numbers = new ArrayList<>(list);
222+ numbers.forEach(s -> System.out.println(s));
223+
224+ for (var i = 0; i <= 10; i++) {
225+ System.out.println(i);
226+ }
227+
228+ for (var value : list) {
229+ System.out.println(value);
230+ }
231+
232+ // Good variable names
233+ // Minimize Scope
234+ // Improve readability for chained expressions
235+ ```
236+
237+ ## Switch Expression
238+
239+ ```
240+ public String determinenameofMonthWithSwitch(int monthNumber) {
241+ switch (monthNumber) {
242+ case 1:
243+ return "January";
244+ case 2:
245+ return "February";
246+ case 3:
247+ return "March";
248+ case 4:
249+ return "April";
250+ case 5:
251+ return "May";
252+ case 6:
253+ return "June";
254+ case 7:
255+ return "July";
256+ case 8:
257+ return "August";
258+ case 9:
259+ return "September";
260+ case 10:
261+ return "October";
262+ case 11:
263+ return "November";
264+ case 12:
265+ return "December";
266+ default:
267+ return "Invalid Month";
268+ }
269+ }
270+
271+ public String determinenameofMonthWithSwitchLabeledRule(int monthNumber) {
272+ // No fallthrough
273+ String monthName = switch (monthNumber) {
274+ case 1 -> {
275+ System.out.println("January");
276+ // yield statement is used in a Switch Expression
277+ // break,continue statements are used in a Switch Statement
278+ yield "January"; // yield mandatory!
279+ }
280+ case 2 -> "February";
281+ case 3 -> "March";
282+ case 4 -> "April";
283+ case 5 -> "May";
284+ case 6 -> "June";
285+ case 7 -> "July";
286+ case 8 -> "August";
287+ case 9 -> "September";
288+ case 10 -> "October";
289+ case 11 -> "November";
290+ case 12 -> "December";
291+ default -> "Invalid Month";
292+ };
293+
294+ return monthName;
295+ }
296+
297+ public String determinenameofMonthWithSwitchYield(int monthNumber) {
298+ // No fallthrough
299+ String monthName = switch (monthNumber) {
300+ case 1:
301+ System.out.println("January");
302+ yield "January";
303+ case 2:
304+ yield "February";
305+ case 3:
306+ yield "March";
307+ case 4:
308+ yield "April";
309+ case 5:
310+ yield "May";
311+ case 6:
312+ yield "June";
313+ case 7:
314+ yield "July";
315+ case 8:
316+ yield "August";
317+ case 9:
318+ yield "September";
319+ case 10:
320+ yield "October";
321+ case 11:
322+ yield "November";
323+ case 12:
324+ yield "December";
325+ default:
326+ yield "Invalid Month";
327+ };
328+
329+ return monthName;
330+ }
331+ ```
332+ ## Text Blocks
333+ ```
334+ // JEP 355 Text Blocks -
335+
336+ System.out.println("First Line\nSecond Line\nThird Line");
337+ System.out.println("""
338+ First Line
339+ Second Line
340+ Third Line""");
341+
342+ System.out.println("\"First Line\"\nSecond Line\nThird Line");
343+ System.out.println("""
344+ "First Line"
345+ Second Line
346+ Third Line""");
347+
348+ // Recommended Approach
349+ System.out.println("""
350+ First Line
351+ Second Line
352+ Third Line
353+ """);
354+
355+ String formattedString = """
356+ Name: %s
357+ Email: %s
358+ Phone: %s
359+ """.formatted("Ranga", "ranga@in28minutes.com", "123-456-7890");
360+
361+ System.out.print(formattedString);
362+ ```
363+
364+ ## Records
365+
366+ ```
367+ package records.after;
368+
369+ //component fields
370+ //public accessor methods
371+ //canonical constructor
372+ //equals and hashCode
373+ //toString method
374+
375+ //record Person(String name, String email, String phoneNumber) { }
376+
377+ //record Person(String name, String email, String phoneNumber) {
378+ // public Person(String name, String email, String phoneNumber) {
379+ // if(name==null) {
380+ // throw new IllegalArgumentException("Name cannot be null");
381+ // }
382+ //
383+ // this.name = name;
384+ // this.email = email;
385+ // this.phoneNumber = phoneNumber;
386+ // }
387+ //}
388+
389+
390+ record Person(String name, String email, String phoneNumber) {
391+
392+ //compact constructor only allowed inside records
393+ public Person {
394+ if(name==null) {
395+ throw new IllegalArgumentException("Name cannot be null");
396+ }
397+ }
398+
399+ // Explicitly Declaration of Methods
400+ public String email() {
401+ System.out.println("email is " + email);
402+ return email;
403+ }
404+
405+ //You can add static fields, static initializers, and static methods
406+ //But you CANNOT add instance variables or instance initializers
407+ //However, you CAN add instance methods
408+ }
409+
410+
411+ public class RecordsEmployeeRunner {
412+ public static void main(String[] args) {
413+ Person person = new Person("Ranga","ranga@in28minutes.com","123-456-7890");
414+
415+ System.out.println(person);
416+ System.out.println(person.email());
417+
418+ }
419+ }
420+ ```
0 commit comments