Learn Programming in Java


This is the second part of a self-paced short course designed to get eager, self-motivated people started in the skill of programming. The first part discusses program structure in a language-neutral way, using a subset of English. Nobody learns programming in one or two weeks or even a whole semester, it takes time and practice -- "motivation and miles" -- but we can give you a roadmap. Once you more or less understand the basic structure of things, you can look at existing programs and figure out how they work -- and by extension and a lot of copying, you can write your own code to do similar things.

Computer programming -- at least what they call "programming" (a lot of computers are programmed in other ways, but nobody wants to admit that's what is happening) -- is mostly text-based, so I had a lot of text to explain that, but nobody wanted to read it. I still rather like what I said, and you can read it here (it won't cost you much, but it does help you know if this is what you want to do for the rest of your life).
 

Design in English

The hardest part of computer programming is understanding what you want the program to do. If you cannot explain to somebody (to yourself first, then to other people) in the language you think in -- English or Spanish or whatever language you grew up with -- if you can't describe in a language you understand what the computer should do, how will you ever describe the same thing in a foreign language like Java or C? Write it down in English. This is why we started in English. Every program in this course of study begins in English. Even the esoteric ways to solve programming problems like Binary Search (which we used and will use again in the Guessing Game) always start out in English. If you think in another language, re-think it in that language. Understanding is the key. The value you bring to the table -- what you will get paid for, if you get paid at all -- is the ability to understand how something works, and then to say the same thing (to translate it) in a precise programming language. Nobody does it any other way and succeeds, not even "deep" neural nets (sometimes erroneously called "AI").

The other thing you should have learned is what I call "The Six Things," the six basic ideas that make up every program in every computer made. You used them to write programs for the Kitchen computer, and you can translate a program in any programming language (including our limited "English") into any other programming language, simply by substituting that language's equivalent for each of the Six Things. We will start out today by showing how each of those Six are spelled in Java, and then you can translate into Java some programs you already wrote in English, and they will run the same.
 

Six Ideas in Java

In the following descriptions, "statement" is a single command, either a subroutine call or variable assignment, or else a complete iteration or conditional. In the code examples it is written in italics "statement" to show that any statement can be there. Italics also are used as place-holders for arbitrary expressions or names supplied by programmer. Reserved words (not allowed as variable or subroutine names) are shown in bold. Other actual code is shown in typewriter font. Colors are used to show relationships

Comments are not part of the program that the computer runs, they are only there to help people (you and your colleagues) read and understand the code. A comment either starts with two slant characters "//" and ends at the end of that line, or else (called "block comment" because it can extend over several lines) starts with "/*" and ends with "*/". I often use comments here to explain the example code.

You will see examples of these Java statement types as we go along, and they will be explained again when they are first needed in the code you write, so don't worry if the explanation here is obscure:
 

Sequence in Java is spelled with a semicolon ";" which terminates each subroutine call or variable assignment or declaration (statement), or else with a pair of braces "{...}" that combines into one statement a whole sequence of statements.
{;;; {}} // four null statements (they do nothing) combined into one statement


Iteration in Java comes in several forms, but only two are really useful:

while (expression)statement // the expression should evaluate to true or false
The expression is tested before the loop statement executes each time through, and any time it is false the loop terminates (and proceeds with the next statement after the loop) without doing statement at all

The statement is whatever you want repeated. Most programmers always wrap braces {...} around the statement so they don't need to think about adding them if they later decide to add another statement inside the loop.

for (name=expression;expression;statement)statement
name is a variable, declared either here or previously

The first expression is an initial value given to that variable

The second expression is a boolean value, usually testing name against some end value, and works like the expression in while

The first statement is typically an increment or decrement on the variable, which executes after the end of the second statement

The second statement is whatever you want repeated.

The for-loop parentheses and the semicolons are required, but the expressions and statements between them are optional (I do not recommend leaving them out, it makes your program hard to read). The assignment, if omitted, could be replaced by some other initialization in a prior line. The second expression, if omitted, is assumed to be true (it would be a never-ending infinite loop unless you have an explicit break inside)
For example, this is exactly equivalent to the corresponding while-loop above:
for (;expression;)statement
Two other commands inside the statement give more precise control of the loop operation:
break; // immediately exits the loop

continue; /* immediately skips to the loop's next iteration, including any incrementation statement and the test expression*/


Conditionals come in two flavors in Java, but we will mostly only use the familiar if..else:

if (expression)statementelsestatement
The expression is a boolean value, which if it evaluated true, the first statement is executed, otherwise the second statement is executed
The else-part (with its stmt) is optional. Most programmers always wrap braces {...} around the statements, so they don't need to think about adding them if they decide to add another statement under the control of the condition.


Variables must always be declared to be a particular type in Java before they are used, and may optionally be given an initial value in the declaration. Multiple variables may be declared of the same type in one statement (ending in a semicolon). The type must be a previously defined class name (see "Object-Oriented Programming" when we get there, and "Objects and Classes" in the "Need to Know" page because we mostly won't get there), or else a defined scalar type like boolean, char, int, float or double. Here is an example of two variable declarations, the second being the system-defined String type:

int abc, xyz = 0, aHugeVeryBigName = xyz+8;
String aWord = "Mary";
Variables can be given values when they are declared, and/or elsewhere in the program like this:
abc = aHugeVeryBigName-xyz+33;
You can give the same value to multiple variables in a single command, but I don't encourage it (look it up online if you want to do that).

The familiar arithmetic operators (+ - * /) work on the number types in Java, along with various bit operators you mostly don't need in the programs we are doing here, and you can group operations using parentheses, where the subexpressions inside parentheses are evaluated before being combined with any operations outside. Everything else is done by subroutine ("function" or "method") calls.

Input/Output in Java is always by predefined subroutine calls.

Subroutines in Java are called "methods" for no particular reason, and can be declared only inside class definitions. They have an optional return type, a name, and a parameter list of zero or more typed parameters that look more or less like variable declarations separated by commas rather than semicolons and enclosed in a pair of parentheses. The body of the method is enclosed in a pair of required braces. If the subroutine ("function") returns a value, it must end in a return statement giving the value to be returned, which must be the declared type, and ending in a semicolon; otherwise the declared "type" is void. Any subroutine or function may return early with a return statement giving the value, or if void, the return statement alone, in either case always ending in a semicolon.

Here is an example of a trivial method (function) returning the sum of its two parameters:

int sum(int a, int b) {return a+b;}
Classes in Java are a way to group related data and methods into a separate compilation, where the class name becomes a new data type with its own copies of the data declared in the class. Most of the programs you will write in this curriculum are not object-oriented, so we will introduce those distinctives when we get there, (see "Object-Oriented Programming" when we get there, and "Objects and Classes" in the "Need to Know" page because we mostly won't get there).


OK, let's get started. Turn the page.

Next: Programming Environment

<<Previous | ToC | Next >>

2021 August 28