-
-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add concept introductions * [CI] Format code Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0bd8da3
commit b38cdc0
Showing
20 changed files
with
527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: add introduction for abstract concept |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
In Java, data structures that can hold zero or more elements are known as _collections_. An **array** is a collection that has a fixed size/length and whose elements must all be of the same type. Elements can be assigned to an array or retrieved from it using an index. Java arrays are zero-based, meaning that the first element's index is always zero: | ||
|
||
```java | ||
// Declare array with explicit size (size is 2) | ||
int[] twoInts = new int[2]; | ||
|
||
// Assign second element by index | ||
twoInts[1] = 8; | ||
|
||
// Retrieve the second element by index and assign to the int element | ||
int element = twoInts[1]; | ||
``` | ||
|
||
Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value. As the compiler can now tell how many elements the array will have, the length can be omitted: | ||
|
||
```java | ||
// Two equivalent ways to declare and initialize an array (size is 3) | ||
int[] threeIntsV1 = new int[] { 4, 9, 7 }; | ||
int[] threeIntsV2 = { 4, 9, 7 }; | ||
``` | ||
|
||
Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the `Array` class. | ||
|
||
The fact that an array is also a _collection_ means that, besides accessing values by index, you can iterate over _all_ its values using a `foreach` loop: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for(char vowel:vowels) { | ||
// Output the vowel | ||
System.out.print(vowel); | ||
} | ||
|
||
// => aeiou | ||
``` | ||
|
||
If you want more control over which values to iterate over, a `for` loop can be used: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
// Output the vowel | ||
System.out.print(vowels[i]); | ||
} | ||
|
||
// => aei | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Java is a statically-typed language, which means that everything has a type at compile-time. Assigning a value to a name is referred to as defining a variable. A variable is defined by explicitly specifying its type. | ||
|
||
```java | ||
int explicitVar = 10; | ||
``` | ||
|
||
Updating a variable's value is done through the `=` operator. Once defined, a variable's type can never change. | ||
|
||
```java | ||
int count = 1; // Assign initial value | ||
count = 2; // Update to new value | ||
|
||
// Compiler error when assigning different type | ||
// count = false; | ||
``` | ||
|
||
Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. The `class` keyword is used to define a class. | ||
|
||
```java | ||
class Calculator { | ||
// ... | ||
} | ||
``` | ||
|
||
A function within a class is referred to as a _method_. Each method can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. Similarly, the return type must also be made explicit. Values are returned from functions using the `return` keyword. To allow a method to be called by other classes, the `public` access modifier must be added. | ||
|
||
```java | ||
class Calculator { | ||
public int add(int x, int y) { | ||
return x + y; | ||
} | ||
} | ||
``` | ||
|
||
Invoking a method is done by specifying its class and method name and passing arguments for each of the method's parameters. | ||
|
||
```java | ||
int sum = new Calculator().add(1, 2); | ||
``` | ||
|
||
Scope in Java is defined between the `{` and `}` characters. | ||
|
||
Java supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. | ||
|
||
[object-oriented-programming]: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: add introduction for boolean concept |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Booleans in Java are represented by the `boolean` type, which values can be either `true` or `false`. | ||
|
||
Java supports three boolean operators: `!` (NOT), `&&` (AND), and `||` (OR). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
The Java `char` type represents the smallest addressable components of text. | ||
Multiple `char`s can comprise a string such as `"word"` or `char`s can be | ||
processed independently. Their literals have single quotes e.g. `'A'`. | ||
|
||
Java `char`s support Unicode encoding so in addition to the latin character set | ||
pretty much all the writing systems in use world can be represented, | ||
e.g. ancient greek `'β'`. | ||
|
||
There are many builtin library methods to inspect and manipulate `char`s. These | ||
can be found as static methods of the `java.lang.Character` class. | ||
|
||
`char`s are sometimes used in conjunction with a `StringBuilder` object. | ||
This object has methods that allow a string to be constructed | ||
character by character and manipulated. At the end of the process | ||
`toString` can be called on it to output a complete string. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). The fields and methods of a class are known as its _members_. | ||
|
||
Access to members can be controlled through access modifiers, the two most common ones being: | ||
|
||
- `public`: the member can be accessed by any code (no restrictions). | ||
- `private`: the member can only be accessed by code in the same class. | ||
|
||
You can think of a class as a template for creating instances of that class. To create an instance of a class (also known as an _object_), the `new` keyword is used: | ||
|
||
```java | ||
class Car { | ||
} | ||
|
||
// Create two car instances | ||
Car myCar = new Car(); | ||
Car yourCar = new Car(); | ||
``` | ||
|
||
Fields have a type and a name (defined in camelCase) and can be defined anywhere in a class (by convention cased in PascalCase): | ||
|
||
```java | ||
class Car { | ||
// Accessible by anyone | ||
public int weight; | ||
|
||
// Only accessible by code in this class | ||
private String color; | ||
} | ||
``` | ||
|
||
One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it wll be set to its type's default value. An instance's field values can be accessed and updated using dot-notation. | ||
|
||
```java | ||
class Car { | ||
// Will be set to specified value | ||
public int weight = 2500; | ||
|
||
// Will be set to default value (0) | ||
public int year; | ||
} | ||
|
||
Car newCar = new Car(); | ||
newCar.weight; // => 2500 | ||
newCar.year; // => 0 | ||
|
||
// Update value of the field | ||
newCar.year = 2018; | ||
``` | ||
|
||
Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be `void`: | ||
|
||
```java | ||
class CarImporter { | ||
private int carsImported; | ||
|
||
public void ImportCars(int numberOfCars) | ||
{ | ||
// Update private field from public method | ||
carsImported = carsImported + numberOfCars; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
## conditionals-if | ||
|
||
## Logical Operators | ||
|
||
Java supports the three logical operators `&&` (AND), `||` (OR), and `!` (NOT). | ||
|
||
## If statement | ||
|
||
The underlying type of any conditional operation is the `boolean` type, which can have the value of `true` or `false`. Conditionals are often used as flow control mechanisms to check for various conditions. For checking a particular case an `if` statement can be used, which executes its code if the underlying condition is `true` like this: | ||
|
||
```java | ||
int val; | ||
|
||
if(val == 9) { | ||
// conditional code | ||
} | ||
``` | ||
|
||
In scenarios involving more than one case many `if` statements can be chained together using the `else if` and `else` statements. | ||
|
||
```java | ||
if(val == 10) { | ||
// conditional code | ||
} else if(val == 17) { | ||
// conditional code | ||
} else { | ||
// executes when val is different from 10 and 17 | ||
} | ||
``` | ||
|
||
## Switch statement | ||
|
||
Java also provides a `switch` statement for scenarios with multiple options. | ||
|
||
```java | ||
int val; | ||
|
||
// switch statement on variable content | ||
switch(val) { | ||
case 1: | ||
// conditional code | ||
break; | ||
case 2: case 3: case 4: | ||
// conditional code | ||
break; | ||
default: | ||
// if all cases fail | ||
break; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: add introduction for conditionals concept |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. A constructor is a special type of method whose goal is to initialize a newly created instance. Constructors look like regular methods, but without a return type and with a name that matches the classes' name. | ||
|
||
```java | ||
class Library { | ||
private int books; | ||
|
||
public Library() { | ||
// Initialize the books field | ||
this.books = 10; | ||
} | ||
} | ||
|
||
// This will call the constructor | ||
var library = new Library(); | ||
``` | ||
|
||
Like regular methods, constructors can have parameters. Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. Arguments can be passed to constructors just like passing arguments to regular methods. | ||
|
||
```java | ||
class Building { | ||
private int numberOfStories; | ||
private int totalHeight; | ||
|
||
public Building(int numberOfStories, double storyHeight) { | ||
this.numberOfStories = numberOfStories; | ||
this.totalHeight = numberOfStories * storyHeight; | ||
} | ||
} | ||
|
||
// Call a constructor with two arguments | ||
var largeBuilding = new Building(55, 6.2); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
## arrays | ||
|
||
In Java, data structures that can hold zero or more elements are known as _collections_. An **array** is a collection that has a fixed size/length and whose elements must all be of the same type. Elements can be assigned to an array or retrieved from it using an index. Java arrays are zero-based, meaning that the first element's index is always zero: | ||
|
||
```java | ||
// Declare array with explicit size (size is 2) | ||
int[] twoInts = new int[2]; | ||
|
||
// Assign second element by index | ||
twoInts[1] = 8; | ||
|
||
// Retrieve the second element by index and assign to the int element | ||
int element = twoInts[1]; | ||
``` | ||
|
||
Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value. As the compiler can now tell how many elements the array will have, the length can be omitted: | ||
|
||
```java | ||
// Two equivalent ways to declare and initialize an array (size is 3) | ||
int[] threeIntsV1 = new int[] { 4, 9, 7 }; | ||
int[] threeIntsV2 = { 4, 9, 7 }; | ||
``` | ||
|
||
Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the `Array` class. | ||
|
||
The fact that an array is also a _collection_ means that, besides accessing values by index, you can iterate over _all_ its values using a `foreach` loop: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for(char vowel:vowels) { | ||
// Output the vowel | ||
System.out.print(vowel); | ||
} | ||
|
||
// => aeiou | ||
``` | ||
|
||
If you want more control over which values to iterate over, a `for` loop can be used: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
// Output the vowel | ||
System.out.print(vowels[i]); | ||
} | ||
|
||
// => aei | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
## arrays | ||
|
||
In Java, data structures that can hold zero or more elements are known as _collections_. An **array** is a collection that has a fixed size/length and whose elements must all be of the same type. Elements can be assigned to an array or retrieved from it using an index. Java arrays are zero-based, meaning that the first element's index is always zero: | ||
|
||
```java | ||
// Declare array with explicit size (size is 2) | ||
int[] twoInts = new int[2]; | ||
|
||
// Assign second element by index | ||
twoInts[1] = 8; | ||
|
||
// Retrieve the second element by index and assign to the int element | ||
int element = twoInts[1]; | ||
``` | ||
|
||
Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value. As the compiler can now tell how many elements the array will have, the length can be omitted: | ||
|
||
```java | ||
// Two equivalent ways to declare and initialize an array (size is 3) | ||
int[] threeIntsV1 = new int[] { 4, 9, 7 }; | ||
int[] threeIntsV2 = { 4, 9, 7 }; | ||
``` | ||
|
||
Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the `Array` class. | ||
|
||
The fact that an array is also a _collection_ means that, besides accessing values by index, you can iterate over _all_ its values using a `foreach` loop: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for(char vowel:vowels) { | ||
// Output the vowel | ||
System.out.print(vowel); | ||
} | ||
|
||
// => aeiou | ||
``` | ||
|
||
If you want more control over which values to iterate over, a `for` loop can be used: | ||
|
||
```java | ||
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
// Output the vowel | ||
System.out.print(vowels[i]); | ||
} | ||
|
||
// => aei | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: add introduction for functions concept |
Oops, something went wrong.