From 757ed9b49b8de4a3147fe0a44f91960095fdbc55 Mon Sep 17 00:00:00 2001 From: EgorBobrov Date: Sun, 22 Aug 2021 21:21:01 +0300 Subject: [java/en] Added more info about Collections initialization (#4200) --- java.html.markdown | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 79769352..4be940be 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -510,10 +510,12 @@ public class LearnJava { // toString returns this Object's string representation. System.out.println("trek info: " + trek.toString()); + } // End main method + private static class TestInitialization { // Double Brace Initialization - // The Java Language has no syntax for how to create static Collections - // in an easy way. Usually you end up in the following way: + // Before Java 11, the Java Language had no syntax for how to create + // static Collections in an easy way. Usually you end up like this: private static final Set COUNTRIES = new HashSet(); static { COUNTRIES.add("DENMARK"); @@ -521,10 +523,10 @@ public class LearnJava { COUNTRIES.add("FINLAND"); } - // But there's a nifty way to achieve the same thing in an - // easier way, by using something that is called Double Brace - // Initialization. - private static final Set COUNTRIES = new HashSet() {{ + // There's a nifty way to achieve the same thing, + // by using something that is called Double Brace Initialization. + private static final Set COUNTRIES_DOUBLE_BRACE = + new HashSet() {{ add("DENMARK"); add("SWEDEN"); add("FINLAND"); @@ -535,8 +537,45 @@ public class LearnJava { // is called when the anonymous inner class is created. // This does not only work for Collections, it works for all // non-final classes. + + + // Another option was to initialize the Collection from an array, + // using Arrays.asList() method: + private static final List COUNTRIES_AS_LIST = + Arrays.asList("SWEDEN", "DENMARK", "NORWAY"); + // This has one catch: the list we get is internally backed by the array, + // and since arrays can't change their size, the list backed by the array + // is not resizeable, which means we can't add new elements to it: + public static void main(String[] args) { + COUNTRIES.add("FINLAND"); // throws UnsupportedOperationException! + // However, we can replace elements by index, just like in array: + COUNTRIES.set(1, "FINLAND"); + System.out.println(COUNTRIES); // prints [SWEDEN, FINLAND, NORWAY] + } + // The resizing problem can be circumvented + // by creating another Collection from the List: + private static final Set COUNTRIES_SET = + new HashSet<>(Arrays.asList("SWEDEN", "DENMARK", "NORWAY")); + // It's perfectly fine to add anything to the Set of COUNTRIES now. + } // End TestInitialization class + + private static class TestJava11Initialization { + // Since Java 11, there is a convenient option to initialize Collections: + // Set.of() and List.of() methods. + private static final Set COUNTRIES = + Set.of("SWEDEN", "DENMARK", "NORWAY"); + // There is a massive catch, though: Lists and Sets initialized like this + // 1) are immutable + // 2) can't contain null elements (even check for null elements fails)! + public static void main(String[] args) { + COUNTRIES.add("FINLAND"); // throws UnsupportedOperationException + COUNTRIES.remove("NORWAY"); // throws UnsupportedOperationException + COUNTRIES.contains(null); // throws NullPointerException + } + private static final Set COUNTRIES_WITH_NULL = + Set.of("SWEDEN", null, "NORWAY"); // throws NullPointerException - } // End main method + } // End TestJava11Initialization class } // End LearnJava class // You can include other, non-public outer-level classes in a .java file, -- cgit v1.2.3 From 0379989cfb9246535fb5fbc58f015200bfba02d9 Mon Sep 17 00:00:00 2001 From: Ivan <46273803+Tookser@users.noreply.github.com> Date: Fri, 25 Aug 2023 06:59:22 +0300 Subject: [en/java] mistake fix and add missed assignment (#4344) --- java.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 4be940be..394e0b09 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -91,6 +91,9 @@ public class LearnJava { int numInt = scanner.nextInt(); // read long input + long numLong = scanner.nextLong(); + + // read float input float numFloat = scanner.nextFloat(); // read double input -- cgit v1.2.3 From d5e7ada2dc6d0e2b176f9e346622a193036ec56f Mon Sep 17 00:00:00 2001 From: San Baby Francis Date: Fri, 8 Sep 2023 11:05:01 +0530 Subject: fix code formatting (#4722) --- java.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 394e0b09..10b2315b 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -317,7 +317,7 @@ public class LearnJava { System.out.println("1/2.0 = " + (i1 / (double)i2)); // => 0.5 // Modulo - System.out.println("11%3 = "+(11 % 3)); // => 2 + System.out.println("11%3 = " + (11 % 3)); // => 2 // Comparison operators System.out.println("3 == 2? " + (3 == 2)); // => false @@ -371,7 +371,7 @@ public class LearnJava { // While loop int fooWhile = 0; - while(fooWhile < 100) { + while (fooWhile < 100) { System.out.println(fooWhile); // Increment the counter // Iterated 100 times, fooWhile 0,1,2...99 @@ -386,7 +386,7 @@ public class LearnJava { // Increment the counter // Iterated 100 times, fooDoWhile 0->99 fooDoWhile++; - } while(fooDoWhile < 100); + } while (fooDoWhile < 100); System.out.println("fooDoWhile Value: " + fooDoWhile); // For Loop -- cgit v1.2.3 From 1590fba5f2a7c53b180bd5257cc13cbe79c621fe Mon Sep 17 00:00:00 2001 From: noahmarro <68234387+noahmarro@users.noreply.github.com> Date: Sat, 9 Sep 2023 20:13:40 +0200 Subject: Typo java.html.markdown --- java.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 10b2315b..6003f8ef 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -185,7 +185,7 @@ public class LearnJava { // // BigInteger is a data type that allows programmers to manipulate // integers longer than 64-bits. Integers are stored as an array of - // of bytes and are manipulated using functions built into BigInteger + // bytes and are manipulated using functions built into BigInteger // // BigInteger can be initialized using an array of bytes or a string. BigInteger fooBigInteger = new BigInteger(fooByteArray); -- cgit v1.2.3 From 9f317de120e8ad02ff47e3ad976bd69391efcada Mon Sep 17 00:00:00 2001 From: Aaryan Rampal <76913929+aaryan-rampal@users.noreply.github.com> Date: Thu, 14 Dec 2023 06:54:25 -0800 Subject: [java/en] Update java.html.markdown (#4079) * Update java.html.markdown java/en * Update java.html.markdown --------- Co-authored-by: jus78help <76913929+jus78help@users.noreply.github.com> --- java.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 6003f8ef..7bf3fa0a 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -126,6 +126,9 @@ public class LearnJava { // = = = int barInt1, barInt2, barInt3; barInt1 = barInt2 = barInt3 = 1; + // Shorthand for multiple declarations + int barInt4 = 1, barInt5 = 2; + /* * Variable types @@ -307,8 +310,8 @@ public class LearnJava { /////////////////////////////////////// System.out.println("\n->Operators"); - int i1 = 1, i2 = 2; // Shorthand for multiple declarations - + int i1 = 1, i2 = 2; + // Arithmetic is straightforward System.out.println("1+2 = " + (i1 + i2)); // => 3 System.out.println("2-1 = " + (i2 - i1)); // => 1 -- cgit v1.2.3