You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/hello-world.md
+14-30Lines changed: 14 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,6 @@ layout: default
3
3
title: Hello World
4
4
---
5
5
# Hello World
6
-
> # WIP
7
-
8
6
In the first lesson here, we'll learn the basics of Java.
9
7
10
8
## The First Program
@@ -63,37 +61,23 @@ Next, let's make it a bit more complicated. Look at the next piece of code, befo
63
61
```java
64
62
System.out.println(6+7);
65
63
```
66
-
What it puts on the screen is the number `13`, which you probably guessed it would do. What you can learn from this is that you can do some math in java like so.
67
-
### Comments rq
68
-
To leave a comment on a line of code in Java, you can put a `//` before it and the rest of line will be the comment.
69
-
```java
70
-
System.out.println(2*3); // Prints 6
71
-
System.out.println("Do I have rizz?");
72
-
// This code won't run because its in a comment
73
-
// System.out.println("I don't run");
74
-
```
75
-
### Quick intro to variables
76
-
We'll be exploring variables more in depth later (in fact it's the next lesson), but I'd like to at least introduce you to them.
64
+
What it puts on the screen is the number `13`, which you probably guessed it would do. What you can learn from this is that you can do some math in java by using some basic symbols. Here's some more examples:
77
65
```java
78
-
int x =6+7;
79
-
System.out.println(x);
80
-
```
81
-
The code you see above achieves exactly the same result as before, printing `13`. However, as you can see, it assigns it to a variable first. We're going to skip the `int` part for now, but you can see that the variable x is assigned the result of the evaluated expression to the right. You can think of them as variables from math, except they always have a value.
66
+
System.out.println(5*3+1); // Prints 16
82
67
83
-
Below here are a few more examples to get you used to them.
68
+
System.out.println(6/2* (1+2)); // Prints 9
69
+
```
70
+
## Comments
71
+
I've also included comments the previous example, any text put after a `//` will not be run. It's helpful for describing what code does inline without having to create a word doc or something. You'll see them all the time in code snippets to tell you what going on but later on, we'll start to not write as many because they will get redundant. In the robot's code, we'll normally write them to explain complicated logic or strange decisions. You can do multi line comments like so, if you would like.
84
72
```java
85
-
int x =5*3+1;
86
-
System.out.println(x); // Prints 16
87
-
88
-
int y =6/2* (1+2); // Do it pemdas?
89
-
System.out.println(x); // It does, prints 9
90
-
91
-
int sum = x + y;
92
-
System.out.println(sum); // Prints 25
93
-
94
-
String greeting ="Welcome to programming preseason";
95
-
System.out.println(greeting);
73
+
/* The following line will do the following:
74
+
1. Add 6 and 4 to create 10
75
+
2. Multiply that by 9 to create 90
76
+
3. Subtract 9 to create 81
77
+
4. Display 81 to the screen
78
+
*/
79
+
System.out.println(9* (6+4) -9);
96
80
```
97
-
In the next lesson, you're going to learn about what the `int` and `String` are.
81
+
It's `/*` to start a comment and `*/` to end the multi-line comment. Anything in-between will be a comment.
98
82
99
83
[^1]: (1) Code snippits - Something that always bothered me about learning to program was that they never put where the code actually went. I can see why in hindsight, but I figured I would tell you where code snippits go. In the first code snippit, it's the entire file, you can copy and paste that into a main.java and run it. But for the rest you can't just do that. The 2nd snippit of code is of a function definition, when you see code with definitions like that, you can assume it came from inside of a class. When you don't see any function definitions, it most likely came from inside of a function. In our case, besides the 1st and 2nd snippits, the rest is from within the main function.
Variables are arguably the most important thing in programming any language. Variables consist of 3 main components:
10
+
11
+
1. The type
12
+
This is what the variable stores, some examples are:
13
+
* `String` - Stores text, such as "Hello". String values are surrounded by double quotes.
14
+
* `int` - stores integers (whole numbers), without decimals, such as 123 or -123.
15
+
* `double` - stores floating point numbers, with decimals, like 19.99 or -3.14159265 (for people with experience, we will not be using `float` in our code).
16
+
* `char` - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
17
+
* `boolean` - stores values with two states: `true` or `false`
18
+
19
+
2. The name
20
+
This is what the value is stored to, you use this name to reference its value
21
+
3. The value
22
+
This is what the variable stores, it will be of the same type of the variable
23
+
24
+
They get arranged like so:
25
+
```java
26
+
type name = value;
27
+
```
28
+
29
+
It's important to note that the value is optional and the following is also a valid format:
30
+
```java
31
+
type name;
32
+
```
33
+
If you use this, I would expect that you give it a value soon afterwards.
34
+
35
+
36
+
## Some Examples
37
+
If I wanted to make a variable called `message` with a type of `String` and a value of `Hello World` I could do the following.
38
+
```java
39
+
String message ="Hello World";
40
+
System.out.println(message);
41
+
```
42
+
The code above will print the value of the variable `message` which in this case is `"Hello World"`.
43
+
44
+
```java
45
+
double pi =3.14159265;
46
+
System.out.println(pi); // Prints 3.14159265
47
+
```
48
+
49
+
```java
50
+
int number =67;
51
+
System.out.println(number); // Prints 67
52
+
```
53
+
54
+
### Assigning Later
55
+
The following is a example of how you can give a variable a value after declaring it.
56
+
```java
57
+
int number;
58
+
number =45;
59
+
System.out.println(number); // Prints 45
60
+
```
61
+
Notice how after declaring a variable, we don't need to put the type next to it when assigning it a value.
62
+
### Assigning Generally
63
+
We often find it useful to update a variable so here's a example, it's the exact same assigning syntax.
0 commit comments