update prog-intro course
This commit is contained in:
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 11 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 16 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 49 KiB |
@@ -88,4 +88,58 @@ Java comes in two parts: **JDK - Java Development Kit** and **JVM - Java Virtual
|
||||
|
||||
There is also a **JRE - Java Runtime Environment**. For example if we want to run our code somewhere on the server we don't need to compile it there because we have our byte code and we just need JRE to run it.
|
||||
|
||||
Some of the most popular JVMs right now are:
|
||||
|
||||
- OpenJDK
|
||||
- Eclipse
|
||||
- Azul Systems
|
||||
- Excelsior JET
|
||||
|
||||
The disadvantage of such system is in the connection between JVM and a native processing unit. In case of C++ compiler that we reviewed earlier the source code is compiled directly into machine-code but in case with Java it is compiled into byte-code. And so the problem is to develop such a JVM that would quickly turn our byte-code into machine-code. Anyway it takes extra time. That's why it mostly will be slower than direct compilation into machine-code. So ultimately while we have the advantage of compiling out code only once, we have the disadvantage of turning byte-code into machine-code slower.
|
||||
|
||||
None the less, there is a way to speed up this process which is called JIT - Just In Time compilation. How does it work? While our program is running some of the instructions or functions turns directly into processor commands.
|
||||
|
||||
# What is garbage collection?
|
||||
|
||||
For example we have `int` which is represented with 4 bytes of data which is directly stored in memory.
|
||||
|
||||

|
||||
*Img. X -- `int` in memory*
|
||||
|
||||
But what if we have a `String`. How many memory cells does this string take? We don't know. We will say that our `String` that has length of 5 symbols is stored at `0x12347865`. We defined an address where this string is located in memory. And somewhere in the memory of our programm will be a large buffer where the 5 cells will be stored.
|
||||
|
||||

|
||||
*Img. X -- `String` in memory*
|
||||
|
||||
But what do we do with that buffer? We won't need it forever and so sometimes we need to clear that buffer. In case with C/C++ whoever created the memory for that string is in charge of clearing it. There are also languages with garbage collection such as Java and Python. The purpose of the **Garbage Collector** is to automatically find variables that we no longer need and clear their memory.
|
||||
|
||||
Suppose we have some code like this.
|
||||
|
||||
```java
|
||||
if (someCondition) {
|
||||
x = [1, 3, 7] // first link
|
||||
// some code here
|
||||
y = x // second link
|
||||
// some code here
|
||||
} // no links
|
||||
```
|
||||
|
||||
After the `if`-statement we no longer need `x` or `y`. Every variable in this case `x` and `y` is the link to our array (`[1, 3, 7]`). After we left the `if`-statement the amount of links to the array is zero, so we can safely delete the array. This approach is implemented in Python. The problem is that if we have to objects linked to one another and there are no external links, they will not be deleted by garbage collector since there link counter is 1.
|
||||
|
||||

|
||||
*Img. X -- Edge case for link counting*
|
||||
|
||||
The advanced way of implementing the garbage collection is *traversing the graph of links* which is implemented in Java, C# and Go.
|
||||
|
||||
# What other advantages does Java have?
|
||||
|
||||
- It's easy (in terms of syntax)
|
||||
- It's secure and stable
|
||||
- It supports Unicode
|
||||
- It supports multithreading
|
||||
- It has backwards compatibility
|
||||
|
||||
# How should Java code look like?
|
||||
|
||||
You can go and learn about it [here](https://google.github.io/styleguide/javaguide.html).
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 11 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 16 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 49 KiB |
@@ -53,7 +53,7 @@ What does Java consist of?Java consists of multiple main components. The first o
|
||||
Img. 1 - TIOBE Programming Community Index
|
||||
Also, Java has a lot of advantages for beginners such as:
|
||||
It’s fairly easy It has a broad spectre of usage Server Desktop Mobile devices Smart-cards What does Java consist of?Java consists of multiple main components. The first one being the Java compiler. The process of converting human-readable text to machine code is called compilation.">
|
||||
<meta itemprop="wordCount" content="468">
|
||||
<meta itemprop="wordCount" content="987">
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:title" content="Lecture 1. Introduction">
|
||||
<meta name="twitter:description" content="Why do we choose Java?According to TIOBE Programming Community Index Java is one of the most demanded languages in the programming field.
|
||||
@@ -296,7 +296,7 @@ It’s fairly easy It has a broad spectre of usage Server Desktop Mobile devices
|
||||
</div></div></div></aside>
|
||||
|
||||
<nav class="hextra-toc hx:order-last hx:hidden hx:w-64 hx:shrink-0 hx:xl:block hx:print:hidden hx:px-4" aria-label="table of contents">
|
||||
<div class="hextra-scrollbar hx:sticky hx:top-16 hx:overflow-y-auto hx:pr-4 hx:pt-6 hx:text-sm [hyphens:auto] hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] hx:ltr:-mr-4 hx:rtl:-ml-4"><p class="hx:mb-4 hx:font-semibold hx:tracking-tight">On this page</p><ul></ul><ul></ul>
|
||||
<div class="hextra-scrollbar hx:sticky hx:top-16 hx:overflow-y-auto hx:pr-4 hx:pt-6 hx:text-sm [hyphens:auto] hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] hx:ltr:-mr-4 hx:rtl:-ml-4"><p class="hx:mb-4 hx:font-semibold hx:tracking-tight">On this page</p><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul>
|
||||
<div class="hx:mt-8 hx:border-t hx:bg-white hx:pt-8 hx:shadow-[0_-12px_16px_white] hx:dark:bg-dark hx:dark:shadow-[0_-12px_16px_#111] hx:sticky hx:bottom-0 hx:flex hx:flex-col hx:items-start hx:gap-2 hx:pb-8 hx:border-gray-200 hx:dark:border-neutral-800 hx:contrast-more:border-t hx:contrast-more:border-neutral-400 hx:contrast-more:shadow-none hx:contrast-more:dark:border-neutral-400">
|
||||
<button aria-hidden="true" id="backToTop" onClick="scrollUp();" class="hx:cursor-pointer hx:transition-all hx:duration-75 hx:opacity-0 hx:text-xs hx:font-medium hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-100 hx:contrast-more:text-gray-800 hx:contrast-more:dark:text-gray-50">
|
||||
<span>Scroll to top</span>
|
||||
@@ -429,6 +429,55 @@ It’s fairly easy It has a broad spectre of usage Server Desktop Mobile devices
|
||||
</ul>
|
||||
<p>Java comes in two parts: <strong>JDK - Java Development Kit</strong> and <strong>JVM - Java Virtual Machine</strong>.</p>
|
||||
<p>There is also a <strong>JRE - Java Runtime Environment</strong>. For example if we want to run our code somewhere on the server we don’t need to compile it there because we have our byte code and we just need JRE to run it.</p>
|
||||
<p>Some of the most popular JVMs right now are:</p>
|
||||
<ul>
|
||||
<li>OpenJDK</li>
|
||||
<li>Eclipse</li>
|
||||
<li>Azul Systems</li>
|
||||
<li>Excelsior JET</li>
|
||||
</ul>
|
||||
<p>The disadvantage of such system is in the connection between JVM and a native processing unit. In case of C++ compiler that we reviewed earlier the source code is compiled directly into machine-code but in case with Java it is compiled into byte-code. And so the problem is to develop such a JVM that would quickly turn our byte-code into machine-code. Anyway it takes extra time. That’s why it mostly will be slower than direct compilation into machine-code. So ultimately while we have the advantage of compiling out code only once, we have the disadvantage of turning byte-code into machine-code slower.</p>
|
||||
<p>None the less, there is a way to speed up this process which is called JIT - Just In Time compilation. How does it work? While our program is running some of the instructions or functions turns directly into processor commands.</p>
|
||||
<h1>What is garbage collection?</h1><p>For example we have <code>int</code> which is represented with 4 bytes of data which is directly stored in memory.</p>
|
||||
<p>
|
||||
<img src="../assets/int-in-memory.svg" alt="int in memory" loading="lazy" />
|
||||
<em>Img. X – <code>int</code> in memory</em></p>
|
||||
<p>But what if we have a <code>String</code>. How many memory cells does this string take? We don’t know. We will say that our <code>String</code> that has length of 5 symbols is stored at <code>0x12347865</code>. We defined an address where this string is located in memory. And somewhere in the memory of our programm will be a large buffer where the 5 cells will be stored.</p>
|
||||
<p>
|
||||
<img src="../assets/string-in-memory.svg" alt="String in memory" loading="lazy" />
|
||||
<em>Img. X – <code>String</code> in memory</em></p>
|
||||
<p>But what do we do with that buffer? We won’t need it forever and so sometimes we need to clear that buffer. In case with C/C++ whoever created the memory for that string is in charge of clearing it. There are also languages with garbage collection such as Java and Python. The purpose of the <strong>Garbage Collector</strong> is to automatically find variables that we no longer need and clear their memory.</p>
|
||||
<p>Suppose we have some code like this.</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">if</span> (someCondition) {
|
||||
</span></span><span style="display:flex;"><span> x <span style="color:#f92672">=</span> <span style="color:#f92672">[</span>1, 3, 7<span style="color:#f92672">]</span> <span style="color:#75715e">// first link </span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// some code here </span>
|
||||
</span></span><span style="display:flex;"><span> y <span style="color:#f92672">=</span> x <span style="color:#75715e">// second link </span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// some code here </span>
|
||||
</span></span><span style="display:flex;"><span>} <span style="color:#f92672">//</span> no links</span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0">
|
||||
<button
|
||||
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
|
||||
title="Copy code"
|
||||
>
|
||||
<div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
<div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p>After the <code>if</code>-statement we no longer need <code>x</code> or <code>y</code>. Every variable in this case <code>x</code> and <code>y</code> is the link to our array (<code>[1, 3, 7]</code>). After we left the <code>if</code>-statement the amount of links to the array is zero, so we can safely delete the array. This approach is implemented in Python. The problem is that if we have to objects linked to one another and there are no external links, they will not be deleted by garbage collector since there link counter is 1.</p>
|
||||
<p>
|
||||
<img src="../assets/edge-case-for-link-counting.svg" alt="Edge case for link couting" loading="lazy" />
|
||||
<em>Img. X – Edge case for link counting</em></p>
|
||||
<p>The advanced way of implementing the garbage collection is <em>traversing the graph of links</em> which is implemented in Java, C# and Go.</p>
|
||||
<h1>What other advantages does Java have?</h1><ul>
|
||||
<li>It’s easy (in terms of syntax)</li>
|
||||
<li>It’s secure and stable</li>
|
||||
<li>It supports Unicode</li>
|
||||
<li>It supports multithreading</li>
|
||||
<li>It has backwards compatibility</li>
|
||||
</ul>
|
||||
<h1>How should Java code look like?</h1><p>You can go and learn about it <a href="https://google.github.io/styleguide/javaguide.html"target="_blank" rel="noopener">here</a>.</p>
|
||||
|
||||
</div>
|
||||
<div class="hx:mt-16"></div>
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user