A couple of weeks ago Mike challenged me to build my Java HTTP server and Java tic-tac-toe game by the end of this month. I knew that learning another new language would take some time, but the shift to a statically-, strongly-typed language has been particularly painful. Combine it with working on both a client project and an internal project the past couple of weeks… and October 31st is approaching a little too quickly for my liking.

It’s a good thing that when I say Java I think of coffee so that I at least have some pleasant association with the word. However, as I explained that to a friend she said, “that’s funny, because every time you say Java I immediately think of ‘Jabba the Hut’”… which made me think of a large, unwieldy, monstrosity not unlike a certain programming language … and that I might end up frozen in carbonite if I don’t finish this task on time … but I digress.

The biggest problems for me with Java have really been the basics of a static, strong language: declaring variables, declaring the right type of variables, declaring return values, declaring the right type of return values, and using data sets. For anyone not familiar with java, here’s a couple quick examples:

Declaring a variable

If you want to use a variable in Java you need to declare its type, and if it’s outside of a method you also need to say whether it’s public or private. So if I want to use a variable in my class that contains console output I couldn’t use it until I declared it:

private String consoleOutput;

Now, inside of a method I could assign it as normal:

consoleOutput = "Please make a move";

(Note that statements in Java end with a semicolon).

Declaring an Array

When using an Array in Java you not only need to declare it, but often you also need to declare what size it is and what type it contains. (You can use multiple types by saying it contains objects of the type Object- but that can cause other problems). Array declaration for an array of 9 integers would look like this:

private int[] numbers = new int[9];

However, if you know what your array will contain right off the bat you can also set it this way:

private int[] otherNumbers =  {1, 2, 3, 4, 5, 6, 7, 8, 9};

Because of course you’d use curly braces to represent your array, right?

Declaring return values and variables in methods

All methods in Java need to have their return value declared. If a method doesn’t return anything then “void” is used. Java also requires that Types are declared for all arguments that come in. Here’s some examples using our numbers arrays from above:

// changes value but doesn't return anything. takes in two integers as arguments.
public void changeArrayValue(int indexToChange, int newValue) {
numbers[indexToChange] = newValue;
}

//changes value and returns new array
public int[] changeAndReturnUpdatedArray(int indexToChange, int newValue) {
numbers[indexToChange] = newValue;
return numbers;
}

//returns true and false
public boolean checkArrayLengthforForgetfulPeople(int lengthGuess) {
if (numbers.length == lengthGuess)
return true;
return false;
}

Note that I’ve made all of these methods public because they could be called on instances of this theoretical class we’re working in… let’s call it NineDigitNumberArray. So to create an instance we’d of course have to declare it’s type

public NineDigitNumberArray newNineDigitArray;

… as you can see, the language can be quite verbose. In hindsight I’m glad that I was able to start with Ruby and Clojure as my first two languages. Both are dynamic, and for lack of a better term, are just more “fun”. But who knows, when I first started learning Clojure I had a similar feeling… going back to Ruby was a breath of fresh air. Now that I started learning Java, when I went back to working on a Clojure project this week it felt like a breath of fresh air. Fortunately, the past couple of days Java has finally started to click… on to tic-tac-toe.