Learn Programming in Java

Introductory Remarks

aka "Wall of Text"


<<Previous | ToC | Next >>

When I first thought of this course, this was the first page, but then I thought of an easier way to get started, the game Chomp, which by now you have already worked through. As a result, some of the first few "lessons" here in Java will be easy to understand. I would encourage you to read through it and play with some of the example programs anyway.

Java is similar in appearance to both C/C++ and JavaScript, so that switching to one of those languages, while not trivial, at least you understand the basic principles. Programming is a lot of work, and even more attention to detail, but you can do it if you want to. If you want life to be easy, find a different profession, but there's a lot of satisfaction in telling a computer what to do and seeing it do it.

The purpose of this class (and this website) is to get you started on a hopefully long path of computer programming. You will learn Java here because it resembles C++ and C is the language everybody who is anybody uses to program computers. It is not an easy language to learn, but you wouldn't be here unless you are smart enough to knuckle down and put the effort into becoming a wizard, especially after this course is over and you still have a long way to go. You can do it, if you want to. I can get you started, but you alone must supply the wanna.

It is said that there are two kinds of people in the world, those who divide the world into two kinds of people, and those who do not... I'm more interested today in the difference between those who get their jollies out of altering the positions of the molecules on the face of the earth, and those who get their jollies out of telling people to alter the positions of the molecules on the face of the earth. The former have lots of fun and the latter make lots of money. I -- and I hope most of you -- are in the first category. Programming a computer doesn't move many molecules (just the keys on the keyboard), but we move a lot of electrons and photons in very subtle ways. It's a position of control like control phreaks (the other category) can never know, because the computer absolutely does our every command -- even when we didn't want to tell it to do that. We alone have the final say on what the computer does. You can do that, if you want to, but you gotta have the wanna.

You don't need a PhD in computer science to control the computer like Bill Atkinson or Andy Herzfeld. I know that because I are one.* What you need is a little bit of direction on how things work, and then dive in and figure it out for yourself. The six ideas we presented in "Breakfast" and then used in the game "Chomp" that you played and then programmed at the beginning is a large part of getting the feel for programming. I'm going to give you some more of that direction, now in a Java context, but you will not be an expert at the end of this week, it will take months and a voracious appetite for everything computational -- and a lot of help from Google. There are thousands of us out there, and we help each other, just like I'm doing for you this week. If you run into a problem, Google it. Five people on StackOverflow.com had the same problem and posted answers you can understand. Seven more posted obscure answers that leave everybody confused. Ignore them.

Programming is a little like riding a bicycle: it helps to know a few things, but mostly you just get on and do it. You will fall off a lot, but after a while, you can go quite far without falling. Unlike riding a bicycle, nobody gets hurt when your program fails. Unless it's a medical program or self-driving car -- but beginners are not invited to do those kinds of programs ;-)

The best way to learn programming is to watch a master programmer do his thing. Mostly they don't like people looking over their shoulder, so the rest of us must be content to read finished programs and try to understand how they work, and then imitate what they did when we write our own. I did a lot of that, and you will too, but not immediately.

Before you can program a computer, you need to be able to use it, that is, you need to be reasonably skillful at clicking and dragging and double-clicking and drag-selecting and copying and pasting and (of course) typing into fields in windows. You need a recent Windows (Win7 or later) or OSX (again, x.7 or later) computer to do this on. You could do it on Linux or an earlier computer, but it's harder; save that for after you get good at it (if you still want to).

There is one more requirement, not so much something you know how to do, but a willingness (or even better: a desire) to be very precise and accurate. Unlike people, the computer cannot guess what you meant if you said something other. The computer is a marvelous device, it does just exactly what you tell it to do, even if you didn't want to tell it to do that. Even if you are exceedingly meticulous, most of your programming time will be finding and fixing piddly little typing mistakes or things you forgot to put in (or take out: I call them "Copy/Paste errors" ;-) Plan on it, because it happens to the best of us. But the computer doesn't care, so just fix your mistakes and move on. You don't have to show anybody what you did wrong, only how good it looks after you fixed it and it works properly. Unless you need help. All of us get stuck. That's what I'm here in the classroom for, and if you get stuck when you are working on your own, there are on-line forums that answer all kinds of programming questions. We'll look at one of those later.
 

Language

Before you can program anything, you must first understand the language of programming. People speak (and write) to each other in a natural language like English or Spanish, or in an artificial language like Klingon. Yes, Klingon was invented for a TV show, but it can be learned and people can communicate in it. We also use artificial languages to communicate with computers, because they do not understand natural languages the way people do, and mostly not at all. Actually computers understand only their own language of ones and zeroes, so if we want to tell the computer how to do something, whatever we say must be translated from a language you and I understand into the 1s and 0s the computer understands. There are programs that do this, and I am one of the experts in writing that kind of program -- so you don't need to. Instead you will learn what is called a "programming language," a language for writing programs.

The most common programming language used today for writing new programs is called "C" -- which is the grade teachers give to students who did not score well enough to earn an "A" or "B" -- but it's what we have. There are numerous dialects of C, variations that sort of look the same but are as hard to understand as Harlem street talk or Cockney British is for people who only know American TV English. Java is such a dialect. After you learn Java, switching over to C is not much harder than learning a few new words and a slightly different way of saying things. It's much harder to make unintended and undiscovered mistakes in Java, because the computer catches most of them very early in the process, whereas in C the problems can remain hidden for hours and days and years. For the first 35 or so years of my career I refused to write a single line of C -- and then I had to teach it. Now I write everything in a dialect of Java, and then let the computer translate it to C when I need C. Mostly I just bypass C altogether. A few times I bypass using a programming language at all, and just write in machine language (1s and 0s), but that's very hard and not at all worth the trouble unless there are no "high level" programming languages that can say what you want to say, which is almost never. The program that does the translating is called a "compiler" and you will be seeing a lot of that in the course of these lessons.

So what kinds of things can you tell a computer in a programming language? Obviously computers know about numbers, so you can always tell it to do arithmetic. Computers have "memory" so you can tell it to remember a number for a while. The other things computers can do are more complicated, so let me explain them with an analogy.

Let's say you want to send your little sister to the grocery store to buy a jug of milk and a box of corn flakes and some fruit. It's three blocks away, so first you start out by reminding her that when she gets to a street corner, she must be very careful. If there's a traffic light with a pedestrian button, push the button first, then (even if there's no button) wait for the green light. When it turns green -- or if there is no light -- wait at the curb and look both ways. If there is a car coming, wait until it stops or is past, then start over looking both ways. After there are no cars, cross between the white lines. Do this at every street corner. When we tell the computer to do something the same for different circumstances, we call it a "subroutine" or "method." We saw subroutines in Breakfast and in Chomp, and we'll do them again in Java later this week.

Then you tell her that she is to walk one block to the next corner, and do the street corner thing, three times, three blocks. When we tell the computer to do something three times or a thousand times, or as many times as necessary, we call it a "loop" even though -- and we certainly hope most of the time -- that your sister isn't walking around in circles, but making progress toward the store, and then progress back again. We will study loops in a couple days, and we will pay special attention to making sure progress happens.

You want to tell little Mary to buy some fruit, but the store doesn't always have the same kind of fruit for sale. So you tell her that if they have bananas, get bananas; otherwise if they have strawberries, get strawberries. They always have apples, but apples aren't so nice on cereal, so get apples only if they have neither bananas nor strawberries. These are conditional instructions, to be performed (or not) based on circumstances you can anticipate, but do not actually know ahead of time. A lot of what we tell the computer is conditional, so we will learn the Java way of doing that next, after we learn how to tell the computer to do arithmetic and to remember things.

Remember the instructions for crossing the street? You told Mary to first do this, and then do that, and so on. It's a sequence of steps, which the computer does in order. Very fast computers -- including the one in front of you -- often do several things at once, but the designers work very hard to make sure each step gets back in line in the proper sequence, like as if they were done one at a time. You don't even need to know about it (unless you are going to be designing hardware) only that the computer does things in the order you tell it to do them. Somewhere along the line we'll sneak in talking about input and output, things we can tell our own program while it's running in the computer, and what it can tell us or tell other computers, because that's another of the six things every programming language does.

And that's all there is! Everything the computer does is some combination of those six primitive operations, and every programming language has a way to say each of those six things, it's just spelled differently in the different languages. You used all six of those six ideas in Breakfast, and when you programmed the Chomp game, you used the same six ideas. If you grow up to be a computer professional, you will probably learn several languages, but it's easy once you understand it's just different ways to spell these six things. Some languages have whole libraries of things the computer already knows how to do that you can borrow from, while in other languages you have to write everything yourself. Some languages make it easier or harder to make mistakes, but mistakes are possible in all programming languages, and we all make them all the time, and most of the time you will spend trying to figure out what you said wrong, so try to use languages that make that common task easier. Java is one of the better languages for that.

Remember, the computer does just exactly what you tell it to do, even if you didn't want to tell it to do that. You control the computer by controlling yourself. Think everything through carefully. Some people are naturally detail-oriented, but I'm not. I have to work at it. I make lots of mistakes, but that's what the backspace key is for. My father used to say "paper is cheap." The electrons inside the computer are even cheaper. Even the hard drive in your computer is cheaper than paper, for the amount of storage you get.

Next: Programming Environment

<<Previous | ToC | Next >>

Revised: 2020 September 16

* A joke among the engineering students when I was in college, "Four years ago I koont spel enjin-ear, now I are one."