Skip to content

Commit b38cdc0

Browse files
ErikSchierboomgithub-actions[bot]
authored andcommitted
Add concept introductions
* Add concept introductions * [CI] Format code Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0bd8da3 commit b38cdc0

20 files changed

Lines changed: 527 additions & 0 deletions

File tree

concepts/abstract/introduction.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: add introduction for abstract concept

concepts/arrays/introduction.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
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:
2+
3+
```java
4+
// Declare array with explicit size (size is 2)
5+
int[] twoInts = new int[2];
6+
7+
// Assign second element by index
8+
twoInts[1] = 8;
9+
10+
// Retrieve the second element by index and assign to the int element
11+
int element = twoInts[1];
12+
```
13+
14+
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:
15+
16+
```java
17+
// Two equivalent ways to declare and initialize an array (size is 3)
18+
int[] threeIntsV1 = new int[] { 4, 9, 7 };
19+
int[] threeIntsV2 = { 4, 9, 7 };
20+
```
21+
22+
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.
23+
24+
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:
25+
26+
```java
27+
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
28+
29+
for(char vowel:vowels) {
30+
// Output the vowel
31+
System.out.print(vowel);
32+
}
33+
34+
// => aeiou
35+
```
36+
37+
If you want more control over which values to iterate over, a `for` loop can be used:
38+
39+
```java
40+
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
41+
42+
for (int i = 0; i < 3; i++) {
43+
// Output the vowel
44+
System.out.print(vowels[i]);
45+
}
46+
47+
// => aei
48+
```

concepts/basics/introduction.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
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.
2+
3+
```java
4+
int explicitVar = 10;
5+
```
6+
7+
Updating a variable's value is done through the `=` operator. Once defined, a variable's type can never change.
8+
9+
```java
10+
int count = 1; // Assign initial value
11+
count = 2; // Update to new value
12+
13+
// Compiler error when assigning different type
14+
// count = false;
15+
```
16+
17+
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.
18+
19+
```java
20+
class Calculator {
21+
// ...
22+
}
23+
```
24+
25+
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.
26+
27+
```java
28+
class Calculator {
29+
public int add(int x, int y) {
30+
return x + y;
31+
}
32+
}
33+
```
34+
35+
Invoking a method is done by specifying its class and method name and passing arguments for each of the method's parameters.
36+
37+
```java
38+
int sum = new Calculator().add(1, 2);
39+
```
40+
41+
Scope in Java is defined between the `{` and `}` characters.
42+
43+
Java supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`.
44+
45+
[object-oriented-programming]: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

concepts/boolean/introduction.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: add introduction for boolean concept

concepts/booleans/introduction.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Booleans in Java are represented by the `boolean` type, which values can be either `true` or `false`.
2+
3+
Java supports three boolean operators: `!` (NOT), `&&` (AND), and `||` (OR).

concepts/chars/introduction.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The Java `char` type represents the smallest addressable components of text.
2+
Multiple `char`s can comprise a string such as `"word"` or `char`s can be
3+
processed independently. Their literals have single quotes e.g. `'A'`.
4+
5+
Java `char`s support Unicode encoding so in addition to the latin character set
6+
pretty much all the writing systems in use world can be represented,
7+
e.g. ancient greek `'β'`.
8+
9+
There are many builtin library methods to inspect and manipulate `char`s. These
10+
can be found as static methods of the `java.lang.Character` class.
11+
12+
`char`s are sometimes used in conjunction with a `StringBuilder` object.
13+
This object has methods that allow a string to be constructed
14+
character by character and manipulated. At the end of the process
15+
`toString` can be called on it to output a complete string.

concepts/classes/introduction.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
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_.
2+
3+
Access to members can be controlled through access modifiers, the two most common ones being:
4+
5+
- `public`: the member can be accessed by any code (no restrictions).
6+
- `private`: the member can only be accessed by code in the same class.
7+
8+
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:
9+
10+
```java
11+
class Car {
12+
}
13+
14+
// Create two car instances
15+
Car myCar = new Car();
16+
Car yourCar = new Car();
17+
```
18+
19+
Fields have a type and a name (defined in camelCase) and can be defined anywhere in a class (by convention cased in PascalCase):
20+
21+
```java
22+
class Car {
23+
// Accessible by anyone
24+
public int weight;
25+
26+
// Only accessible by code in this class
27+
private String color;
28+
}
29+
```
30+
31+
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.
32+
33+
```java
34+
class Car {
35+
// Will be set to specified value
36+
public int weight = 2500;
37+
38+
// Will be set to default value (0)
39+
public int year;
40+
}
41+
42+
Car newCar = new Car();
43+
newCar.weight; // => 2500
44+
newCar.year; // => 0
45+
46+
// Update value of the field
47+
newCar.year = 2018;
48+
```
49+
50+
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`:
51+
52+
```java
53+
class CarImporter {
54+
private int carsImported;
55+
56+
public void ImportCars(int numberOfCars)
57+
{
58+
// Update private field from public method
59+
carsImported = carsImported + numberOfCars;
60+
}
61+
}
62+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction
2+
3+
## conditionals-if
4+
5+
## Logical Operators
6+
7+
Java supports the three logical operators `&&` (AND), `||` (OR), and `!` (NOT).
8+
9+
## If statement
10+
11+
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:
12+
13+
```java
14+
int val;
15+
16+
if(val == 9) {
17+
// conditional code
18+
}
19+
```
20+
21+
In scenarios involving more than one case many `if` statements can be chained together using the `else if` and `else` statements.
22+
23+
```java
24+
if(val == 10) {
25+
// conditional code
26+
} else if(val == 17) {
27+
// conditional code
28+
} else {
29+
// executes when val is different from 10 and 17
30+
}
31+
```
32+
33+
## Switch statement
34+
35+
Java also provides a `switch` statement for scenarios with multiple options.
36+
37+
```java
38+
int val;
39+
40+
// switch statement on variable content
41+
switch(val) {
42+
case 1:
43+
// conditional code
44+
break;
45+
case 2: case 3: case 4:
46+
// conditional code
47+
break;
48+
default:
49+
// if all cases fail
50+
break;
51+
}
52+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: add introduction for conditionals concept
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
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.
2+
3+
```java
4+
class Library {
5+
private int books;
6+
7+
public Library() {
8+
// Initialize the books field
9+
this.books = 10;
10+
}
11+
}
12+
13+
// This will call the constructor
14+
var library = new Library();
15+
```
16+
17+
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.
18+
19+
```java
20+
class Building {
21+
private int numberOfStories;
22+
private int totalHeight;
23+
24+
public Building(int numberOfStories, double storyHeight) {
25+
this.numberOfStories = numberOfStories;
26+
this.totalHeight = numberOfStories * storyHeight;
27+
}
28+
}
29+
30+
// Call a constructor with two arguments
31+
var largeBuilding = new Building(55, 6.2);
32+
```

0 commit comments

Comments
 (0)