forked from ChrisMayfield/ThinkJava2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch02.tex
864 lines (616 loc) · 33.8 KB
/
ch02.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
\chapter{Variables and Operators}
\label{variables}
This chapter describes how to write statements using {\em variables}, which store values like numbers and words, and {\em operators}, which are symbols that perform a computation.
We also explain three kinds of programming errors and offer additional debugging advice.
%Understanding what can go wrong will help you get it right.
%Finally we discuss the rules of code style, which helps make programs easier to read and debug.
To run the examples in this chapter, you will need to create a new Java class with a \java{main} method (see Section~\ref{hello}).
Throughout the book, we often omit class and method definitions to keep the examples concise.
\section{Declaring Variables}
%NOTE: in response to review comments, I am cleaning up the use of
% ``memory''. I would like to avoid talking about hardware, or being
% specific about where values are stored. The addresses that appear
% as object IDs are actually virtual addresses; the values themselves
% might be in RAM or on HDD. But we really don't want to get into
% that. I'd rather offer the abstract model that a variable indicates
% a location that contains a value.
\index{variable}
\index{value}
One of the most powerful features of a programming language is the ability to define and manipulate variables.
A {\bf variable} is a named location in memory that stores a {\bf value}.
Values may be numbers, text, images, sounds, and other types of data.
%They can be printed, and as we'll see later, operated on.
To store a value, you first have to declare a variable:
%Since the values we want to store are text, we declare that the new variable is a string:
\begin{code}
String message;
\end{code}
\index{declaration}
\index{statement!declaration}
\index{type!int}
\index{type!char}
\index{type!String}
This statement is called a {\bf declaration}, because it declares that the variable \java{message} has the type \java{String}.
Each variable has a {\bf type} that determines what kind of values it can store.
For example, the \java{int} type can store integers like \java{1} and \java{-5}, and the \java{char} type can store characters like \java{'A'} and \java{'z'}.
Some types begin with a capital letter and some with lowercase.
You will learn the significance of this distinction later, but for now you should take care to get it right.
There is no such type as \java{Int} or \java{string}.
%, and the compiler will complain if you make one up.
To declare an integer variable named \java{x}, you simply type this:
\begin{code}
int x;
\end{code}
Note that \java{x} is an arbitrary name for the variable.
In general, you should use names that indicate what the variables mean:
%For example, if you saw these declarations, you could probably guess what values would be stored:
\begin{code}
String firstName;
String lastName;
int hour, minute;
\end{code}
This example declares two variables with type \java{String} and two with type \java{int}.
The last line shows how to declare multiple variables with the same type: \java{hour} and \java{minute} are both integers.
Note that each declaration statement ends with a semicolon (\java{;}).
\index{case-sensitive}
Variable names usually begin with a lowercase letter, in contrast to class names (like \java{Hello}) that start with a capital letter.
When a variable name contains more than one word (like \java{firstName}), it is conventional to capitalize the first letter of each subsequent word.
Variable names are case-sensitive, so \java{firstName} is not the same as \java{firstname} or \java{FirstName}.
\index{keyword}
You can use any name you want for a variable.
But there are about 50 reserved words, called {\bf keywords}, that you are not allowed to use as variable names.
These words include \java{public}, \java{class}, \java{static}, \java{void}, and \java{int}, which are used by the compiler to analyze the structure of the program.
You can see the full list of keywords (\url{https://thinkjava.org/keywords}), but you don't have to memorize them.
Most programming editors provide ``syntax highlighting'', which makes different parts of the program appear in different colors.
And the compiler will complain even if one does sneak past you and your editor.
%For example, keywords are often blue, strings red, comments green, and other code black.
%If you type a variable name and it turns blue, watch out!
\section{Assigning Variables}
\index{assignment}
\index{= assignment operator}
\index{statement!assignment}
Now that we have declared some variables, we can use them to store values.
We do that with an {\bf assignment} statement:
\begin{code}
message = "Hello!"; // give message the value "Hello!"
hour = 11; // assign the value 11 to hour
minute = 59; // set minute to 59
\end{code}
This example shows three assignments, and the comments illustrate different ways people sometimes talk about assignment statements.
The vocabulary can be confusing here, but the idea is straightforward:
\begin{itemize}
\item When you declare a variable, you create a named storage location.
\item When you make an assignment to a variable, you update its value.
\end{itemize}
As a general rule, a variable has to have the same type as the value you assign to it.
For example, you cannot store a string in \java{minute} or an integer in \java{message}.
We will show some examples that seem to break this rule, but we'll get to that later.
%On the other hand, that rule can be confusing.
%There are many ways that you can convert values from one type to another, and Java sometimes converts things automatically.
%For now you should remember the general rule, and we'll talk about exceptions later.
A common source of confusion is that some strings {\em look} like integers, but they are not.
For example, \java{message} can contain the string \java{"123"}, which is made up of the characters \java{'1'}, \java{'2'}, and \java{'3'}.
But that is not the same thing as the integer \java{123}:
\begin{code}
message = "123"; // legal
message = 123; // not legal
\end{code}
\index{initialize}
Variables must be {\bf initialized} (assigned for the first time) before they can be used.
You can declare a variable and then assign a value later, as in the previous example.
You can also declare and initialize on the same line:
\begin{code}
String message = "Hello!";
int hour = 11;
int minute = 59;
\end{code}
%You can make more than one assignment to the same variable.
%For example:
%
%\begin{code}
%int i = 1;
%i = 2;
%\end{code}
%
%\index{update}
%
%The first line initializes \java{i} to 1.
%The second line changes its value to \java{2}.
%In this example, there is no reason to make two assignments, but in many programs it is useful to reassign, or {\bf update}, variables.
\section{Memory Diagrams}
\label{state}
Because Java uses the \java{=} symbol for assignment, it is tempting to interpret the statement \java{a = b} as a statement of equality.
It is not!
Equality is commutative, and assignment is not.
For example, in mathematics if $a = 7$, then $7 = a$.
In Java \java{a = 7;} is a legal assignment statement, but \java{7 = a;} is not.
The left side of an assignment statement has to be a variable name (storage location).
Also, in mathematics, a statement of equality is true for all time.
If $a = b$ now, $a$ is always equal to $b$.
In Java, an assignment statement can make two variables equal, but they don't have to stay that way:
\begin{code}
int a = 5;
int b = a; // a and b are now equal
a = 3; // a and b are no longer equal
\end{code}
The third line changes the value of \java{a}, but it does not change the value of \java{b}, so they are no longer equal.
\index{state}
Taken together, the variables in a program and their current values make up the program's {\bf state}.
Figure~\ref{fig.state} shows the state of the program after these assignment statements run.
\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/state.pdf}
\caption{Memory diagram of the variables \java{a} and \java{b}.}
\label{fig.state}
\end{center}
\end{figure}
\index{memory diagram}
\index{diagram!memory}
Diagrams like this one that show the state of the program are called {\bf memory diagrams}.
Each variable is represented with a box showing the name of the variable on the outside and its current value inside.
As the program runs, the state of memory changes, so memory diagrams show only a particular point in time.
For example, if we added the line \java{int c = 0;} to the previous example, the memory diagram would look like Figure~\ref{fig.state2}.
\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/state2.pdf}
\caption{Memory diagram of the variables \java{a}, \java{b}, and \java{c}.}
\label{fig.state2}
\end{center}
\end{figure}
\section{Printing Variables}
\label{sec:printvar}
You can display the current value of a variable by using \java{print} or \java{println}.
The following statements declare a variable named \java{firstLine}, assign it the value \java{"Hello, again!"}, and display that value:
\begin{code}
String firstLine = "Hello, again!";
System.out.println(firstLine);
\end{code}
%Assuming this code fragment is inside a method that is inside a class, the output is:
%
%\begin{stdout}
%Hello, again!
%\end{stdout}
When we talk about displaying a variable, we generally mean the {\em value} of the variable.
To display the {\em name} of a variable, you have to put it in quotes:
%For example: \java{System.out.println("firstLine");}
\begin{code}
System.out.print("The value of firstLine is ");
System.out.println(firstLine);
\end{code}
For this example, the output is as follows:
\begin{stdout}
The value of firstLine is Hello, again!
\end{stdout}
Conveniently, the code for displaying a variable is the same regardless of its type.
For example:
\begin{code}
int hour = 11;
int minute = 59;
System.out.print("The current time is ");
System.out.print(hour);
System.out.print(":");
System.out.print(minute);
System.out.println(".");
\end{code}
The output of this program is shown here:
\begin{stdout}
The current time is 11:59.
\end{stdout}
To output multiple values on the same line, it's common to use several \java{print} statements followed by \java{println} at the end.
But don't forget the \java{println}!
On many computers, the output from \java{print} is stored without being displayed until \java{println} is run; then the entire line is displayed at once.
If you omit the \java{println}, the program might display the stored output at unexpected times or even terminate without displaying anything.
\section{Arithmetic Operators}
%Recall that Java programs are organized into {\em classes}, each of which has one or more {\em methods}, each of which has one or more {\em statements}.
%Most statements consist of one or more {\bf expressions}.
\index{operator}
\index{addition!integer}
{\bf Operators} are symbols that represent simple computations.
%Most operators in Java do what you expect them to do, since they are common mathematical symbols.
For example, the addition operator is \java{+}, subtraction is \java{-}, multiplication is \java{*}, and division is \java{/}.
%Variables are replaced with their values before the computation is performed.
The following program converts a time of day to minutes:
\begin{code}
int hour = 11;
int minute = 59;
System.out.print("Number of minutes since midnight: ");
System.out.println(hour * 60 + minute);
\end{code}
The output is as follows:
\begin{stdout}
Number of minutes since midnight: 719
\end{stdout}
\index{expression}
\index{operand}
In this program, \java{hour * 60 + minute} is an {\bf expression}, which represents a single value to be computed (\java{719}).
When the program runs, each variable is replaced by its current value, and then the operators are applied.
The values that operators work with are called {\bf operands}.
Expressions are generally a combination of numbers, variables, and operators.
When compiled and executed, they become a single value.
For example, the expression \java{1 + 1} has the value \java{2}.
In the expression \java{hour - 1}, Java replaces the variable with its value, yielding \java{11 - 1}, which has the value \java{10}.
In the expression \java{hour * 60 + minute}, both variables get replaced, yielding \java{11 * 60 + 59}.
The multiplication happens first, yielding \java{660 + 59}.
Then the addition yields \java{719}.
Addition, subtraction, and multiplication all do what you expect, but you might be surprised by division.
For example, the following fragment tries to compute the fraction of an hour that has elapsed:%, but it has a logic error:
\begin{code}
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60);
\end{code}
The output is as follows:
\begin{stdout}
Fraction of the hour that has passed: 0
\end{stdout}
\index{division!integer}
\index{integer division}
This result often confuses people.
The value of \java{minute} is \java{59}, and 59 divided by 60 should be 0.98333, not 0.
The problem is that Java performs {\em integer division} when the operands are integers.
By design, integer division always rounds toward zero, even in cases like this one where the next integer is close.
As an alternative, we can calculate a percentage rather than a fraction:
\begin{code}
System.out.print("Percent of the hour that has passed: ");
System.out.println(minute * 100 / 60);
\end{code}
The new output is as follows:
\begin{stdout}
Percent of the hour that has passed: 98
\end{stdout}
Again the result is rounded down, but at least now it's approximately correct.
%To get a more precise answer, we can use a different type of variable that can store fractional values.
\section{Floating-Point Numbers}
\index{floating-point}
\index{double}
\index{type!double}
A more general solution is to use {\bf floating-point} numbers, which represent values with decimal places.
%As the name implies, the decimal point floats around (i.e., you can have as many decimal places as you want).
In Java, the default floating-point type is called \java{double}, which is short for ``double-precision''.
You can create \java{double} variables and assign values to them the same way we did for the other types:
\begin{code}
double pi;
pi = 3.14159;
\end{code}
\index{division!floating-point}
Java performs {\em floating-point division} when one or more operands are \java{double} values.
So we can solve the problem from the previous section:
\begin{code}
double minute = 59.0;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60.0);
\end{code}
The output is shown here:
\begin{stdout}
Fraction of the hour that has passed: 0.9833333333333333
\end{stdout}
Although floating-point numbers are useful, they can be a source of confusion.
For example, Java distinguishes the integer value \java{1} from the floating-point value \java{1.0}, even though they seem to be the same number.
They belong to different data types, and strictly speaking, you are not allowed to make assignments between types.
The following is illegal because the variable on the left is an \java{int} and the value on the right is a \java{double}:
\begin{code}
int x = 1.1; // compiler error
\end{code}
\index{automatic conversion}
It is easy to forget this rule, because in many cases Java {\em automatically} converts from one type to another:
\begin{code}
double y = 1; // legal, but bad style
\end{code}
The preceding example should be illegal, but Java allows it by converting the \java{int} value \java{1} to the \java{double} value \java{1.0} automatically.
This leniency is convenient, but it often causes problems for beginners.
For example:
\begin{code}
double y = 1 / 3; // common mistake
\end{code}
\index{division!integer}
\index{integer division}
You might expect the variable \java{y} to get the value \java{0.333333}, which is a legal floating-point value.
But instead it gets the value \java{0.0}.
The expression on the right divides two integers, so Java does integer division, which yields the \java{int} value \java{0}.
Converted to \java{double}, the value assigned to \java{y} is \java{0.0}.
One way to solve this problem (once you figure out the bug) is to make the right-hand side a floating-point expression.
The following sets \java{y} to \java{0.333333}, as expected:
\begin{code}
double y = 1.0 / 3.0; // correct
\end{code}
As a matter of style, you should always assign floating-point values to floating-point variables.
The compiler won't make you do it, but you never know when a simple mistake will come back and haunt you.
\section{Rounding Errors}
\label{rounderr}
%The operations we have seen so far---addition, subtraction, multiplication, and division---also work on floating-point values, although you might be interested to know that the underlying mechanism is completely different.
%In fact, most processors have special circuitry just for performing floating-point operations.
Most floating-point numbers are only {\em approximately} correct.
Some numbers, like reasonably sized integers, can be represented exactly.
But repeating fractions, like $1/3$, and irrational numbers, like $\pi$, cannot.
To represent these numbers, computers have to round off to the nearest floating-point number.
%Notwithstanding, there is a fundamental flaw with floating-point arithmetic.
%In mathematics, there is an infinite number of real numbers.
%But computer processors are finite; they cannot represent {\em every} possible floating-point number.
%Even with double-precision, you will frequently run into problems.
\index{rounding error}
\index{error!rounding}
The difference between the number we want and the floating-point number we get is called {\bf rounding error}.
For example, the following two statements should be equivalent:
\begin{code}
System.out.println(0.1 * 10);
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
\end{code}
But on many machines, the output is as follows:
\begin{stdout}
1.0
0.9999999999999999
\end{stdout}
The problem is that \java{0.1} is a repeating fraction when converted into binary.
So its floating-point representation stored in memory is only approximate.
When we add up the approximations, the rounding errors accumulate.
For many applications (like computer graphics, encryption, statistical analysis, and multimedia rendering), floating-point arithmetic has benefits that outweigh the costs.
But if you need {\em absolute} precision, use integers instead.
For example, consider a bank account with a balance of \$123.45:
\begin{code}
double balance = 123.45; // potential rounding error
\end{code}
In this example, balances will become inaccurate over time as the variable is used in arithmetic operations like deposits and withdrawals.
The result would be angry customers and potential lawsuits.
You can avoid the problem by representing the balance as an integer:
\begin{code}
int balance = 12345; // total number of cents
\end{code}
\index{type!long}
This solution works as long as the number of cents doesn't exceed the largest \java{int}, which is about 2 billion.
%If necessary you can use \java{long} instead, which has a max value of $2^{63}-1$ (about 92 quadrillion dollars).
%Hopefully nobody will ever need that much money!
\section{Operators for Strings}
\index{string!operator}
\index{operator!string}
In general, you cannot perform mathematical operations on strings, even if the strings look like numbers.
The following expressions are illegal:
\begin{code}
"Hello" - 1 "World" / 123 "Hello" * "World"
\end{code}
\index{concatenate}
\index{addition!string}
The \java{+} operator works with strings, but it might not do what you expect.
For strings, the \java{+} operator performs {\bf concatenation}, which means joining end-to-end.
So \java{"Hello, " + "World!"} yields the string \java{"Hello, World!"}.
Likewise if you have a variable called \java{name} that has type \java{String}, the expression \java{"Hello, " + name} appends the value of \java{name} to the hello string, which creates a personalized greeting.
Since addition is defined for both numbers and strings, Java performs automatic conversions you may not expect:
\begin{code}
System.out.println(1 + 2 + "Hello");
// the output is 3Hello
System.out.println("Hello" + 1 + 2);
// the output is Hello12
\end{code}
Java executes these operations from left to right.
In the first line, \java{1 + 2} is \java{3}, and \java{3 + "Hello"} is \java{"3Hello"}.
But in the second line, \java{"Hello" + 1} is \java{"Hello1"}, and \java{"Hello1" + 2} is \java{"Hello12"}.
%\section{Order of Operations}
\index{order of operations}
\index{precedence}
When more than one operator appears in an expression, they are evaluated according to the {\bf order of operations}.
Generally speaking, Java evaluates operators from left to right (as you saw in the previous section).
But for numeric operators, Java follows mathematical conventions:
\begin{itemize}
\item Multiplication and division take ``precedence'' over addition and subtraction, which means they happen first.
So \java{1 + 2 * 3} yields 7, not 9, and \java{2 + 4 / 2} yields 4, not 3.
\item If the operators have the same precedence, they are evaluated from left to right.
So in the expression \java{minute * 100 / 60}, the multiplication happens first; if the value of \java{minute} is 59, we get \java{5900 / 60}, which yields 98.
If these same operations had gone from right to left, the result would have been \java{59 * 1}, which is incorrect.
\index{parentheses}
\index{( ) parentheses}
\item Anytime you want to override the order of operations (or you are not sure what it is) you can use parentheses.
Expressions in parentheses are evaluated first, so \java{(1 + 2) * 3} is 9.
You can also use parentheses to make an expression easier to read, as in \java{(minute * 100) / 60}, even though it doesn't change the result.
\end{itemize}
See the official Java tutorials for a complete table of operator precedence (\url{https://thinkjava.org/operators}).
If the order of operations is not obvious when looking at an expression, you can always add parentheses to make it more clear.
But over time, you should internalize these kinds of details about the Java language.
\section{Compiler Error Messages}
\index{error!message}
Three kinds of errors can occur in a program: compile-time errors, run-time errors, and logic errors.
It is useful to distinguish among them in order to track them down more quickly.
\index{compile-time error}
\index{error!compile-time}
{\bf Compile-time} errors occur when you violate the rules of the Java language.
For example, parentheses and braces have to come in matching pairs.
So \java{(1 + 2)} is legal, but \java{8)} is not.
In the latter case, the program cannot be compiled, and the compiler displays a ``syntax error''.
\index{error!message}
Error messages from the compiler usually indicate where in the program the error occurred.
Sometimes they can tell you exactly what the error is.
As an example, let's get back to the Hello World program from Section~\ref{hello}:
\begin{trinket}[235]{Hello.java}
public class Hello {
public static void main(String[] args) {
// generate some simple output
System.out.println("Hello, World!");
}
}
\end{trinket}
\index{semicolon}
\index{; semicolon}
If you forget the semicolon at the end of the print statement, you might get an error message like this:
\begin{stdout}
File: Hello.java [line: 5]
Error: ';' expected
\end{stdout}
That's pretty good: the location of the error is correct, and the error message tells you what's wrong.
But error messages are not always easy to understand.
Sometimes the compiler reports the place in the program where the error was {\em detected}, not where it actually occurred.
And sometimes the description of the problem is more confusing than helpful.
For example, if you forget the closing brace at the end of \java{main} (line 6), you might get a message like this:
\begin{stdout}
File: Hello.java [line: 7]
Error: reached end of file while parsing
\end{stdout}
\index{parse}
There are two problems here.
First, the error message is written from the compiler's point of view, not yours.
{\bf Parsing} is the process of reading a program before translating; if the compiler gets to the end of the file while still parsing, that means something was omitted.
But the compiler doesn't know what.
It also doesn't know where.
The compiler discovers the error at the end of the program (line 7), but the missing brace should be on the previous line.
Error messages contain useful information, so you should make an effort to read and understand them.
But don't take them too literally.
During the first few weeks of your programming career, you will probably spend a lot of time tracking down syntax and other compile-time errors.
As you gain experience, you will make fewer mistakes and find them more quickly.
\section{Other Types of Errors}
\label{exception}
\index{run-time error}
\index{error!run-time}
\index{exception}
The second type of error is a {\bf run-time error}, so-called because it does not appear until after the program has started running.
In Java, these errors occur while the interpreter is executing byte code and something goes wrong.
These errors are also called ``exceptions'' because they usually indicate that something unexpected has happened.
Run-time errors are rare in the simple programs you will see in the first few chapters, so it might be a while before you encounter one.
When a run-time error occurs, the program ``crashes'' (terminates) and displays an error message that explains what happened and where.
For example, if you accidentally divide by zero, you will get a message like this:
\begin{small}
\begin{stdout}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Hello.main(Hello.java:5)
\end{stdout}
\end{small}
\index{ArithmeticException}
\index{exception!Arithmetic}
Error messages are very useful for debugging.
The first line includes the name of the exception, \java{ArithmeticException}, and a message that indicates more specifically what happened, division by zero.
The next line shows the method where the error occurred; \java{Hello.main} indicates the method \java{main} in the class \java{Hello}.
It also reports the file where the method is defined, {\tt Hello.java}, and the line number where the error occurred, {\tt 5}.
%Sometimes error messages contain additional information that doesn't make sense.
%It can be a challenge to figure out where to find the useful parts without being overwhelmed by extraneous information.
%Keep in mind that the line where the program crashed may not be the line that needs to be corrected.
%\subsection{Logic Errors}
\index{logic error}
\index{error!logic}
The third type of error is a {\bf logic error}.
%{\bf Semantics} pertains to the meaning of a program; that is, what it does when it runs.
If your program has a logic error, it will compile and run without generating error messages, but it will not do the right thing.
Instead, it will do exactly what you told it to do.
For example, here is a version of the Hello World program with a logic error:
\begin{trinket}[235]{Hello.java}
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, ");
System.out.println("World!");
}
}
\end{trinket}
This program compiles and runs just fine, but the output is as follows:
\begin{stdout}
Hello,
World!
\end{stdout}
Assuming that we wanted the output on one line, this is not correct.
The problem is that the first line uses \java{println}, when we probably meant to use \java{print} (see the ``Goodbye, cruel world'' example of Section~\ref{goodbye}).
Identifying logic errors can be hard because you have to work backward, looking at the output of the program, trying to figure out why it is doing the wrong thing, and how to make it do the right thing.
Usually, the compiler and the interpreter can't help you, since they don't know what the right thing is.
\section{Vocabulary}
\begin{description}
\term{variable}
A named storage location for values.
All variables have a type, which is declared when the variable is created.
\term{value}
A number, string, or other data that can be stored in a variable.
Every value belongs to a type (e.g., \java{int} or \java{String}).
\term{type}
Mathematically speaking, a set of values.
The type of a variable determines which values it can have.
\term{declaration}
A statement that creates a new variable and specifies its type.
\term{keyword}
A reserved word used by the compiler to analyze programs.
You cannot use keywords (like \java{public}, \java{class}, and \java{void}) as variable names.
\term{assignment}
A statement that gives a value to a variable.
\term{initialize}
To assign a variable for the first time.
%\term{update}
%An assignment that changes the value of a variable.
\term{state}
The variables in a program and their current values.
\term{memory diagram}
A graphical representation of the state of a program at a point in time.
\term{operator}
A symbol that represents a computation like addition, multiplication, or string concatenation.
\term{operand}
One of the values on which an operator operates.
Most operators in Java require two operands.
\term{expression}
A combination of variables, operators, and values that represents a single value.
Expressions also have types, as determined by their operators and operands.
\term{floating-point}
A data type that represents numbers with an integer part and a fractional part.
In Java, the default floating-point type is \java{double}.
\term{rounding error}
The difference between the number we want to represent and the nearest floating-point number.
\term{concatenate}
To join two values, often strings, end to end.
\term{order of operations}
The rules that determine in what order expressions are evaluated.
Also known as ``operator precedence''.
\term{compile-time error}
An error in the source code that makes it impossible to compile.
Also called a ``syntax error''.
\term{parse}
To analyze the structure of a program; what the compiler does first.
\term{run-time error}
An error in a program that makes it impossible to run to completion.
Also called an ``exception''.
\term{logic error}
An error in a program that makes it do something other than what the programmer intended.
\end{description}
\section{Exercises}
The code for this chapter is in the {\it ch02} directory of {\it ThinkJavaCode2}.
See page~\pageref{code} for instructions on how to download the repository.
Before you start the exercises, we recommend that you compile and run the examples.
If you have not already read Appendix~\ref{interactions}, now might be a good time.
It describes the DrJava Interactions pane, which is a useful way to develop and test short fragments of code without writing a complete class definition.
\begin{exercise} %%V6 Ex2.1
If you are using this book in a class, you might enjoy this exercise.
Find a partner and play {\it Stump the Chump}:
Start with a program that compiles and runs correctly.
One player looks away, while the other player adds an error to the program.
Then the first player tries to find and fix the error.
You get two points if you find the error without compiling the program, one point if you find it using the compiler, and your opponent gets a point if you don't find it.
\end{exercise}
\begin{exercise} %%V6 Ex2.2
\label{ex:date}
The point of this exercise is (1) to use string concatenation to display values with different types (\java{int} and \java{String}), and (2) to practice developing programs gradually by adding a few statements at a time.
\begin{enumerate}
\item Create a new program named {\it Date.java}.
Copy or type in something like the Hello World program and make sure you can compile and run it.
\item Following the example in Section~\ref{sec:printvar}, write a program that creates variables named \java{day}, \java{date}, \java{month}, and \java{year}.
The variable \java{day} will contain the day of the week (like Friday), and \java{date} will contain the day of the month (like the 13th).
%What type is each variable?
Assign values to those variables that represent today's date.
\item Display the value of each variable on a line by itself.
This is an intermediate step that is useful for checking that everything is working so far.
Compile and run your program before moving on.
\item Modify the program so that it displays the date in standard American format; for example: {\tt Thursday, July 18, 2019}.
\item Modify the program so it also displays the date in European format.
The final output should be as follows:
\begin{stdout}
American format: Thursday, July 18, 2019
European format: Thursday 18 July 2019
\end{stdout}
%{\em Hint:} You should be able to copy, paste, and modify the code from Step 4 when completing Step 5.
\end{enumerate}
\end{exercise}
\begin{exercise} %%V6 Ex2.3
The point of this exercise is to (1) use some of the arithmetic operators, and (2) start thinking about compound entities (like time of day) that are represented with multiple values.
\begin{enumerate}
\item Create a new program called {\it Time.java}.
From now on, we won't remind you to start with a small, working program, but you should.
\item Following the example program in Section~\ref{sec:printvar}, create variables named \java{hour}, \java{minute}, and \java{second}.
Assign values that are roughly the current time.
Use a 24-hour clock so that at 2:00 PM the value of \java{hour} is \java{14}.
\item Make the program calculate and display the number of seconds since midnight.
\item Calculate and display the number of seconds remaining in the day.
\item Calculate and display the percentage of the day that has passed.
You might run into problems when computing percentages with integers, so consider using floating-point.
\item Change the values of \java{hour}, \java{minute}, and \java{second} to reflect the current time.
Then write code to compute the elapsed time since you started working on this exercise.
\end{enumerate}
{\em Hint:} You might want to use additional variables to hold values during the computation.
Variables that are used in a computation but never displayed are sometimes called ``intermediate'' or ``temporary'' variables.
\end{exercise}