mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-04 23:44:11 +02:00
feat(resources): split up topics and languages
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
---
|
||||
authors:
|
||||
- "AstronautEVA#0331"
|
||||
title: "Classes"
|
||||
created_at: 2019/10/20
|
||||
---
|
||||
|
||||
## What is a class?
|
||||
|
||||
Oftentimes in programs, it is necessary to represent objects from the real world as computer data.
|
||||
For example, if someone is writing a program that simulates a zoo, they will need to create some animals.
|
||||
A class is a way to write the blueprint of whatever you are trying to represent; in our case it would be a blueprint of an animal.
|
||||
|
||||
Let us investigate what an animal class might look like.
|
||||
First we will declare attributes that an animal would have. There are many attributes we could list, but let's choose 3.
|
||||
The animal will have a
|
||||
|
||||
- name (because zoos name their animals)
|
||||
- age
|
||||
- weight
|
||||
|
||||
All of these words are nouns which is a hint that they should be implemented as variables.
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public String name;
|
||||
public int age;
|
||||
public float weight;
|
||||
}
|
||||
```
|
||||
|
||||
Notice that we have only declared these variables, we did **not** initialize them (set them equal to a value).
|
||||
|
||||
Okay so we have added attributes to our animal which is great, but we want our animal to actually be able to _do_ things. Let's pick 2
|
||||
things we want our animal to do.
|
||||
Our animal should
|
||||
|
||||
- move
|
||||
- sleep
|
||||
|
||||
These are verbs which means they likely need to be implemented as methods.
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public String name;
|
||||
public int age;
|
||||
public float weight;
|
||||
|
||||
public void move() {
|
||||
System.out.println("The animal moves.");
|
||||
}
|
||||
|
||||
public void sleep() {
|
||||
System.out.println("The animal sleeps.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Class vs Object
|
||||
|
||||
Remember this is only the description of what an animal is, not a specific animal. A specific animal such as Max is one instance of a class and is called an object. It has values assigned to its attributes like the name is Max and the age is 5. You can call methods on the object such as move or sleep. And there can be an infinite number of objects constructed from a class. To create objects from your class, you need something called a constructor.
|
||||
|
||||
The format of a constructor looks like this:
|
||||
|
||||
```java
|
||||
public ClassName() {
|
||||
}
|
||||
```
|
||||
|
||||
It should be public, have no return type, and the name of the method should be the same name as the class.
|
||||
|
||||
Since the constructor is called when you want to create an object of a class, and you will likely want that object to have specific values for its attributes (such as making the name be "Max"), you need the constructor to initialize the variables of the object. One way to do this is by giving the constructor parameters that you will use to pass in the values you want for your object.
|
||||
Modifying our constructor template from above to do this might look like:
|
||||
|
||||
```java
|
||||
public ClassName(String myObjectsName) {
|
||||
name = myObjectsName;
|
||||
}
|
||||
```
|
||||
|
||||
Now that you've got a general idea of what a constructor should look like, we will apply this to our Animal class.
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public String name;
|
||||
public int age;
|
||||
public float weight;
|
||||
|
||||
// here is the constructor
|
||||
public Animal(String name, int age, float weight) {
|
||||
/*
|
||||
Since we used the same name for the parameters as we did the class variables, we have
|
||||
to use the word "this" to differentiate between which variable we are talking about.
|
||||
"this" refers to the Animal class, so stating "this.name" refers to the name variable
|
||||
declared at the top of the Animal class.
|
||||
Stating "name" without "this" refers to the parameter variable called name.
|
||||
*/
|
||||
this.name = name; // set the class variable called name equal to the value of the parameter called name
|
||||
this.age = age;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
System.out.println("The animal moves.");
|
||||
}
|
||||
|
||||
public void sleep() {
|
||||
System.out.println("The animal sleeps.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Our class is finally ready to use! Let's add in a main method to our class so we can create some objects and see what happens.
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public String name;
|
||||
public int age;
|
||||
public float weight;
|
||||
|
||||
public Animal(String name, int age, float weight) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
System.out.println("The animal moves.");
|
||||
}
|
||||
|
||||
public void sleep() {
|
||||
System.out.println("The animal sleeps.");
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
// create a variable of type Animal by calling the constructor of the Animal class
|
||||
Animal myAnimal = new Animal("Max", 5, 10.3);
|
||||
|
||||
// access the variables of the object you just created
|
||||
myAnimal.name; // returns "Max"
|
||||
myAnimal.age; // returns 5
|
||||
myAnimal.weight; // returns 10.3
|
||||
|
||||
// change a variable of the object
|
||||
myAnimal.name = "JoJo";
|
||||
myAnimal.name; // returns "JoJo"
|
||||
|
||||
// call the functions of the object you just created
|
||||
myAnimal.move(); // returns "The animal moves."
|
||||
myAnimal.sleep(); // returns "The animal sleeps."
|
||||
|
||||
// create as many Animal objects as you want
|
||||
Animal foo = new Animal("Polly", 34, 1.4);
|
||||
Animal bar = new Animal("Sophie", 8, 89.7);
|
||||
|
||||
foo.name; // returns "Polly"
|
||||
bar.name; // returns "Sophie"
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,144 @@
|
||||
---
|
||||
authors:
|
||||
- "supergrecko#3434"
|
||||
created_at: "2019/10/04"
|
||||
title: Generics
|
||||
---
|
||||
|
||||
## What are generics?
|
||||
|
||||
Generics is a concept in programming which allows passing a type argument to a class or a method.
|
||||
|
||||
Java's standard library makes heavy use of generics to reduce repetition and to provide flexibility.
|
||||
|
||||
Here's an example where Java uses generics.
|
||||
|
||||
```java
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
List<String> = new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this example we are passing the String type to the List class. We can now say we have "A List of Strings".
|
||||
|
||||
## Why should we use generics?
|
||||
|
||||
By using generics we can provide type safety while having reusable code.
|
||||
|
||||
Lets say we want to create coffee capsule, We want one holder for Espresso capsules and one for Cappuccino capsules. This is one way we can implement this coffee capsule holder.
|
||||
|
||||
For this example we will use these classes to represent our coffee capsules.
|
||||
|
||||
```java
|
||||
class EspressoCapsule {}
|
||||
class CappuccinoCapsule {}
|
||||
```
|
||||
|
||||
```java
|
||||
import java.util.ArrayList;
|
||||
|
||||
// An espresso capsule holder
|
||||
class EspressoHolder {
|
||||
// This is a generic. ArrayList of EspressoCapsules
|
||||
private ArrayList<EspressoCapsule> capsules = new ArrayList<EspressoCapsule>();
|
||||
|
||||
public EspressoHolder(EspressoCapsule capsule) {
|
||||
this.capsules.add(capsule);
|
||||
}
|
||||
}
|
||||
|
||||
// A cappuccino capsule holder
|
||||
class CappuccinoHolder {
|
||||
// This is a generic. ArrayList of CappuccinoCapsules
|
||||
private ArrayList<CappuccinoCapsule> capsules = new ArrayList<CappuccinoCapsule>();
|
||||
|
||||
public CappuccinoHolder(CappuccinoCapsule capsule) {
|
||||
this.capsules.add(capsule);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
We can now use these capsule holders like this:
|
||||
|
||||
```java
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
EspressoHolder espresso = new EspressoHolder(new EspressoCapsule());
|
||||
CappuccinoHolder cappuccino = new CappuccinoHolder(new CappuccinoCapsule());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The only concern here is that we're repeating a lot of code. Almost the entire `EspressoHolder` implementation is copied into the `CappuccinoHolder` implementation.
|
||||
|
||||
Next up we'll take a look at an implementation which uses generics to avoid this repetitive behavior.
|
||||
|
||||
## Optimizing our Coffee brewer with Generics
|
||||
|
||||
What if we could have a single Holder class for both types of capsules? Let's implement that by using generics.
|
||||
|
||||
Let's start off by removing both the `CappuccinoHolder` and the `EspressoHolder` classes. We're now left with this:
|
||||
|
||||
```
|
||||
import java.util.ArrayList;
|
||||
|
||||
class EspressoCapsule {}
|
||||
class CappuccinoCapsule {}
|
||||
```
|
||||
|
||||
We can now implement our brand new `Holder` class. We'll start of by making the class accept a generic type.
|
||||
|
||||
```java
|
||||
class Holder<T> {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
This creates a new data type inside the Holder class named `T`.
|
||||
|
||||
We now expect users to create a `Holder` instance via this syntax. String in this example can be replaced with any type.
|
||||
|
||||
```java
|
||||
Holder<String> holder = new Holder<String>();
|
||||
```
|
||||
|
||||
In this scenario, `T` inside the Holder instance will be the String type.
|
||||
|
||||
Let's add our ArrayList again.
|
||||
|
||||
```java
|
||||
class Holder<T> {
|
||||
private ArrayList<T> capsules = new ArrayList<T>();
|
||||
}
|
||||
```
|
||||
|
||||
Notice how we're passing T further down into the ArrayList generic? That means the ArrayList generic type will also change based on the Holder generic type.
|
||||
|
||||
We can now finish up our holder implementation like this.
|
||||
|
||||
```java
|
||||
class Holder<T> {
|
||||
private ArrayList<T> capsules = new ArrayList<T>();
|
||||
|
||||
public Holder(T capsule) {
|
||||
this.capsules.add(capsule);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Because T is a qualified type inside our Holder class we can also make the constructor accept the T type as an argument.
|
||||
|
||||
This is our new way of using the Holder class.
|
||||
|
||||
```java
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
Holder<CappuccinoCapsule> cappuccino = new Holder<CappuccinoCapsule>(new CappuccinoCapsule());
|
||||
Holder<EspressoCapsule> espresso = new Holder<EspressoCapsule>(new EspressoCapsule());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
We just reduces the amount code required to hold our two capsule types in by 50%. The cool thing is that we're now able to create as many capsule types as we want while being able to stick to our Holder implementation. We could have 20 different capsule types, our `Holder` class would be able to take care of all of them.
|
||||
@@ -0,0 +1,157 @@
|
||||
---
|
||||
authors:
|
||||
- "AstronautEVA#0331"
|
||||
title: "Inheritance"
|
||||
created_at: 2019/10/24
|
||||
recommended_reading:
|
||||
- java/class
|
||||
---
|
||||
|
||||
## What is Inheritance?
|
||||
|
||||
Inheritance is the idea that when two classes have similar implementations, rather than duplicating code for each class one can obtain it from the other. Much like a human child will inherit traits from their parents, a child class will inherit data from
|
||||
its parent class.
|
||||
Here is an example of a class hierarchy:
|
||||
|
||||
```sh
|
||||
Animal
|
||||
_____|______
|
||||
| |
|
||||
Bird Reptile
|
||||
```
|
||||
|
||||
Animal is the parent class (also called the superclass), Dog and Bird are both child classes (also called subclasses).
|
||||
**NOTE:** A child class will only have 1 direct parent (known as single inheritance), not more.
|
||||
Here is an example of an illegal class hierarchy in Java:
|
||||
|
||||
```sh
|
||||
// THIS IS ILLEGAL
|
||||
// A child can only have 1 parent.
|
||||
|
||||
Mammal Fish
|
||||
|____________|
|
||||
|
|
||||
Otter
|
||||
```
|
||||
|
||||
However, there can be many children.
|
||||
|
||||
```sh
|
||||
Animal
|
||||
_____|______
|
||||
| |
|
||||
Bird Reptile
|
||||
| ____|____________
|
||||
| | | |
|
||||
Eagle Snake Frog Lizard
|
||||
```
|
||||
|
||||
Now Eagle is a child of Bird, and Bird is the parent of Eagle. Bird is also a child of Animal. Similarly Snake, Frog, and Lizard are
|
||||
children of Reptile, Reptile is each one's parent, and Reptile is also the child of Animal.
|
||||
|
||||
To understand what the children inherit, let's define our Animal class.
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public int age;
|
||||
}
|
||||
```
|
||||
|
||||
This is a very simple class and only has one field. In our hierarchy we see that Bird is a child of Animal. To define a subclass we
|
||||
must use the keyword `extends` in the format of `public class ChildClass extends ParentClass`. The `extends` keyword signifies an is-a
|
||||
relationship. The child class **is a** type of the parent class. A bird **is a** type of animal.
|
||||
|
||||
```java
|
||||
public class Bird extends Animal {
|
||||
}
|
||||
```
|
||||
|
||||
As you can see we have not placed anything in the Bird class. But this does not mean the Bird class has no fields or methods. Since
|
||||
Bird inherits from Animal, it has the age field defined in the Animal class. **NOTE:** The subclass will inherit all the _public_ fields and methods of the parent class, but never the constructors.
|
||||
|
||||
```java
|
||||
Animal myAnimal = new Animal();
|
||||
myAnimal.age //returns 0
|
||||
|
||||
Bird myBird = new Bird();
|
||||
myBird.age //returns 0
|
||||
```
|
||||
|
||||
Let's say we want to keep track of the size of a bird's wingspan. We will implement this as a variable called `wingspan`. Since
|
||||
only birds have a wingspan and not every animal, we will put this variable in the Bird class.
|
||||
|
||||
```java
|
||||
public class Bird extends Animal {
|
||||
public float wingspan;
|
||||
}
|
||||
```
|
||||
|
||||
If we create an object of type Bird it will have 2 variables. If we create an object of type Animal it will have 1.
|
||||
|
||||
```java
|
||||
Animal myAnimal = new Animal();
|
||||
myAnimal.age //returns 0
|
||||
myAnimal.wingspan //produces an error because the variable doesn't exist
|
||||
|
||||
Bird myBird = new Bird();
|
||||
myBird.age //returns 0
|
||||
myBird.wingspan //returns 0.0
|
||||
```
|
||||
|
||||
Things can get a little bit confusing when trying to understand what you can access from an object. Just remember that you will only be able to access the fields/methods that correspond to the object's _type_. Also, inheritance works in one direction; the children reach up to get the fields/methods from their parent, but the parent cannot reach down to get fields/methods from their children.
|
||||
|
||||
```java
|
||||
Animal myAnimal = new Animal(); //myAnimal only has the age variable and you can access it
|
||||
Animal myAnimal = new Bird(); //you can only access the age variable since the object is of type Animal. the object does have the wingspan variable due to calling the Bird() constructor, but you cannot access it.
|
||||
|
||||
Bird myBird = new Bird(); //myBird has both the age and wingspan variables since you called the Bird() constructor and Bird inherits from Animal. you can access both variables since the object type is Bird.
|
||||
Bird myBird = new Animal(); //this produces an error because the object type is Bird which means constructor Animal() does not exist (constructors are not inherited).
|
||||
```
|
||||
|
||||
All of the above examples using public variables also works when using public methods.
|
||||
If we define a method in our superclass:
|
||||
|
||||
```java
|
||||
public class Animal {
|
||||
public int age;
|
||||
|
||||
public void sleep() {
|
||||
System.out.println("The animal sleeps.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
It can be used in our subclass.
|
||||
|
||||
```java
|
||||
Animal myAnimal = new Animal();
|
||||
myAnimal.sleep(); // returns "The animal sleeps."
|
||||
|
||||
Bird myBird = new Bird();
|
||||
myBird.sleep(); // returns "The animal sleeps."
|
||||
```
|
||||
|
||||
But if we define a method in our subclass:
|
||||
|
||||
```java
|
||||
public class Bird extends Animal {
|
||||
public float wingspan;
|
||||
|
||||
public void chirp(){
|
||||
System.out.println("The bird chirps.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
It cannot be used in our superclass.
|
||||
|
||||
```java
|
||||
Animal myAnimal = new Animal();
|
||||
myAnimal.chirp(); // error, the method does not exist
|
||||
|
||||
Bird myBird = new Bird();
|
||||
myBird.chirp(); // returns "The bird chirps."
|
||||
```
|
||||
|
||||
One final thing to note about inheritance is that in Java, every class inherits from a class called Object (java.lang.Object) even when
|
||||
it is not explicitly stated via `extends`. You can read what your class inherits from java.lang.Object in [the official docs](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html).
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
authors:
|
||||
- "veksen#1565"
|
||||
created_at: 2019/12/15
|
||||
title: Java
|
||||
---
|
||||
|
||||
###### Documentation
|
||||
|
||||
- [Java docs](https://docs.oracle.com/en/java/javase/11/)
|
||||
|
||||
###### Free Books
|
||||
|
||||
- [https://goo.gl/WZTC9C](https://goo.gl/WZTC9C)
|
||||
|
||||
###### Video
|
||||
|
||||
- [EJ Media](https://goo.gl/tC1KwC)
|
||||
- [Derek Banas](https://goo.gl/9eVFkE)
|
||||
- [Free With Registration | Cave of Programming](https://goo.gl/NXTo4H)
|
||||
|
||||
###### Guides
|
||||
|
||||
- [Learn By Examples | Beginnersbook.com](https://goo.gl/3nDWeQ)
|
||||
|
||||
###### Useful Repositories
|
||||
|
||||
- [Frameworks](https://goo.gl/EoLvrH)
|
||||
- [Design patterns](https://goo.gl/wT7SfQ)
|
||||
|
||||
###### Other
|
||||
|
||||
- [History of Java](https://en.wikipedia.org/wiki/Java_version_history)
|
||||
- [Wikibooks](https://en.wikibooks.org/wiki/Java_Programming/History)
|
||||
- [Timeline](https://oracle.com.edgesuite.net/timeline/java/)
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
authors:
|
||||
- "KaeseKuchenDEV#6322"
|
||||
title: "An introduction to Streams"
|
||||
created_at: 2019/12/15
|
||||
external_resources:
|
||||
- text: Java8 Stream Tutorial
|
||||
href: https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
|
||||
---
|
||||
|
||||
Basically everyone, who spent a few hours coding some Java came across a situation like this:
|
||||
|
||||
```java
|
||||
int[] intArray = {1,2,3,4,5};
|
||||
|
||||
for(int i = 0; i < intArray.length; i++) {
|
||||
System.out.println(intArray[i]);
|
||||
}
|
||||
```
|
||||
|
||||
So, you need to print every element in an array, a list or any other data structure. But there has to be a nicer way to do this, especially with less code. Since Java 8 we are able to use so called "Streams" in Java. In particular the same result as from the code above can be achieved with this code:
|
||||
|
||||
```java
|
||||
int[] intArray = {1,2,3,4,5};
|
||||
|
||||
Arrays.stream(intArray).forEach(n -> System.out.println(n));
|
||||
```
|
||||
|
||||
You just made one line out of three. But how does this work? `int[] intArray = {1,2,3,4,5};` does just declare and initialize a new array of integers. This stays the same as before, because you can use the `Arrays.stream()` method for every Array, [as it works with generic Arrays](https://www.mkyong.com/java8/java-how-to-convert-array-to-stream/). This method creates a [Stream Object](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html), that has several methods to perform operations on an array.
|
||||
|
||||
## Sorting
|
||||
|
||||
Now maybe you have a list instead of an array and you also want the list to get sorted before you print it. Without Streams you would first have to sort the list and then iterate through it. Way too many lines! Using Streams you can simply do:
|
||||
|
||||
```java
|
||||
ArrayList<Integer> intList = new ArrayList<>();
|
||||
intList.add(4);
|
||||
intList.add(8);
|
||||
intList.add(3);
|
||||
intList.add(6);
|
||||
|
||||
intList.stream().sorted().forEach(n -> System.out.print(n + ", "));
|
||||
```
|
||||
|
||||
At the end, you have again only one line, for sorting and printing every element in a list. Due to the fact that Streams mostly use Method Chaining, you could perform a bunch of operations.
|
||||
|
||||
## Filtering
|
||||
|
||||
Now you are able to print out every element of an array, a list, a stack, etc. sorted as well as unsorted. The last essential operation is, how to filter your array/list. One often has to only get specific elements of an array. Using Streams you could do it like this:
|
||||
|
||||
```java
|
||||
String[] stringArray = {"The", "programmers", "Hangout"};
|
||||
|
||||
Arrays.stream(stringArray).filter(s -> Character.isUpperCase(s.charAt(0))).forEach(n -> System.out.println(n));
|
||||
```
|
||||
|
||||
This prints:
|
||||
`The`
|
||||
`Hangout`
|
||||
Since those two strings of the array begin with an uppercase letter.
|
||||
Basically many methods of Stream use [lambda expressions](https://www.geeksforgeeks.org/lambda-expressions-java-8/). Using these in the `filter()` method, you can create very specific filters for your array, which can be compared to database operations in for Example SQL.
|
||||
|
||||
These are only a few examples on how to use Streams in Java, but those can already be very powerful and can make your code look way better and shorter. If you want to dive deeper into Streams, I would recommend you: https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/, as it deals with most of the possibilities of Streams.
|
||||
Reference in New Issue
Block a user