add important info about strings to tutorial 2

This commit is contained in:
Ellpeck 2019-10-11 12:17:57 +02:00
parent 60b230fc8b
commit 20c3684543

View file

@ -24,8 +24,17 @@ So what we see here is called the `if` condition. It's structured as follows: Yo
The condition can be any statement that can either be `true` (correct) or `false` (incorrect). In this case, we're comparing two numbers with each other; there are several other ways to compare two numbers: `>`, `<`, `>=`, `<=` and `==`, the latter of which means "are the two numbers exactly equal".[^2] The condition can be any statement that can either be `true` (correct) or `false` (incorrect). In this case, we're comparing two numbers with each other; there are several other ways to compare two numbers: `>`, `<`, `>=`, `<=` and `==`, the latter of which means "are the two numbers exactly equal".[^2]
An important thing to note at this point is that this behavior is *different* with `String` variables.[^3] Comparing if two strings are equal by using `==` will not result in the behavior you might expect. Instead, you should compare two strings using `equals()` as follows:
```java
String s1 = "This is some text";
String s2 = "This is also some text";
if (s1.equals(s2)) {
System.out.println("s1 and s2 are equal!");
}
```
## The `boolean` type ## The `boolean` type
A condition like this (which can either be `true` or `false`) can also be stored in a `boolean` variable[^3] whose state can then be checked using a similar `if` statement: A condition like this (which can either be `true` or `false`) can also be stored in a `boolean` variable[^4] whose state can then be checked using a similar `if` statement:
```java ```java
int i = 15; int i = 15;
boolean i27 = i >= 27; boolean i27 = i >= 27;
@ -38,6 +47,13 @@ The above code has the same effect as the code we looked at before. The cool thi
if (!i27) { if (!i27) {
System.out.println("i is NOT greater than or equal to 27!"); System.out.println("i is NOT greater than or equal to 27!");
} }
// As you can see, this is especially useful when comparing strings
String s1 = "This is some text";
String s2 = "This is also some text";
if (!s1.equals(s2)) {
System.out.println("s1 and s2 are NOT equal!");
}
``` ```
Additionally, two boolean variables (or simply two conditions) can be chained together in two ways: Additionally, two boolean variables (or simply two conditions) can be chained together in two ways:
@ -142,4 +158,6 @@ Thanks for reading this tutorial and I hope it helped you out! If you have any f
[^2]: Note that, when *comparing* two numbers, two equals signs `==` are used. This is different from *assigning a value* to a variable, which only uses one equals sign `=`. [^2]: Note that, when *comparing* two numbers, two equals signs `==` are used. This is different from *assigning a value* to a variable, which only uses one equals sign `=`.
[^3]: Named after [George Boole](https://en.wikipedia.org/wiki/George_Boole), a mathematician. [^3]: Why exactly this is the case will be discussed later when we get into object orientation. The different behavior mentioned here is the case for any variable types which aren't so-called *native types*. Any variable whose type starts with an uppercase letter is not a native type, because it derives from a *class*.
[^4]: Named after [George Boole](https://en.wikipedia.org/wiki/George_Boole), a mathematician.