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/variables.md
+20-3Lines changed: 20 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Variables are arguably the most important thing in programming any language. Var
12
12
This is what the variable stores, some examples are:
13
13
* `String` - Stores text, such as "Hello". String values are surrounded by double quotes.
14
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).
15
+
* `double` - stores floating point numbers, with decimals, like 19.99 or -3.14159265. Double values end with `d`. (for people with experience, we will not be using `float` in our code).
16
16
* `char` - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
17
17
* `boolean` - stores values with two states: `true` or `false`
18
18
@@ -42,7 +42,7 @@ System.out.println(message);
42
42
The code above will print the value of the variable `message` which in this case is `"Hello World"`.
43
43
44
44
```java
45
-
double pi =3.14159265;
45
+
double pi =3.14159265d;
46
46
System.out.println(pi); // Prints 3.14159265
47
47
```
48
48
@@ -68,4 +68,21 @@ System.out.println(number); // Prints 41
68
68
69
69
number =67; // Changes number to 67
70
70
System.out.println(number); // Prints 67
71
-
```
71
+
```
72
+
### Using Variables
73
+
Variables are useful in that they act as their value, here's some examples of variables being used.
74
+
```java
75
+
double x =0.4d+12.1d; // After the =, we can put any expression that gives us a value, for example, math operations
76
+
77
+
double y =2.4d+0.1d;
78
+
79
+
double result = x + y; // 12.5 + 2.5
80
+
System.out.println(result); // Prints 15
81
+
```
82
+
```java
83
+
double x =1;
84
+
x = x +1;
85
+
86
+
System.out.println(x); // Prints 2
87
+
```
88
+
In our next lesson we'll look at functions and how they are useful.
0 commit comments