Tuesday, November 30, 2010

Getting going

Greetings all. My name is Bill, and I'm a Java developer. ("Hi Bill.") My intent for this blog is to be a resource for discussing different aspects of the Java programming language, from the most basic to more advanced topics. Hopefully there will be something for everyone.

I don't have any fixed plans on how I'll approach things. I'll probably bounce between some pretty basic stuff and some more out-there topics, rather than use a more traditional, build-as-you-go model. I think it'll be more interesting that way. It'll give Java veterans (including me) some refreshers, and give Java newbies a peek ahead.

One overriding theme I intend to stick to is that of getting some code done and working. You could spend a lot of time delving into the details of, say, visibility or thread safety or any of many, many other Java topics, but instead I'm going to treat each topic like I treat Christmas shopping: get in, get what you need, get out. You probably have a job to do, anyway, so let's not spend too much time on abstractions.

Still, I do have to start somewhere, with some explanation. So, let's talk about what an object is. For most of you, this may be boring. You're free to wait until my next posting, which might be more to your liking.

Java is all about objects. It's object-oriented, after all. All the code you write will be contained in objects, and that's what you'll run, and break and fix, and deliver.

An object is a hunk of data, or code, or both. That's pretty much it. Some objects are only data, and some are only code, but most are both. Also, the code in an object tends to use the data in an object, although it doesn't have to. From a design point of view, though, you want to keep data and related code together.

Now that you know what an object is, let's make one. You have to define it, and that definition is called a class. I still remember the first class I wrote ... in C++ actually, not Java. You see, before I started learning C++, another object-oriented language, I was working in C, which is a procedural language that doesn't support objects. You could still write functions, though, and so I imagined that in C I could write a function to roll some dice. In pseudo-code, it might work like this:

int[] rollDice (int numberOfDice) {
    int[] results = new int [numberOfDice];
    for (int i = 0; i < numberOfDice; i++) {
      results[i] = random number between 1 and 6
    return results;

So, I thought, in an OBJECT-ORIENTED WORLD, it's about the dice, not the rolling. The dice roll themselves! What an epiphany.

Let's start a Java class for a die, which is one dice ... one of a set of dice ... well, you know what I mean.

class Die {
  int numberOfSides = 6;
  int roll() {
    return 2;

OK, well, it's a loaded die right now. But you can see that it has some data (the number of sides) and some code (to roll it). The data would normally be called a "field", while the code would be called a "method".

Let's get just a little more detailed here, just so all our bases are covered for those new to Java.

This class definition starts with the word "class", followed by the name of the class. Class names should always be capitalized; it's a convention. For multi-word class names, use CamelCaseLikeThis. After the class name, the definition is in curly brackets, or curly braces, or curlies, or whatever you want to call them.

The field called "numberOfSides" is set equal to 6, which is hopefully pretty obvious. The field has a "type", which describes what sort of data it is. This field is type "int", which is short for "integer", which is a whole number ranging from around negative two billion to around positive two billion. So, you know, a regular number without a fractional part.

The method called "roll" also has a type, which indicates what sort of data it produces. This method always produces the number 2, which we can represent as an int. The 2 gets send back out of the method by that "return" statement. You can see that curly brackets are used again here, this time to hold the body of the method. And, last thing: The method name is followed by parentheses, which lets you know that it's a method and not a field or something else.

So, in my opinion, that's plenty of information to get this blog off and running. To bootstrap it, if you will. This class is by no means done, but it will compile. In ensuing posts we'll improve it, and even get it to where we can run it.