From 62972b67feb3f65a02a21cf7d037e9c0b08b3937 Mon Sep 17 00:00:00 2001 From: Viacheslav Shevtsov Date: Tue, 25 Nov 2014 08:14:54 +0200 Subject: [PATCH 1/4] 20141125 --- .../viacheslav.shevtsov/Exercise_1.java | 31 +++++ week3/workspace/viacheslav.shevtsov/README.md | 118 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 week3/workspace/viacheslav.shevtsov/Exercise_1.java create mode 100644 week3/workspace/viacheslav.shevtsov/README.md diff --git a/week3/workspace/viacheslav.shevtsov/Exercise_1.java b/week3/workspace/viacheslav.shevtsov/Exercise_1.java new file mode 100644 index 0000000..5b8c21e --- /dev/null +++ b/week3/workspace/viacheslav.shevtsov/Exercise_1.java @@ -0,0 +1,31 @@ +class FieldInitializer{ + static String FieldInitializer(String message){ + System.out.println(message); + return message; + } +} +class ParentClass { + ParentClass(){ + System.out.println("This is a object of ParentClass"); + } +} + +class HeritorClass extends ParentClass { + private String nonStaticString = FieldInitializer.FieldInitializer("Init nonStaticString for HeritorClass"); + private static String StaticString = FieldInitializer.FieldInitializer("Init StaticString for HeritorClass"); + { + System.out.println("Non-static initialization block in HeritorClass"); + } + static { + System.out.println("Static initialization block in HeritorClass"); + } + HeritorClass(){ + System.out.println("This is a object of HeritorClass"); + } + +} +public class Exercise_1 { + public static void main(String[] args) { + HeritorClass heritor = new HeritorClass(); + } +} diff --git a/week3/workspace/viacheslav.shevtsov/README.md b/week3/workspace/viacheslav.shevtsov/README.md new file mode 100644 index 0000000..f1d66e4 --- /dev/null +++ b/week3/workspace/viacheslav.shevtsov/README.md @@ -0,0 +1,118 @@ +* 1. Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks and a constructor without parameters. In addition, the class inherits another class which also has a parameterless constructor. +What will be the initialization order of these two classes? Create these classes in order to prove your answer.* +- static filed +- Static initialization block +- Constructor of the super class +- Non-Static initialization block +- Constructor of the inherited class + +*Note:* Please see prove in the Exercise_1.java. + +2. Look at the code block below. What will be the output if we run the main method of the class `A`? +What will happen if we remove the no-argument constructor of the `A` class? If there is an error, explain why it happens and suggest the way how to fix it. + +```java +public class A { + private String variable_D; + + public static String static_var = "Hello static variable"; + + static { + System.out.println("Hello from static block A"); + System.out.println("Hello static variable = " + static_var); + } + + public A() { + System.out.println("Hello from constructor A"); + printVariable(); + } + + public A(String variable) { + System.out.println("Hello from constructor A"); + printVariable(); + } + + protected void printVariable() { + variable_D = "variable is initialized in Main Class"; + } + + public static void main(String[] args) { + System.out.println("Hello World!"); + B b = new B(); + } +} + +public class B extends A { + private String variable; + + public static String static_var = "Hello static variable"; + + static { + System.out.println("Hello from static block B"); + System.out.println("Hello static variable = " + static_var); + } + + public B() { + super(static_var); + System.out.println("variable value = " + variable); + } + + public B(String variable) { + System.out.println("variable value = " + this.variable); + } + + protected void printVariable() { + variable = "variable is initialized in B Class"; + } +} +``` + +3. List here as many ways to create and initialize an array of two integers as you know. + +4. Look at the class below, probably there is a mistake that the author made. If you think so, change the class in order to + do that the author intended to do. + +```java +public class C { + private static Entry entry = new Entry(); + + public static void main(String... args) { + Entry[] entryArray = new Entry[10]; + for (int i = 0; i < 10; i++) { + entry.value = "str_" + i; + entryArray[i] = entry; + } + + printArray(entryArray); + } + + private static void printArray(Entry[] entryArr) { + for (Entry entry : entryArr) { + System.out.println(entry); + } + } + + static class Entry { + public String toString() { + return "Entry{" + + "value='" + value + '\'' + + '}'; + } + + private String value; + } +} +``` + +5. Explain how to destroy objects in Java. + +6. Can you rely on the `finalize` method? Are there any best practices to use `finalize` in your programs? + +7. What is the main purpose of the Garbage Collector in Java? + +8. Read the "how a garbage collector works" chapter twice and try to explain how the GC works in Java. + +9. Probably you've heard of the JIT term before. Explain what does this term stand for? + +Good luck + From 96785d4b497686d417ac2aed8f6c371d4fbaf366 Mon Sep 17 00:00:00 2001 From: hedgss Date: Tue, 25 Nov 2014 08:19:41 +0200 Subject: [PATCH 2/4] Update README.md --- week3/workspace/viacheslav.shevtsov/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/week3/workspace/viacheslav.shevtsov/README.md b/week3/workspace/viacheslav.shevtsov/README.md index f1d66e4..96fce58 100644 --- a/week3/workspace/viacheslav.shevtsov/README.md +++ b/week3/workspace/viacheslav.shevtsov/README.md @@ -1,5 +1,6 @@ -* 1. Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks and a constructor without parameters. In addition, the class inherits another class which also has a parameterless constructor. -What will be the initialization order of these two classes? Create these classes in order to prove your answer.* +1. *Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks and a constructor without parameters. In addition, the class inherits another class which also has a parameterless constructor.* +*What will be the initialization order of these two classes? Create these classes in order to prove your answer.* + - static filed - Static initialization block - Constructor of the super class From 8b02277cce407e5a8a2be5e1e13cd02dbbfd24c0 Mon Sep 17 00:00:00 2001 From: Hedg Date: Sun, 30 Nov 2014 03:41:58 +0200 Subject: [PATCH 3/4] 20141130 --- .gitignore | 3 +++ .../{ => src}/Exercise_1.java | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) rename week3/workspace/viacheslav.shevtsov/{ => src}/Exercise_1.java (57%) diff --git a/.gitignore b/.gitignore index 4b370e2..6b199c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ # Intellij +out/ +.idea/ .idea *.iml *.iws @@ -12,3 +14,4 @@ *.war *.jar *.class +*.stackdump diff --git a/week3/workspace/viacheslav.shevtsov/Exercise_1.java b/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java similarity index 57% rename from week3/workspace/viacheslav.shevtsov/Exercise_1.java rename to week3/workspace/viacheslav.shevtsov/src/Exercise_1.java index 5b8c21e..89cedb5 100644 --- a/week3/workspace/viacheslav.shevtsov/Exercise_1.java +++ b/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java @@ -6,13 +6,13 @@ static String FieldInitializer(String message){ } class ParentClass { ParentClass(){ - System.out.println("This is a object of ParentClass"); + System.out.println("This is a constructor of ParentClass"); } } class HeritorClass extends ParentClass { - private String nonStaticString = FieldInitializer.FieldInitializer("Init nonStaticString for HeritorClass"); - private static String StaticString = FieldInitializer.FieldInitializer("Init StaticString for HeritorClass"); + private String nonStaticString = FieldInitializer.FieldInitializer("Init a field nonStaticString for HeritorClass"); + private static String StaticString = FieldInitializer.FieldInitializer("Init a field StaticString for HeritorClass"); { System.out.println("Non-static initialization block in HeritorClass"); } @@ -20,7 +20,7 @@ class HeritorClass extends ParentClass { System.out.println("Static initialization block in HeritorClass"); } HeritorClass(){ - System.out.println("This is a object of HeritorClass"); + System.out.println("This is a constructor of HeritorClass"); } } @@ -29,3 +29,14 @@ public static void main(String[] args) { HeritorClass heritor = new HeritorClass(); } } +/* +Output: + +Init a field StaticString for HeritorClass +Static initialization block in HeritorClass +This is a constructor of ParentClass +Init a field nonStaticString for HeritorClass +Non-static initialization block in HeritorClass +This is a constructor of HeritorClass + + */ From a44c9050bce6e57f709f770bd9de187ddfa90488 Mon Sep 17 00:00:00 2001 From: Hedg Date: Mon, 1 Dec 2014 04:32:37 +0200 Subject: [PATCH 4/4] week_3 is done --- week3/README.md | 18 +- week3/workspace/viacheslav.shevtsov/README.md | 182 ++++++++---------- .../workspace/viacheslav.shevtsov/src/C.java | 43 +++++ .../viacheslav.shevtsov/src/Exercise_1.java | 2 + 4 files changed, 129 insertions(+), 116 deletions(-) create mode 100644 week3/workspace/viacheslav.shevtsov/src/C.java diff --git a/week3/README.md b/week3/README.md index f1f0ed9..6d32285 100644 --- a/week3/README.md +++ b/week3/README.md @@ -4,11 +4,11 @@ This is a new lesson so please open the book and read pages from 155 to 209. It' Then just answer a few questions: -1. Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks +1 . Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks and a constructor without parameters. In addition, the class inherits another class which also has a parameterless constructor. What will be the initialization order of these two classes? Create these classes in order to prove your answer. -2. Look at the code block below. What will be the output if we run the main method of the class `A`? +2 . Look at the code block below. What will be the output if we run the main method of the class `A`? What will happen if we remove the no-argument constructor of the `A` class? If there is an error, explain why it happens and suggest the way how to fix it. ```java @@ -67,9 +67,9 @@ public class B extends A { } ``` -3. List here as many ways to create and initialize an array of two integers as you know. +3 . List here as many ways to create and initialize an array of two integers as you know. -4. Look at the class below, probably there is a mistake that the author made. If you think so, change the class in order to +4 . Look at the class below, probably there is a mistake that the author made. If you think so, change the class in order to do that the author intended to do. ```java @@ -104,15 +104,15 @@ public class C { } ``` -5. Explain how to destroy objects in Java. +5 . Explain how to destroy objects in Java. -6. Can you rely on the `finalize` method? Are there any best practices to use `finalize` in your programs? +6 . Can you rely on the `finalize` method? Are there any best practices to use `finalize` in your programs? -7. What is the main purpose of the Garbage Collector in Java? +7 . What is the main purpose of the Garbage Collector in Java? -8. Read the "how a garbage collector works" chapter twice and try to explain how the GC works in Java. +8 . Read the "how a garbage collector works" chapter twice and try to explain how the GC works in Java. -9. Probably you've heard of the JIT term before. Explain what does this term stand for? +9 . Probably you've heard of the JIT term before. Explain what does this term stand for? Good luck diff --git a/week3/workspace/viacheslav.shevtsov/README.md b/week3/workspace/viacheslav.shevtsov/README.md index 96fce58..0f6aaef 100644 --- a/week3/workspace/viacheslav.shevtsov/README.md +++ b/week3/workspace/viacheslav.shevtsov/README.md @@ -1,119 +1,87 @@ -1. *Let's assume that we created a class which has a field, a static field, both static and non-static initialization blocks and a constructor without parameters. In addition, the class inherits another class which also has a parameterless constructor.* -*What will be the initialization order of these two classes? Create these classes in order to prove your answer.* -- static filed -- Static initialization block -- Constructor of the super class -- Non-Static initialization block -- Constructor of the inherited class +1 . *The initialization order will be following:* + - Static filed + - Static initialization block + - Constructor of the super class + - Non-Static initialization block + - Constructor of the inherited class -*Note:* Please see prove in the Exercise_1.java. - -2. Look at the code block below. What will be the output if we run the main method of the class `A`? -What will happen if we remove the no-argument constructor of the `A` class? If there is an error, explain why it happens and suggest the way how to fix it. +*Note:* Please see the prove in the ./src/Exercise_1.java -```java -public class A { - private String variable_D; - - public static String static_var = "Hello static variable"; - - static { - System.out.println("Hello from static block A"); - System.out.println("Hello static variable = " + static_var); - } - - public A() { - System.out.println("Hello from constructor A"); - printVariable(); - } - - public A(String variable) { - System.out.println("Hello from constructor A"); - printVariable(); - } - - protected void printVariable() { - variable_D = "variable is initialized in Main Class"; - } - - public static void main(String[] args) { - System.out.println("Hello World!"); - B b = new B(); - } -} - -public class B extends A { - private String variable; - - public static String static_var = "Hello static variable"; - - static { - System.out.println("Hello from static block B"); - System.out.println("Hello static variable = " + static_var); - } - - public B() { - super(static_var); - System.out.println("variable value = " + variable); - } - - public B(String variable) { - System.out.println("variable value = " + this.variable); - } - - protected void printVariable() { - variable = "variable is initialized in B Class"; - } -} -``` +2 . The initialization order: -3. List here as many ways to create and initialize an array of two integers as you know. - -4. Look at the class below, probably there is a mistake that the author made. If you think so, change the class in order to - do that the author intended to do. +- Static variable in class "A" +- Static block in class "A" +- Static method main +- print "Hello World!". +- Static variable in class "B" +- Static block in class "B" +- Constructor "A" with a parameter (It was executed from Constructor B without parameters) +- Constructor "A" executes a method printVariable() from from class B that set value for "variable" in class "B" +- Constructor "B" + +If we delete the constructor A() we will get a compilation error. +It happens because constructor B() uses constructor A() indirectly. +If we don't use "super" with or without parameters directly then superclass's constructor without parameters will be executed by default. +To fix this issue we should add call of constructor A with parameter to constructor B with parameter. + +3 . List here as many ways to create and initialize an array of two integers as you know. + +- int[] a1 = {1,2}; +- int[] a2 = a1; +- int[] a = new int[2]; +- int[] a = new int[]{1,2}; +- int[] a = new int[2]; a = new int[]{1,2}; +- int[] a = new int[2]; for(int i = 0; i < a.length; i++){a[i] = i;} +- int[] a = new int[2]; a[0] = 1; a[1] = 2; + +4 . I changed the code below. ```java -public class C { - private static Entry entry = new Entry(); - - public static void main(String... args) { - Entry[] entryArray = new Entry[10]; - for (int i = 0; i < 10; i++) { - entry.value = "str_" + i; - entryArray[i] = entry; - } - - printArray(entryArray); - } - - private static void printArray(Entry[] entryArr) { - for (Entry entry : entryArr) { - System.out.println(entry); - } - } - - static class Entry { - public String toString() { - return "Entry{" + - "value='" + value + '\'' + - '}'; - } - - private String value; - } -} + public static void main(String... args) { + Entry[] entryArray = new Entry[10]; + for (int i = 0; i < 10; i++) { + Entry entryTemp = new Entry(); + entryTemp.value = "str_" + i; + entryArray[i] = entryTemp; + } + printArray(entryArray); + } ``` +*Note:* Please see a fix in the ./src/C.java -5. Explain how to destroy objects in Java. +5 . Java doesn't have a special method to destroy object like a destructor in C++ (` ~` ). +Java has a Garbage Collector. It should free unused memory automatically. +If a object isn't used (nothing has reference on it), GC will delete it automatically. +Also we can call gc manually, but it is a bad idea in general. -6. Can you rely on the `finalize` method? Are there any best practices to use `finalize` in your programs? - -7. What is the main purpose of the Garbage Collector in Java? - -8. Read the "how a garbage collector works" chapter twice and try to explain how the GC works in Java. +6 . We cannot rely on the `finalize` method. GC will call a finalize method before deleting object if class contains method finalize. +But we don't know when it happens exactly. GC can never call method finalize in a few cases. +This method slows down the GC. + +The method `finalze` can be used for : +- the verification on the termination condition. +- cleanup non-Java resources + +7 . GC is a part of Java Memory Management. GC is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects -9. Probably you've heard of the JIT term before. Explain what does this term stand for? +8 . OS allocates the heap when JVM starts . This memory is managed by JVM while the program is running. +This has a couple of important ramifications: +- Object creation is faster because global synchronization with the operating system is not needed for every single object. +- When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation + +All objects are allocated on the heap area managed by the JVM. +As long as an object is being referenced, the JVM considers it alive. +Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory. + +GC process can be described as follows: +- Step 1: Marking. GC checks all object trees and marks every object found as alive. +This can be a very time consuming process if all objects in a system must be scanned. +- Step 2: Deletion. All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free + +9 . A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, + will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. + JVM can optimize a piece of code each time it is executed, so the more the code is executed, the faster it gets (This approach is used in HotSpot). + -Good luck diff --git a/week3/workspace/viacheslav.shevtsov/src/C.java b/week3/workspace/viacheslav.shevtsov/src/C.java new file mode 100644 index 0000000..cb676ec --- /dev/null +++ b/week3/workspace/viacheslav.shevtsov/src/C.java @@ -0,0 +1,43 @@ +public class C { + private static Entry entry = new Entry(); + + public static void main(String... args) { + Entry[] entryArray = new Entry[10]; + for (int i = 0; i < 10; i++) { + Entry entryTemp = new Entry(); + entryTemp.value = "str_" + i; + entryArray[i] = entryTemp; + } + + printArray(entryArray); + } + + private static void printArray(Entry[] entryArr) { + for (Entry entry : entryArr) { + System.out.println(entry); + } + } + + static class Entry { + public String toString() { + return "Entry{" + + "value='" + value + '\'' + + '}'; + } + + private String value; + } +} + +/* Output: +Entry{value='str_0'} +Entry{value='str_1'} +Entry{value='str_2'} +Entry{value='str_3'} +Entry{value='str_4'} +Entry{value='str_5'} +Entry{value='str_6'} +Entry{value='str_7'} +Entry{value='str_8'} +Entry{value='str_9'} + */ \ No newline at end of file diff --git a/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java b/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java index 89cedb5..63912e2 100644 --- a/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java +++ b/week3/workspace/viacheslav.shevtsov/src/Exercise_1.java @@ -1,9 +1,11 @@ + class FieldInitializer{ static String FieldInitializer(String message){ System.out.println(message); return message; } } + class ParentClass { ParentClass(){ System.out.println("This is a constructor of ParentClass");