Programming 101

A couple of weeks ago, I began a course in Introductory Programming at the IT University. I don’t know a lot about programming, and I certainly don’t have a lot a training or experience programming, so I’m starting at the very bottom and working my way up.

The course focuses on the programming language Java as the central element of our learning. My friend (and local computer guru) Stefan says that Java is a pretty decent first language – as his teachers at the Computer Science department warned that learning a too easy or soft-around-the-edges language (whatever that might mean) first will give you bad habits that will make even worse when you try to learn something more rigid.

Apparently, Java is good mix of easy and rigid – so we’ll see about that.

Since I’m interested in examining how people learn and use programming languages and how this affects their relationship with the computer, I thought it would be relevant to write about my own experiences learning Java. I expect that this will sort of a weekly feature, describing what I’ve learned about programming in past week, what difficulties have arisen, what I have produced, and about the general atmosphere and tone on the course. I won’t go into all the details, but even so I still expect this to be pretty lengthy.

Since I’ve already managed to get behind schedule on this weekly feature, they’ll be a lot stuff about programming here this week…

————————————————

Programming 101

Apparently for the first time, the course is being taught in English. Though both lecturers are Danish, a fair few of the students at the ITU are various sorts of foreigners – quite possibly attracted to the class because of the relatively few English-language courses at the ITU.

88 people signed up for the course (I didn’t sign up, because the ol’ institute wouldn’t accept it as relevant for anthropology – I suppose it still is a rather radical thought) and maybe 10 or so of these are female. The 88 is motley bunch of full-time students and open university students with proper jobs and lives outside the university (and who can thus afford to pay the 6.250 Danish kroner the ITU charges for the course).

The ITU course system allows for people to switch courses within the first two weeks of the semester, so this number is likely to change. On the “Prerequisites” slide of the lecture, the lecturer put “Don’t Despair” underneath “User level computer skills”, “e-mail, browsers” and “some word-processing”, and generally this course has a reputation for being difficult.

The lecturer slyly answered this concern by saying: “It’s definitely doable.” Just before he launched into a lengthy discussion involving lots of acronyms, descriptions and metaphors under the heading, “So what is a computer anyway?”

“It’s not a hard disk – so don’t call it that. It’s layers upon layers of technology. From the hardware layer (the keyboard, monitor, memory, CPU, etc.) to the operating system layer (i.e. Windows or Linux) to the actual application layer (Word, Notepad, etc.).”

“The central element of the computer is the CPU which is the clever guy that can compare and manipulate numbers extremely quickly. The CPU only understands numbers which is also called machine language. Apart from that it is extremely stupid but because it is so fast, you get the impression of intelligence when it seems to be able to do two things at the same time.”

“It has no intelligence. It can’t improvise. It can’t figure out things for you. Don’t expect too much of computers – you will have to do most of the thinking.”

“A program is collection of commands to the computer. Internal commands are basically just numbers. In the ASCII standard way of translating letters into numbers, capital A is represented by the number 65.”

The lecturer is using so many new terms and acronyms that I expect somebody with just basic level of computing knowledge would already have trouble digesting it all. Though he is keen to point out that you can ask questions at any time. Nobody raises their hand.

He continues to talk about “thin and thick clients”, different kinds of servers, LAN and WAN networks, backbones, hubs, switches and wireless networks. He points out how the Internet is just interconnected networks sharing nothing more than cables and common protocols.

From there he touches upon the FTP, telnet, World Wide Web, hypertext, HTML, browsers and how HTML and program code is just text and that it can’t do anything on its own. That the text needs to be interpreted by a program in order to be used by the computer.

Then he introduces to kinds of files – text files and binary files. Text files are “just text” and binary files are “just numbers”. We will just be working with the text files, he assures us.

Both kinds of files have extensions that helps the operating system to recognize and use the files in a certain way. At this point he stops awkwardly and says that sometimes people don’t think about the extensions, because the operating system is so good at associating the files with certain programs.

At this point he is ready to introduce programming languages: “A programming language is a set of rules on how to issue or write commands to the computer. It is based on syntax and semantics like other languages. But it tends to be a lot stricter:”

This is correct:

label.setForeground(color.blue);

This is completely wrong:

label.setforeground(color.blue);

(as Java differentiates between upper and lower case letters)

He mentions a bunch of programming languages such as C, C++, Haskell, Prolog, Fortran and LISP, and various schools, designs, algorithms, and different ways of doing things. Some are “functional” and “procedural”, whilst others – such as Java – are “object-oriented”.

He then declares that that really isn’t all that relevant at this point. It isn’t entirely obvious as to why he bothers to mention all of this at this point.

But back to the central stuff:

“But how do we make the transition from an arbitrary high level language to machine instructions – the only thing a computer can actually execute?”

You use a compiler. A compiler is a program that translates the programming language into binary machine language that the computer can understand. The compiling process fails if the syntax of statements written in the programming language is wrong, or if the references to other files (for instance libraries of code) aren’t right. The compiler is very picky about these things, and generates a fair few error messages if your syntax isn’t up to scratch.

Java isn’t like most other programming languages, and doesn’t just use a compiler. It uses both a compiler and something called the Java Virtual Machine:

First you have your text-file containing the program you’ve written in Java, and that you want to run on the computer. In order for the computer to recognize it as a java program, you need to name it with the extension “.java” – in this case, the file is called “MyClass.java”.

You then use a compiler, for instance the one called “javac” to compile the program into bytecode. bytecode is not exactly “just numbers”, but a lot closer to pure machine language than java itself.

This bytecode can then be run by a Java Virtual Machine, which is a sort of interpreter program that can translate the bytecode directly into the binary language that the local computer can understand. The reason why Java contains this extra step is that it allows Java to run on many different systems – actually as many as there are different versions of the Java Virtual Machine.

It has to be said that most computers use different chips and architectures which apparently is the concrete, physical engineering layout of the interior of the computer. Depending on the computer and on how the individual operating system utilises the available resources, it can be very difficult to make a program run on than one kind of platform.

Java is so lauded because it with its Virtual Machine is “platform independent” and therefore code written in java and compiled to bytecode (which has the extension “.class”) works on most kinds of computers – and even mobile phones and other electronical equipment.

Even though most programming languages have very tight demands on syntax, they’re much more lax when it comes to formatting. Our lecturer gives us several examples of how different the same kind of code can look, and still execute exactly the same:

This all depends on the use of Whitespaces: spaces, tabs, new lines. There are various conventions, “cultural variations” and norms around how to format your code. It is all optional, but it is good practice and it makes it easy to read – which is vital when comes to having other people understanding and commenting on your code.

“As for comments: It is central to use them on a higher level of abstraction: Comment the code so as to express motivations or the ideas behind a certain chunk of code.”

“A good comparison is people who write assembler code (very low-level computer code, much like java’s bytecode) use to write their comments in higher-level computer code such as Java. In this way, it is easy to understand what function they’re trying to achieve through the assembler code.”

Because Java is platform independent, it is also the programming language of the Internet: That means that they’re three kinds of Java programs:
– ordinary programs that react to keyboard input
– Graphical User Interface (GUI) programs that react mainly to mouse input and responds through dialogue boxes – and are dependent on a graphical Operating System.
– Applets – small programs that can run directly in a web browser such as Firefox or Internet Explorer.

He ends the lecture by showing us how differently the code behind just a simple program that writes one line of text looks in for the three different kinds of Java programs:

Damn, this turned out to be awfully long. And so far, I’ve been averaging 6 pages of notes per lecture. There is just so much that can be taken for granted, and if you examine it with just a tad more inquisitiveness (is that even a word?) you find lots and lots and lots.

Programming languages offer huge amounts of conventions, embedded ideas and cleverness to consider. And on top of that comes writing my own programs.

We weren’t really expected to be able to do a lot of programming at this point, as this is the introductory lecture. So there was no home assignment.