Writing a computer program is somewhat similar to writing a recipe. You need to do things in a certain order in order to ensure success.
In Object Oriented programming such as Java, you focus on writing each chunk of code as a separate object. When you write a chunk of code like this, you shape these objects by assigning them
a) attributes, thus defining their state – which you can use to decide when the object should interact with the other objects, and in what ways it can be affected (in can’t be affected in a way that you haven’t defined).
b) behaviour, thus defining the way in which the object can act. This is called the method of the object. An object can contain many methods, and these are generally used to change the state of one or more objects.
But when you write code, you don’t write objects as such – you write classes – classes are the blueprints, the templates based on which the objects are instantiated. You can think of a class as a little car factory all rigged to produce cars according to a certain blueprint, and the objects as little cars produced by the factory.
In short, the essential bit is that objects are realisations of classes.
In a programming context, you can think of database – eg. a bank’s list of accounts. Here there is a pre-set bunch of information that is necessary for each account. In the bank computer system, the template account is programmed as a class containing certain attributes: Name, address, balance, birth date – and so on.
Instead of having to rewrite the whole class everytime a person wanted a new account, Object Oriented Programming allows the system to just instantiate a new object of the account class, and fill in the necessary information.
With me thus far? If not, try looking at the last 4 slides from this presentation.
Good.
As I said, each object consists of methods and attributes (and probably some more stuff they haven’t told us about yet).
Methods are used for grouping and naming sequences of statements (or commands) so that it is easy for the program to call upon that sequence merely by the method name.
Methods can be used as input, eg.:
System.out.println(“Hello there!”);
“System.out.println” is the call to the method “println” which happens to be located in another class named “System”. Thus, everytime you want to call any of the methods from the “System” class you will have to refer to that class by writing “System” before the method you want to use. The method “println” accepts the argument entered in the parenthesis and prints that on the screen.
Methods are always called with zero or more arguments or parameters, but that basically means that you always have to put the parenthesis there – even if you don’t want to call any parameters.
Finally, there’s the semi-colon at the end of the line. Java syntax demands the semi-colon in order for it to treat the line as a statement. It is in fact the semi-colon that turns an expression into a statement – and thus it is part of the statement.
Another central part of Java (and presumably most other programming languages) are identifiers. All a program’s specific elements that is defined by a programmer must also have a name to separate and identify them from the other parts, and, hopefully, indicate the proper use and idea of the element. The identifier is the name of any programmer-defined element. In this example, “Happy” identifies the specific class that writes “Oh, happy day!” on the screen:
public class Happy {
public static void main (String[] args){
System.out.println(“Oh happy day!”);
}
}
There are some rules on the naming of identifiers:
1) The first character must be Java letter (these are lower case a-z, uppercase A-Z, $ and _ and few more).
2) The rest of the Identifier can contain any and as many Java letters and Java numbers (the numerals 0-9), as long as there are no whitespaces.
Java convention also demands that when classes and packages should always be named with an uppercase letter (as with the “Happy” above), while attributes, parameters, methods and variables (more on these later) are written with lower case.
If you want to give an identifier a name with more than one word, you write the first letter of each new word in Upper case – eg. “HappyDay” (a class) or “labelColor” (an parameter).
The rule for identifiers is that they should be short, concise and easily recognizable and distinguishable.
Another central part of computer programming is the literals – these are the atomic units of information used in the programming language, and they’re used to supply specific data to programs – often as variables. A variable is a named slot or “box” in the computer’s memory that can be assigned a literal value of some sort. There are several types of literals, and when you assign memory you (“open a little box in the big warehouse of the computer memory”) need to declare which type of literal you want to assign to that slot (“what kind of box you want, depending on what type you want it to contain”).
The rules of Java demand that each variable has a specified type. Other programming languages are more lax about this sort of thing, but Java has a “strong syntax” that require all variables to be declared.
int height;
double weight;
String name;
Where “int” and “double” signifies the types of variables, and “height” and “weight” are the names (identifiers) of the variables.
Thus, the type signifies the type of “box”, while the identifier signifies the name of the “box”. Whenever you want to get something from or put something into the box, you need to call it by its exact name.
So what types of literals are there? What kind of information can we store in our boxes of memory?
There are many. And they vary depending on how much space they take up in the computer memory. It is worth remembering that computers used to be have very little memory, and that it was necessary to worry a fair bit about “memory management” – ie. the way that the computer stored relevant data in its memory. This is less relevant today, but is still worth noting.
So far, we have learned about the following types:
Integers:
Integers are whole numbers (ie. without any decimals) such as 1, 43 and -17.
There are 4 types of integers – which can contain different sizes of numbers depending on how many bits (short for Binary digIT – The smallest piece of data (a 1 or a 0) that a computer recognizes) it takes up in the computer memory. Bigger numbers take up more memory:
byte – 8 bits – can contain values from -128 to 127
short – 16 bits – can contain values from -32768 to 32767
int – 32 bits – can contain values from minus to plus 2.147 billion
long – 64 bits – insanely large numbers. No, really: Seriously big.
Just to complicate matters further, computers aren’t limited to the conventional decimal numbers that we’re used to. As noted above, the basic atomic unit of the computer is binary, and it does octal (0-7) and hexadecimal integers (0-9,a-f) with similar ease. I find these different base-2, base-8 and base-16 count systems to be a serious mindfuck but they are a central and integral part of computer science. Anyway, back to the types:
Decimal numbers:
Are fractions. These are numbers like 0.1 or -4.5 or 438.75 or 3.14159.
They come in two variants, depending on how precise you want your fraction:
float – 32 bits – 7 significant digits
double – 64 bits – 15 significant digits
Due to the way that computers calculate fractions – something called floating point calculation – fractions and decimals are always approximations. Though if I claimed to understand why this is, I would be lying through my teeth.
Characters:
Are single keyboard characters such as a lower case ‘a’ or an uppercase ‘A’, a ‘C’ or a ‘ ‘ (a whitespace) or some such. All of these characters have matching coded numbers in a specific system. In the widespread ASCII system, the upper case ‘A’ is represented by the number 65, while lower case ‘r’ is represented by the number 114.
ASCII is limited in that it only has room for 128 different characters (cf. the memory limitations discussed above), so nowadays most people use the Unicode system instead, which has room for 65535 different characters – plenty of room, not just for the Danish Ã?, Ã? and Ã? but for all of the world’s major alphabets such as Greek, Arabic, Cyrillic, Hebrew, and all sorts of far-Eastern variants. So far we’ve learned of one character type, called
Char – 16 bits
Each Char memory slot or “box” can only contain one character – which is in fact their unicode numerical representation.
Boolean Values:
Are truth variables. A simple 0 or 1 – True or False. The strange name is apparently in honour of British mathematician George Boole and his “Boolean Algebra”. Again, esoteric mathematics are lurking in the background of the programming jargon though it isn’t necessary to understand the theory in order to be able to use it. We’ve heard of one kind of Boolean type thus far:
boolean – 1 bit (presumably)
Strings:
Are strings of arbitrary characters. Whereas the other types are so-called “primitive types”, a string is something of a different categories of which I still know next to nothing. A string is always encapsulated in quotes, like this: “Hello World!” or this: “Hell yeah!”
Strings are also different in the way that when you declare them, you have to capitalize the first letter like a class:
String – size varies?
With all these different kinds of types – or boxes – we can assign values to these variables. Assignment is much like putting stuff in the boxes. It works like this (based on the variables we declared in the example above):
height = 180;
weight = 78,5;
name = “Carl Smart”;
The variable name is always on the left, and the assigned value is always on the right.
You can declare a variable and assign it a value in one statement. This combination of declaration and assignment is called Initialisation:
int height = 180, age = 45;
This is a Good Thing to do, as computer memory is rarely completely empty. When you allocate a bit of memory to your “box”, it doesn’t automatically clear out whatever was in that memory beforehand and it might still contain some random value. Since variables can be changed several times within in a method, it is good to ensure that it contains exactly the value that you want it to contain.
It is also possible to declare constants – which work just like variables, except that they’re not, you know, variable. You declare a constant by prefixing your declaration with a “final”, like this:
final double PI = 3.14159;
It is Java convention that constants are written in all-caps so that you easily can set them apart from the variables.
Another central part of any programming language is the expressions. These consist of operators and operands.
Operators are basic arithmetical operators:
+ addition
– subtraction
* multiplication
/ division
% remainder of a division (if you divide 123 by 10, you’ll have a remainder of 23).
= assignment (as we saw it done above)
Operands are the values of other expressions. The values in the boxes.
The operands are the input and the return value is the output of an expression.
Most often, an expression won’t change the value of a variable (unless you actively assign the variable a new value), but will just read off the value. “Think of them as references, for the most part.” Our lecturer said.
A typical expression would look something like this:
1 + 1;
or:
weight / (height*height) = bodyMassIndex;
Where you assign the result a the expression to a new variable. Though the result would only would be correct if the integer operand “height” is assigned a value in meters, which you probably wouldn’t want to do as that would make most people either 1 or 2 meters tall – with no decimals allowed.
But since the “weight” operand is a double, it is necessary to “promote” the other operand “height” to double status for as long as it takes to make the calculation. The result will be presented as either an integer or a double, depending on what type the new variable “bodyMassIndex” has been declared.
Having introduced classes, methods, identifiers, primitive types and expressions – this is probably a good place to stop, even though this is only half a lecture.
I’m absolutely amazed at the amount of new concepts and ideas that is introduced in programming. Even though it only requires basic math to use, there are so many layers of culture and tradition built into these languages that just trying to explain why it is this way is a solid effort.
One thing is understanding it, another thing is actually writing it and thinking in the proper manner. It is a quite fascinating thing to do. More about this soon.
2 Comments
Add Yours →Andreas I’m back reading the blog again. Tell Stefan to reinitialize me on the nefan board!
James
Hi James!
Sorry to be posting so much anthropology and programming stuff – can’t be that interesting for you.
Don’t quite know what’s up with the board, but I’ll tell Stefan to have a look at it.