For better or worse, Java is one of the dominant languages in the software engineering industry. As a software engineering professional, you’ll quite possibly have to work with a Java code base at one point in your career. I am not a big fan of Java, and I don’t think it’s a good first language in the computer science curriculum. Sadly, this is often the case. In fact, Java is a horrible first language. The first two “real” languages I learnt were Python and Scheme (Lisp), and thus I was used to elegance and expressiveness. Java felt like a big step back. For instance, here are a few lines of Python code that print the numbers from 1 to 5:
for i in range(1, 5): print i
Here is the equivalent in Java:
package demo; public class RangeDemo { public static void main(String[] args) { for (int i = 1; i < 5; i++) { System.out.println(i); } } }
Following Java style guides, you end up with a staggering 11 lines of code instead of 2. Just look at all the wasted horizontal space and the baroque syntax! Proponents of Java like to point out that their language was never designed to write simple “scripts”, but Java bloat is a factor no matter how large your program is. You’ll end up writing reams of code that do very little, and you quickly end up wondering why the code base is metastasizing in front of your eyes. What Python could do in, say, 100 lines could easily require about 1,000 lines in Java. This is not an exaggeration.
I could write a couple of blog posts on the downsides of Java, but my point is merely that there are better languages to learn as a beginner. You are probably better off checking out Python first if you are new to programming. But let’s say you’ve got no choice because you have enrolled in a computer science program, and now you find yourself sitting in an intro class, confused that CS isn’t about playing video games, web design, or using MS Office. Your instructor wields a 1,500 page tome on Java and pontificates about its “industrial strength” and the marvels of object-orientation.
Let’s start with textbooks first. Textbooks seem to get bigger and bigger so that they can be sold for more and more money. Yet, didactically, they only get worse. In case of Java, though, I do think that common textbooks mirror the verbosity of the language they describe in a wondrous way. The OOP course at university I attended used Liang’s Introduction to Java Programming. Just like other similar books, it lacks proper structure, and has absolutely no qualms about wasting the student’s time. The exercises were dull, repetitive and uninspiring. Fortunately, there is a free alternative out there that covers all bases, at least as far as introductory courses are concerned. It is Allen B. Downey’s Think Java: How to Think Like a Computer Scientist. In less than 300 (small) pages, you’ll get acquainted with the basics of the language. If you go through this book in a week or two, then a semester-long course on OOP with Java will teach you barely anything new. It will just take five times longer.
Some teachers tell beginning students to start programming in a text editor first, and to compile and execute their code via the command line. In the case of Java, this is seriously misguided advice. You do not want to spend hours looking for a missing curly bracket because you thought you were “hardcore” and turned off syntax highlighting in your editor. Likewise, tasks like compiling and executing your program are simply repetitive time wasters that should be automated. There is no learning experience in saddling yourself with that kind of manual labor. So, instead of an editor use an IDE. Eclipse is as just bloated as Java, and overkill for what you do in your typical first- or second-year CS course. A more lightweight (and free) alternative is DrJava.
Lastly, you’ll want to do plenty of exercises to get proficient at, well, writing code in Java. Unlike some textbook authors I see little point in going through exercises that have you write classes that don’t do anything but make you resent “object orientation” à la Java. After you’ve written a handful of those, you know how this works. Instead, you should practice programmatic thinking. For this purpose, the Java section on CodingBat is an excellent resource, providing a few hundred exercises. They won’t teach you everything you need to learn, but they will put you well ahead of the pack. If you get stuck, you are welcome to consult the solutions on my website.
Lastly, you might want to check out Stanford’s CS106A: Programming Methodology, which is a free online course that offers an introduction to programming with Java. This course and its associated textbook, The Art and Science of Java by Eric Roberts, start out with a special set of Java libraries, the ACM Java Libraries. Those hide some of the complexities of the language. They allow you to write more concise code, and they also lead to a gentle introduction to object-orientation with Java. The course itself is your standard-fare remedial programming course.
The lecturer, Mehran Sahami, is entertaining and competent, and the course focusses on pretty interesting programming tasks. One of the first bigger projects consists of writing a Breakout clone, for instance. Also, I really liked the introductory sequence, which covers “Karel the Robot”. If you’ve never programmed before and have to get started with Java, this would indeed be a very good starting point. Using those resources, you’ll be well on your way to becoming competent in Java, but I hope you’ll explore some other programming languages too.