diff --git a/new-features-in-java-interface/.classpath b/new-features-in-java-interface/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/new-features-in-java-interface/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/new-features-in-java-interface/.project b/new-features-in-java-interface/.project new file mode 100644 index 0000000..e8023cc --- /dev/null +++ b/new-features-in-java-interface/.project @@ -0,0 +1,17 @@ + + + new-features-in-java-interface + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/new-features-in-java-interface/.settings/org.eclipse.core.resources.prefs b/new-features-in-java-interface/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/new-features-in-java-interface/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/new-features-in-java-interface/.settings/org.eclipse.jdt.core.prefs b/new-features-in-java-interface/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/new-features-in-java-interface/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/new-features-in-java-interface/src/com/example/StudyInterfaceJava8.java b/new-features-in-java-interface/src/com/example/StudyInterfaceJava8.java new file mode 100644 index 0000000..f890f36 --- /dev/null +++ b/new-features-in-java-interface/src/com/example/StudyInterfaceJava8.java @@ -0,0 +1,91 @@ +package com.example; + +import java.util.List; +import java.util.stream.IntStream; + +public class StudyInterfaceJava8 { + + public static void main(String[] args) { + var numbers = List.of(4,8,15,16,23,42); + numbers.stream() + .filter( J::isEven ) + .mapToInt( number -> number * number * number) + .sum(); + + } + +} + +interface I { // before java 8 + public abstract void fun(); + public abstract void gun(); + public abstract void sun(); +} + +interface J { // after java 8+ + public abstract void fun(); + public abstract void gun(); + public abstract void sun(); + // 1. default method + default public void run() { + + }; + // 2. static method + // Functional Programming Utility Method + public static boolean isEven(int number) { + return number%2 == 0; + } +} + +interface K { + default void fun() { + haveFun(); + } + default void sun() { + haveFun(); + } + private void haveFun() {} // since java 9 + public static void haveRun() { + haveGun(); + } + public static void haveRun(int x) { + haveGun(); + } + private static void haveGun() {} // since java 9 +} + +class J1 implements J { + + @Override + public void fun() { } + + @Override + public void gun() { } + + @Override + public void sun() { } + +} + +interface LotteryService { + List draw(int max,int size); + + default List> draw(int max,int size,int column){ + return IntStream.range(0, column) + .mapToObj(i -> draw(max,size)) + .toList(); + } +} + + + + + + + + + + + + + diff --git a/study-arrays/.classpath b/study-arrays/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-arrays/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-arrays/.project b/study-arrays/.project new file mode 100644 index 0000000..393cf47 --- /dev/null +++ b/study-arrays/.project @@ -0,0 +1,17 @@ + + + study-arrays + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-arrays/.settings/org.eclipse.core.resources.prefs b/study-arrays/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-arrays/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-arrays/.settings/org.eclipse.jdt.core.prefs b/study-arrays/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/study-arrays/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-arrays/src/com/example/StudyArray.java b/study-arrays/src/com/example/StudyArray.java new file mode 100644 index 0000000..45160f6 --- /dev/null +++ b/study-arrays/src/com/example/StudyArray.java @@ -0,0 +1,53 @@ +package com.example; + +import java.util.Arrays; + +@SuppressWarnings("unused") +public class StudyArray { + + public static void main(String[] args) { + int x = 42; // scalar + int[] numbers = { 4, 8, 15, 16, 23, 42 }; // Heap + System.out.println(numbers.length); + int[] dizi; + dizi = new int[6]; // Heap -> Zero + dizi[0] = 4; + dizi[1] = 8; + dizi[2] = 15; + dizi[3] = 16; + dizi[4] = 23; + dizi[5] = 42; + int[] array; + array = new int[] { 4, 8, 15, 16, 23, 42 }; // Heap -> Zero + // System.out.println(array[6]); ArrayIndexOutOfBoudsException + // How to print array content + System.out.println(numbers); + System.out.println(Arrays.toString(numbers)); + // Loop #1 + for (int i = 0; i < numbers.length; ++i) + System.out.println(numbers[i]); + // Loop #1 - var + for (var i = 0; i < numbers.length; ++i) + System.out.println(numbers[i]); + // Loop #2 : Java SE 5 - for-each + for (var number : numbers) // safe, sequential, read-only + System.out.println(++number); + var sum = 0; + for (var number : numbers) { + if (number%2 == 1) { // if it is odd + var squared = number * number; + sum += squared; + } + } + + // Loop #3 : Java SE 8 -> Functional Programming + sum = Arrays.stream(numbers) // Stream API + Filter/Map/Reduce -> ForkJoin Framework -> JobStealing Algorithm + .parallel() + .filter(u -> u%2 == 1) + .map(y -> y*y) + .sequential() + .sum(); // Functional Programming: lazy-loading, functions? + // C++20: Ranges -> C++23 + } + +} diff --git a/study-date-time-api/.classpath b/study-date-time-api/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-date-time-api/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-date-time-api/.project b/study-date-time-api/.project new file mode 100644 index 0000000..d2dfd78 --- /dev/null +++ b/study-date-time-api/.project @@ -0,0 +1,17 @@ + + + study-date-time-api + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-date-time-api/.settings/org.eclipse.core.resources.prefs b/study-date-time-api/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..d619b48 --- /dev/null +++ b/study-date-time-api/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/com/example/CreatingDateAndTime.java=UTF-8 +encoding//src/com/example/StudyInternationalization.java=UTF-8 +encoding/=UTF-8 diff --git a/study-date-time-api/.settings/org.eclipse.jdt.core.prefs b/study-date-time-api/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/study-date-time-api/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-date-time-api/src/com/example/CreatingDateAndTime.java b/study-date-time-api/src/com/example/CreatingDateAndTime.java new file mode 100644 index 0000000..acea3a5 --- /dev/null +++ b/study-date-time-api/src/com/example/CreatingDateAndTime.java @@ -0,0 +1,30 @@ +package com.example; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; + +public class CreatingDateAndTime { + + @SuppressWarnings("unused") + public static void main(String[] args) { + var tr = new Locale("tr","TR"); + LocalDate localDate; + LocalTime localTime; + LocalDateTime localDateTime; + ZonedDateTime now = ZonedDateTime.now(); + System.out.println(LocalDate.now()); + System.out.println(LocalTime.now()); + System.out.println(LocalDateTime.now()); + System.out.println(ZonedDateTime.now()); + DateTimeFormatter dtf = + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL) + .withLocale(Locale.CHINA); + System.out.println(dtf.format(now)); + } + +} diff --git a/study-date-time-api/src/com/example/CreatingSpecificDateAndTime.java b/study-date-time-api/src/com/example/CreatingSpecificDateAndTime.java new file mode 100644 index 0000000..3adb1cd --- /dev/null +++ b/study-date-time-api/src/com/example/CreatingSpecificDateAndTime.java @@ -0,0 +1,19 @@ +package com.example; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.Month; + +public class CreatingSpecificDateAndTime { + + @SuppressWarnings("unused") + public static void main(String[] args) { + var birthDate1 = LocalDate.of(1973, Month.JULY, 11); + var birthDate2 = LocalDate.of(1973, 7, 11); + var time1 = LocalTime.of(9, 5); + var time2 = LocalTime.of(9, 5, 30); + var time3 = LocalTime.of(9, 5, 30, 200); + LocalDate.of(2020, Month.FEBRUARY, 30); + } + +} diff --git a/study-date-time-api/src/com/example/DateTimeOperations.java b/study-date-time-api/src/com/example/DateTimeOperations.java new file mode 100644 index 0000000..aff01f9 --- /dev/null +++ b/study-date-time-api/src/com/example/DateTimeOperations.java @@ -0,0 +1,25 @@ +package com.example; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +public class DateTimeOperations { + + public static void main(String[] args) { + LocalDate date = LocalDate.of(2100, 2, 27); + System.out.println(date); + date = date.plusDays(2); + System.out.println(date); + LocalTime time = LocalTime.of(9, 5); + System.out.println(time); + time = time.minusMinutes(10); + System.out.println(time); + // fluent api -> method chaining + LocalDateTime ldt = LocalDateTime.of(date,time); + System.out.println(ldt); + ldt = ldt.minusDays(1).minusHours(10).minusSeconds(30); + System.out.println(ldt); + } + +} diff --git a/study-date-time-api/src/com/example/StudyDuration.java b/study-date-time-api/src/com/example/StudyDuration.java new file mode 100644 index 0000000..ff44bb6 --- /dev/null +++ b/study-date-time-api/src/com/example/StudyDuration.java @@ -0,0 +1,15 @@ +package com.example; + +import java.time.Duration; +import java.time.LocalTime; + +public class StudyDuration { + + public static void main(String[] args) { + var daily = Duration.ofDays(1); + var hourly = Duration.ofHours(1); + LocalTime time = LocalTime.of(9, 5); + time = time.plus(daily).plus(hourly); + } + +} diff --git a/study-date-time-api/src/com/example/StudyInstant.java b/study-date-time-api/src/com/example/StudyInstant.java new file mode 100644 index 0000000..a72122b --- /dev/null +++ b/study-date-time-api/src/com/example/StudyInstant.java @@ -0,0 +1,20 @@ +package com.example; + +import java.time.Duration; +import java.time.Instant; +import java.time.ZonedDateTime; + +public class StudyInstant { + + public static void main(String[] args) { + // LocalDate, LocalTime, LocalDateTime, ZonedDateTime + // Period Duration + // Instant + var now = ZonedDateTime.now(); + var t1 = now.toInstant(); + var t2 = Instant.parse("2018-07-18T17:15:00Z"); + System.out.println(t2); + System.out.println(Duration.between(t1, t2)); + } + +} diff --git a/study-date-time-api/src/com/example/StudyInternationalization.java b/study-date-time-api/src/com/example/StudyInternationalization.java new file mode 100644 index 0000000..40ca162 --- /dev/null +++ b/study-date-time-api/src/com/example/StudyInternationalization.java @@ -0,0 +1,18 @@ +package com.example; + +import java.text.DecimalFormat; +import java.util.Locale; + +public class StudyInternationalization { + + public static void main(String[] args) { + //i18n + Locale locale = new Locale("tr","TR"); //ISO2 + String city = "izmir"; + System.out.println(city.toUpperCase(locale)); + double money = 123456.78; + DecimalFormat df = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.ITALY); + System.out.println(df.format(money)); + } + +} diff --git a/study-date-time-api/src/com/example/StudyPeriod.java b/study-date-time-api/src/com/example/StudyPeriod.java new file mode 100644 index 0000000..cde00e4 --- /dev/null +++ b/study-date-time-api/src/com/example/StudyPeriod.java @@ -0,0 +1,21 @@ +package com.example; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Period; + +public class StudyPeriod { + + @SuppressWarnings("unused") + public static void main(String[] args) { + var annually = Period.ofYears(1); + var quarterly = Period.ofMonths(3); + var everyYearAndAWeek = Period.of(1, 0, 7); + LocalDate date = LocalDate.of(2020, 7, 11); + LocalTime time = LocalTime.of(6, 15); + LocalDateTime ldt = LocalDateTime.of(date,time); + ldt = ldt.plus(quarterly); + } + +} diff --git a/study-date-time-api/src/com/example/StudyZone.java b/study-date-time-api/src/com/example/StudyZone.java new file mode 100644 index 0000000..575dca8 --- /dev/null +++ b/study-date-time-api/src/com/example/StudyZone.java @@ -0,0 +1,16 @@ +package com.example; + +import java.time.ZoneId; + +public class StudyZone { + + public static void main(String[] args) { + ZoneId.getAvailableZoneIds() + .stream() + .filter(zone -> zone.contains("Asia")) + .sorted() + .forEach(System.out::println); + + } + +} diff --git a/study-final-and-static-enum/.classpath b/study-final-and-static-enum/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-final-and-static-enum/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-final-and-static-enum/.project b/study-final-and-static-enum/.project new file mode 100644 index 0000000..96b094e --- /dev/null +++ b/study-final-and-static-enum/.project @@ -0,0 +1,17 @@ + + + study-final-and-static-enum + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-final-and-static-enum/.settings/org.eclipse.core.resources.prefs b/study-final-and-static-enum/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-final-and-static-enum/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-final-and-static-enum/.settings/org.eclipse.jdt.core.prefs b/study-final-and-static-enum/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/study-final-and-static-enum/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-final-and-static-enum/src/com/example/A.java b/study-final-and-static-enum/src/com/example/A.java new file mode 100644 index 0000000..8e41fc7 --- /dev/null +++ b/study-final-and-static-enum/src/com/example/A.java @@ -0,0 +1,19 @@ +package com.example; + +// final: +// 1) field, parameter, local variable -> constant +// 2) class, method -> extensibility +@SuppressWarnings("unused") +public class A { + private final int x = 42; // constant + + public void fun(final int y) { // y is constant + final int z = 108; // constant + } + + public final void sun() {} +} + +class B extends A { + +} \ No newline at end of file diff --git a/study-final-and-static-enum/src/com/example/C.java b/study-final-and-static-enum/src/com/example/C.java new file mode 100644 index 0000000..d97d80f --- /dev/null +++ b/study-final-and-static-enum/src/com/example/C.java @@ -0,0 +1,20 @@ +package com.example; + +public class C { + private int x; + private static int counter = 0; + + public static int getCounter() { + return counter; + } + + public C(int x) { + this.x = x; + counter++; + } + + public int getX() { + return this.x; + } + +} diff --git a/study-final-and-static-enum/src/com/example/D.java b/study-final-and-static-enum/src/com/example/D.java new file mode 100644 index 0000000..3e6a117 --- /dev/null +++ b/study-final-and-static-enum/src/com/example/D.java @@ -0,0 +1,16 @@ +package com.example; + +import static com.example.C.getCounter; + +public class D { + @SuppressWarnings({ "static-access", "null" }) + public static void main(String[] args) { + System.err.println(getCounter()); + var c = new C(100); + System.err.println(getCounter()); + System.err.println(c.getCounter()); + C p = null; + System.err.println(p.getCounter()); + System.err.println(p.getX()); + } +} diff --git a/study-object-layout/.classpath b/study-object-layout/.classpath new file mode 100644 index 0000000..5e8a55f --- /dev/null +++ b/study-object-layout/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/study-object-layout/.project b/study-object-layout/.project new file mode 100644 index 0000000..10099a3 --- /dev/null +++ b/study-object-layout/.project @@ -0,0 +1,23 @@ + + + study-object-layout + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/study-object-layout/.settings/org.eclipse.core.resources.prefs b/study-object-layout/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-object-layout/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-object-layout/.settings/org.eclipse.jdt.core.prefs b/study-object-layout/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..b8947ec --- /dev/null +++ b/study-object-layout/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/study-object-layout/.settings/org.eclipse.m2e.core.prefs b/study-object-layout/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/study-object-layout/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/study-object-layout/pom.xml b/study-object-layout/pom.xml new file mode 100644 index 0000000..43f0ad1 --- /dev/null +++ b/study-object-layout/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + com.example + object-layout + 1.0-SNAPSHOT + + 1.8 + 1.8 + + + + org.openjdk.jol + jol-core + 0.8 + + + \ No newline at end of file diff --git a/study-object-layout/src/main/java/com/example/Exercise1.java b/study-object-layout/src/main/java/com/example/Exercise1.java new file mode 100644 index 0000000..edc9db5 --- /dev/null +++ b/study-object-layout/src/main/java/com/example/Exercise1.java @@ -0,0 +1,23 @@ +package com.example; + +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.vm.VM; + +public class Exercise1 { + public static void main(String[] args) { + System.out.println(VM.current().details()); + System.out.println(ClassLayout.parseClass(A.class).toPrintable()); + } + +} +class B {} +class A { + byte b; + short s; + int i; + long l; + char c; + boolean x; + float f; + double d; +} \ No newline at end of file diff --git a/study-parallel-programming/.classpath b/study-parallel-programming/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-parallel-programming/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-parallel-programming/.project b/study-parallel-programming/.project new file mode 100644 index 0000000..fce1eb5 --- /dev/null +++ b/study-parallel-programming/.project @@ -0,0 +1,17 @@ + + + study-parallel-programming + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-parallel-programming/.settings/org.eclipse.core.resources.prefs b/study-parallel-programming/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-parallel-programming/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-parallel-programming/.settings/org.eclipse.jdt.core.prefs b/study-parallel-programming/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/study-parallel-programming/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-parallel-programming/src/benchmark-results.xlsx b/study-parallel-programming/src/benchmark-results.xlsx new file mode 100644 index 0000000..095099d Binary files /dev/null and b/study-parallel-programming/src/benchmark-results.xlsx differ diff --git a/study-parallel-programming/src/com/example/StudyArraySummation.java b/study-parallel-programming/src/com/example/StudyArraySummation.java new file mode 100644 index 0000000..03875d6 --- /dev/null +++ b/study-parallel-programming/src/com/example/StudyArraySummation.java @@ -0,0 +1,110 @@ +package com.example; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author Binnur Kurt + */ +public class StudyArraySummation { + private static final int DATA_SIZE = 160_000_000; + private static final int CPUS = Runtime.getRuntime().availableProcessors(); + private static final List DATA = new ArrayList<>(DATA_SIZE); + private static final ExecutorService POOL = Executors.newFixedThreadPool(CPUS); + static { + System.err.println("Initializing Data..."); + for (int i = 0; i < DATA_SIZE; i++) { + DATA.add(1.); + } + System.err.println("Initializing Data...Done"); + } + + public static void main(String[] args) { + for (int i = 0; i < 10; ++i) + solveSumParallelStream(); + for (int i = 0; i < 10; ++i) + solveSumParallel(); + for (int i = 0; i < 10; ++i) + solveSumStream(); + for (int i = 0; i < 10; ++i) + solveSumSerial(); + POOL.shutdown(); + } + + private static void solveSumStream() { + long begin = System.currentTimeMillis(); + double sum = DATA.stream().mapToDouble(Double::doubleValue).sum(); + long end = System.currentTimeMillis(); + System.err.println(String.format("%24s %16.8f %16d", "Stream[Serial]", sum, (end - begin))); + } + + private static void solveSumParallelStream() { + long begin = System.currentTimeMillis(); + double sum = DATA.stream().mapToDouble(Double::doubleValue).sum(); + long end = System.currentTimeMillis(); + System.err.println(String.format("%24s %16.8f %16d", "Stream[Parallel]", sum, (end - begin))); + } + + private static void solveSumParallel() { + int size = DATA_SIZE / CPUS; + List> futures = new ArrayList<>(CPUS); + long begin = System.currentTimeMillis(); + for (int i = 0, start = 0; i < CPUS; ++i, start += size) { + SumTask task = new SumTask(DATA, start, size); + futures.add(POOL.submit(task)); + } + double sum = 0.; + for (Future future : futures) { + try { + sum += future.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + long end = System.currentTimeMillis(); + System.err.println(String.format("%24s %16.8f %16d", "Parallel", sum, (end - begin))); + } + + private static void solveSumSerial() { + double sum = 0.; + long start = System.currentTimeMillis(); + for (int i = 0; i < DATA.size(); i++) { + sum += DATA.get(i); + } + long stop = System.currentTimeMillis(); + System.err.println(String.format("%24s %16.8f %16d", "Serial", sum, (stop - start))); + } + +} + +class SumTask implements Callable { + private List data; + private int start; + private int size; + + public SumTask(List data, int start, int size) { + this.data = data; + this.start = start; + this.size = size; + } + + @Override + public Double call() throws Exception { + double sum = 0L; + for (int i = start, j = 0; j < size; i++, j++) { + sum += data.get(i); + } + return sum; + } + + @Override + public String toString() { + return "SumTask [start=" + start + ", size=" + size + "]"; + } + +} diff --git a/study-parallel-programming/src/com/example/StudyParallelProgramming.java b/study-parallel-programming/src/com/example/StudyParallelProgramming.java new file mode 100644 index 0000000..c3509d1 --- /dev/null +++ b/study-parallel-programming/src/com/example/StudyParallelProgramming.java @@ -0,0 +1,190 @@ +package com.example; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.Future; +import java.util.concurrent.RecursiveTask; + +public class StudyParallelProgramming extends Object { + private static final String FORMAT = "Method: %20s Volume: %12.1f Duration: %12d"; + private static final int DATA_SIZE = 100_000_000; + private static final int CORES = Runtime.getRuntime().availableProcessors(); + private static final int DATA_SIZE_PER_CORE = DATA_SIZE / CORES; + private static final int LOOP_SIZE = 12; + private static final Random random = new Random(); + + private static List trades = new ArrayList<>(DATA_SIZE); + + static { + System.err.println("Initializing Trade Data..."); + for (var i = 0; i < DATA_SIZE; ++i) + trades.add(createRandomTrade()); + System.err.println("Initializing Trade Data...Done."); + } + + public static void computeVolumeSerialImperative() { + var begin = System.nanoTime(); + var volume = 0.; + for (var trade : trades) { + volume += trade.price * trade.quantity; + } + var end = System.nanoTime(); + System.err.println(String.format(FORMAT, "Serial Imperative", volume, (end - begin))); + } + + public static void computeVolumeSerialFunctional() { + var begin = System.nanoTime(); + var volume = trades.stream().mapToDouble(Trade::getVolume).sum(); + var end = System.nanoTime(); + System.err.println(String.format(FORMAT, "Serial Functional", volume, (end - begin))); + } + + public static void computeVolumeParallelPureThread() { + ExecutorService es = Executors.newFixedThreadPool(CORES); + var begin = System.nanoTime(); + var futures = new ArrayList>(CORES); + for (int i = 0, start = 0; i < CORES; ++i, start += DATA_SIZE_PER_CORE) { + var task = new VolumeIndicatorTask(trades, start, DATA_SIZE_PER_CORE); + futures.add(es.submit(task)); + } + var volume = 0.; + for (var future : futures) { + try { + volume += future.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + var end = System.nanoTime(); + System.err.println(String.format(FORMAT, "Parallel Pure Thread", volume, (end - begin))); + es.shutdown(); + } + + public static void computeVolumeParallelForkJoinPool() { + ForkJoinPool fjp = new ForkJoinPool(); + var begin = System.nanoTime(); + VolumeIndicatorRecursiveTask task = new VolumeIndicatorRecursiveTask(trades, 0, trades.size() - 1); + var volume = fjp.invoke(task); + var end = System.nanoTime(); + System.err.println(String.format(FORMAT, "Parallel Fork/Join", volume, (end - begin))); + } + + public static void computeVolumeParallelStream() { + var begin = System.nanoTime(); + var volume = trades.stream().parallel().mapToDouble(Trade::getVolume).sum(); + var end = System.nanoTime(); + System.err.println(String.format(FORMAT, "Parallel Stream", volume, (end - begin))); + } + + public static void main(String[] args) { + + for (var i = 0; i < LOOP_SIZE; ++i) { + computeVolumeParallelForkJoinPool(); + //System.gc(); + } + for (var i = 0; i < LOOP_SIZE; ++i) { + computeVolumeSerialImperative(); + //System.gc(); + } + for (var i = 0; i < LOOP_SIZE; ++i) { + computeVolumeSerialFunctional(); + //System.gc(); + } + for (var i = 0; i < LOOP_SIZE; ++i) { + computeVolumeParallelPureThread(); + //System.gc(); + } + for (var i = 0; i < LOOP_SIZE; ++i) { + computeVolumeParallelStream(); + //System.gc(); + } + } + + private static Trade createRandomTrade() { +// return new Trade(createRandomPQ(), createRandomPQ()); + return new Trade(1, 1); + } + + @SuppressWarnings("unused") + private static double createRandomPQ() { + return random.nextDouble() * 50 + 50; + } + +} + +class Trade { // 12B 4B 8B 8B: 32B x 1M + public double price; + public double quantity; + + public Trade(double price, double quantity) { + this.price = price; + this.quantity = quantity; + } + + public double getVolume() { + return price * quantity; + } +} + +class VolumeIndicatorTask implements Callable { + private List trades; + private int start; + private int size; + + public VolumeIndicatorTask(List trades, int start, int size) { + this.trades = trades; + this.start = start; + this.size = size; + } + + @Override + public Double call() throws Exception { + double volume = 0.; + for (int i = start, j = 0; j < size; ++i, ++j) { + volume += trades.get(i).getVolume(); + } + return volume; + } + +} + +@SuppressWarnings("serial") +class VolumeIndicatorRecursiveTask extends RecursiveTask { + private List trades; + private int begin; + private int end; + + public VolumeIndicatorRecursiveTask(List trades, int begin, int end) { + this.trades = trades; + this.begin = begin; + this.end = end; + } + + @Override + protected Double compute() { + int size = end - begin; + if (size <= 50_000) { + double volume = 0.; + for (int i = begin; i <= end; ++i) { + volume += trades.get(i).getVolume(); + } + return volume; + } + var left = new VolumeIndicatorRecursiveTask(trades, begin, begin + size / 2); + var right = new VolumeIndicatorRecursiveTask(trades, begin + size / 2 + 1, end); + invokeAll(left, right); + try { + return left.get() + right.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + return 0.; + } + +} \ No newline at end of file diff --git a/study-parallel-programming/src/~$benchmark-results.xlsx b/study-parallel-programming/src/~$benchmark-results.xlsx new file mode 100644 index 0000000..5b9f69c Binary files /dev/null and b/study-parallel-programming/src/~$benchmark-results.xlsx differ diff --git a/study-switch/.classpath b/study-switch/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-switch/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-switch/.project b/study-switch/.project new file mode 100644 index 0000000..2dac6da --- /dev/null +++ b/study-switch/.project @@ -0,0 +1,17 @@ + + + study-switch + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-switch/.settings/org.eclipse.core.resources.prefs b/study-switch/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-switch/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-switch/.settings/org.eclipse.jdt.core.prefs b/study-switch/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..9478cb1 --- /dev/null +++ b/study-switch/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-switch/src/com/example/SwitchExample1.java b/study-switch/src/com/example/SwitchExample1.java new file mode 100644 index 0000000..25c2029 --- /dev/null +++ b/study-switch/src/com/example/SwitchExample1.java @@ -0,0 +1,26 @@ +package com.example; + +public class SwitchExample1 { + + public static void main(String[] args) { + var weekDay = 3; + var status = "unknown"; + switch (weekDay) { + case 1: + case 2: + case 3: + case 4: + case 5: + status = "working"; + break; + case 6: + case 7: + status = "resting"; + break; + default: + status = "unknown"; + } + System.out.println(status); + } + +} diff --git a/study-switch/src/com/example/SwitchExample2.java b/study-switch/src/com/example/SwitchExample2.java new file mode 100644 index 0000000..c5e0845 --- /dev/null +++ b/study-switch/src/com/example/SwitchExample2.java @@ -0,0 +1,22 @@ +package com.example; + +public class SwitchExample2 { + + public static void main(String[] args) { + var weekDay = 3; + var status = "unknown"; + switch (weekDay) { + case 1, 2, 3, 4, 5 -> { + status = "working"; + } + case 6, 7 -> { + status = "resting"; + } + default -> { + status = "unknown"; + } + } + System.out.println(status); + } + +} diff --git a/study-switch/src/com/example/SwitchExample3.java b/study-switch/src/com/example/SwitchExample3.java new file mode 100644 index 0000000..972abe5 --- /dev/null +++ b/study-switch/src/com/example/SwitchExample3.java @@ -0,0 +1,21 @@ +package com.example; + +public class SwitchExample3 { + + public static void main(String[] args) { + var weekDay = 3; + var status = switch (weekDay) { + case 1, 2, 3, 4, 5 -> { + yield "working"; + } + case 6, 7 -> { + yield "resting"; + } + default -> { + yield "unknown"; + } + }; + System.out.println(status); + } + +} diff --git a/study-switch/src/com/example/SwitchExample4.java b/study-switch/src/com/example/SwitchExample4.java new file mode 100644 index 0000000..240bf8b --- /dev/null +++ b/study-switch/src/com/example/SwitchExample4.java @@ -0,0 +1,15 @@ +package com.example; + +public class SwitchExample4 { + + public static void main(String[] args) { + var weekDay = 3; + var status = switch (weekDay) { + case 1, 2, 3, 4, 5 -> "working"; + case 6, 7 -> "resting"; + default -> "unknown"; + } ; + System.out.println(status); + } + +} diff --git a/study-try-finally/.classpath b/study-try-finally/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/study-try-finally/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/study-try-finally/.project b/study-try-finally/.project new file mode 100644 index 0000000..8076158 --- /dev/null +++ b/study-try-finally/.project @@ -0,0 +1,17 @@ + + + study-try-finally + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/study-try-finally/.settings/org.eclipse.core.resources.prefs b/study-try-finally/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/study-try-finally/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/study-try-finally/.settings/org.eclipse.jdt.core.prefs b/study-try-finally/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/study-try-finally/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/study-try-finally/src/com/example/PreciousResource.java b/study-try-finally/src/com/example/PreciousResource.java new file mode 100644 index 0000000..922e719 --- /dev/null +++ b/study-try-finally/src/com/example/PreciousResource.java @@ -0,0 +1,16 @@ +package com.example; + +public class PreciousResource implements AutoCloseable { + private int id; + + public PreciousResource(int id) { + this.id = id; + } + + @Override + public void close() throws Exception { + System.err.println("Closing the precious resource " + id); + throw new RuntimeException("Ooops!"); + } + +} diff --git a/study-try-finally/src/com/example/Question.java b/study-try-finally/src/com/example/Question.java new file mode 100644 index 0000000..ada8490 --- /dev/null +++ b/study-try-finally/src/com/example/Question.java @@ -0,0 +1,51 @@ +package com.example; + +@SuppressWarnings("unused") +public class Question { + public static void main(String[] args) { + B b = new B(); + } +} + +@SuppressWarnings("unused") +class A { + private D d = new D(); + + public A() { + System.out.println("A's constructor"); + } + + int fun() { + return 42; + } +} + +@SuppressWarnings("unused") +class B extends A { +// private E e = new E(); + private int b = super.fun(); + public B() { + System.out.println("B's constructor"); + } +} + +class C { + public C() { + System.out.println("C's constructor"); + } +} + +class D { + public D() { + System.out.println("D's constructor"); + } +} + +@SuppressWarnings("unused") +class E { + private C c = new C(); + + public E() { + System.out.println("E's constructor"); + } +} \ No newline at end of file diff --git a/study-try-finally/src/com/example/app/CatchMeIfYouCan.java b/study-try-finally/src/com/example/app/CatchMeIfYouCan.java new file mode 100644 index 0000000..97241a1 --- /dev/null +++ b/study-try-finally/src/com/example/app/CatchMeIfYouCan.java @@ -0,0 +1,17 @@ +package com.example.app; + +@SuppressWarnings("finally") +public class CatchMeIfYouCan { + + public static void main(String[] args) { + System.err.println(fun()); + } + + public static int fun() { + try { + return 42; + } finally { + return 108; + } + } +} diff --git a/study-try-finally/src/com/example/app/MyApp.java b/study-try-finally/src/com/example/app/MyApp.java new file mode 100644 index 0000000..a00421f --- /dev/null +++ b/study-try-finally/src/com/example/app/MyApp.java @@ -0,0 +1,22 @@ +package com.example.app; + +import java.io.FileInputStream; + +import com.example.PreciousResource; + +@SuppressWarnings("unused") +public class MyApp { + + public static void main(String[] args) { + FileInputStream fis; + try( // try with resources + PreciousResource res1 = new PreciousResource(1); + PreciousResource res2 = new PreciousResource(2); + PreciousResource res3 = new PreciousResource(3); + ) { + } catch (Exception e) { + System.err.println("Error while closing the resource: " + e.getMessage()); + } + } + +}