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: consolelog.adoc
+93-28Lines changed: 93 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,29 +4,39 @@ Perhaps the simplest Java program is:
4
4
5
5
[source]
6
6
----
7
-
console.log("Hello, World!");
7
+
public class Main {
8
+
public static void main(String[] args) {
9
+
System.out.println("Hello, World!");
10
+
}
11
+
}
8
12
----
9
13
10
-
This program just prints "Hello, World!".footnote:[And while you might _not yet_ understand this _technical description_, it is a program of one _line_ of code, which says "call the 'log' method on the 'console' object, using the string "Hello, World!" as the argument to be logged."]
14
+
This program just prints "Hello, World!".
11
15
12
-
Logging, in this case, means _outputting_ or _printing_ the "Hello, World!" somewhere. That somewhere is the _console_, a place Java uses to communicate with a user (in this case, us, the programmers.)
16
+
_System.out.println_, in this case, means _outputting_ or _printing_ the "Hello, World!" somewhere. That somewhere is probably the terminal where the program was run.
13
17
14
-
(And if you haven't done it yet, go to https://code.zipcode.rocks and make a browser bookmark for yourself. Once that's done, you can use that REPL footnote:[a REPL is short for "read-evaluate-print loop", a special kind of computer program that lets you run code of a given language.] as a place where you can type in the code from this book and run it to see what each code snippet does.)
15
18
16
-
We will use `console.log` to do a lot in the coming pages. It's a simple statement, one that in other languages might be `print` or `write` or `puts` (and why we all cannot just agree on one way to do this, well, beats me. Java's is `console.log`)
19
+
We will use `System.out.println` to do a lot in the coming pages. It's a simple statement, one that in other languages might be `print` or `write` or `puts` (and why we all cannot just agree on one way to do this, well, beats me. Java's is `System.out.println`)
If you typed into your REPL and clicked the "Run" button, you should have seen this as your output:
33
+
Now, how do we `run` this program? Use a REPLI.it!!
27
34
28
35
****
29
-
Car's Speed: 55
36
+
> sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
37
+
> java -classpath .:target/dependency/* Main
38
+
Car's Speed: 55.0
39
+
>
30
40
****
31
41
32
42
Cool, huh? The ability to communicate with you is one of Java's most fundamental capabilities. And you've run two Java programs. Congratulations, you're a coder. (Well, at least for today you are.)
@@ -41,18 +51,21 @@ Java will ignore anything on a line after two forward slashes. "//"
41
51
[source]
42
52
----
43
53
// this is a comment. it might describe something in the code.
44
-
console.log('Hello');
54
+
int x = 53 - 11;
45
55
46
-
console.log('World'); // this is also a comment.
56
+
int y = x * 4; // this is also a comment.
47
57
----
48
58
49
59
Often, you'll see something like this in this book.
50
60
51
61
[source]
52
62
----
53
-
let flourAmount = 3.5;
54
-
55
-
console.log(flourAmount); // -> 3.5
63
+
public class Main {
64
+
public static void main(String[] args) {
65
+
double flourAmount = 3.5;
66
+
System.out.println(flourAmount); // -> 3.5
67
+
}
68
+
}
56
69
----
57
70
58
71
That comment at the end of the console.log line is showing what you can expect to see in the output. Here it would be "3.5" printed by itself. Try it in your bookmarked Repl.
@@ -61,22 +74,45 @@ We can also add useful stuff to the .log call.
61
74
62
75
[source]
63
76
----
64
-
let flourAmount = 3.5;
65
-
66
-
console.log("We need", flourAmount, "cups of flour."); // -> We need 3.5 cups of flour.
77
+
public class Main {
78
+
public static void main(String[] args) {
79
+
double flourAmount = 3.5;
80
+
System.out.println("We need " + flourAmount + " cups of flour."); // -> 3.5
81
+
}
82
+
}
67
83
----
68
84
69
85
See how Java types it all out as a useful phrase? That proves to be very handy in a million-plus (or more) cases.
70
86
71
87
Comments can be used to explain tricky bits of code, or describe what you should see in output. Comments are your friend.
72
88
89
+
== BUT WAIT!!!
90
+
91
+
What does `int` and `double` signify? Well, see how they proceed another word?
92
+
93
+
[source]
94
+
----
95
+
public class Main {
96
+
public static void main(String[] args) {
97
+
double flourAmount = 3.5;
98
+
// ^^^^^^ ^^^^^^^^^^^
99
+
System.out.println("We need " + flourAmount + " cups of flour."); // -> 3.5
100
+
}
101
+
}
102
+
----
103
+
104
+
It is our way of telling Java what `flourAmount` is. We want `flourAmount` to be place holder for the number `3.5`, because makes
105
+
the code easier to read and understand. See much more on this, up ahead, in `variables`.
106
+
107
+
Oh, and `int`?? Well, it means a number, but just an integer like 0, 1, 2, 42, 99, etc. A `double` can have decimal point and some number after, so I can write `3.5` or `42.0`.
108
+
73
109
== Statements and Expressions
74
110
75
111
In Java, there are parts of a program and different parts have different names. Two of the most basic (and fundamental) are *statements* and *expressions*.
76
112
77
113
=== Expressions
78
114
79
-
An *expression* is something that needs to be computed to find out the answer. Here are a few simple ones.
115
+
An *expression* is something that needs to be _computed_ to find out the answer. Here are a few simple ones.
80
116
81
117
[source]
82
118
----
@@ -87,26 +123,34 @@ speed > 55.0
87
123
regularPrice * (1.0 - salePercentOff)
88
124
----
89
125
90
-
Each of these lines is something we'd like Java to *compute* for us. That computation is often referred to as "evaluation" or "evaluate the expression" to get to the answer. There are two kinds of expressions in Java, _arithmetic expressions_ and _boolean expressions_.
126
+
Each of these lines is something we'd like Java to *compute* for us. That computation is often referred to as "evaluation" or "evaluate the expression" to get to the answer.
127
+
There are two kinds of expressions in Java, _arithmetic expressions_ and _boolean expressions_.
91
128
92
-
*Arithmetic expressions* are, as their name implies, something that require arithmetic to get the answer. An expression like "5 + 8 - 3" gets _evaluated_ to 10.
129
+
*Arithmetic expressions* are, as their name implies, something that require arithmetic to get the answer. An expression like "5 + 8 - 3" gets _evaluated_ to 10.
130
+
So an arithmetic expression will end up being a _number_.
93
131
94
-
*Boolean expressions* result in either a True or a False value. Example: "maxSpeed > 500.0" - this is either true or false depending on the value of maxSpeed.
132
+
*Boolean expressions* result in either a True or a False value.
133
+
Example: "maxSpeed > 500.0" - this is either true or false depending on the value of maxSpeed.
134
+
Or "homeTeamScore > visitorTeamScore" which will be true is and only if the Home team's score is greater than
135
+
the Visitor team's score. Both _scores_ are numbers, the result of the **greater than** makes the result of the
136
+
epxression a _boolean expression_ (either **true** or **false**).
95
137
96
138
=== Statements
97
139
98
140
A *statement* is just a line of Java. It ends with a ';' (semi-colon).
99
141
100
142
[source]
101
143
----
102
-
// at the Grocery
103
-
104
-
salesTaxRate = 0.06;
144
+
public class Main {
145
+
public static void main(String[] args) {
146
+
// at the Grocery
105
147
106
-
totalGroceries = 38.99;
107
-
salesTax = totalGroceries * salesTaxRate;
108
-
109
-
chargeToCard = totalGroceries + salesTax;
148
+
double salesTaxRate = 0.06;
149
+
double totalGroceries = 38.99;
150
+
double salesTax = totalGroceries * salesTaxRate;
151
+
double chargeToCard = totalGroceries + salesTax;
152
+
}
153
+
}
110
154
----
111
155
And this is what a Java program looks like. It's just a list of statements, one after the other, that get computed from the top down.
112
156
@@ -144,3 +188,24 @@ if (magePower > 120.0) {
144
188
145
189
See those curly-braces? They start and stop the _block_, and contain the statements within. You can also see how the code is indented, but the real key are those braces. You'll see lots of blocks when you're looking at Java code.
0 commit comments