Published on
O'Reilly (http://oreilly.com/)
See this if you're having trouble printing code examples
Why Learn Java?
by Jonathan Knudsen
06/01/2000
If you work with computers at all, you probably think of Java as a computing
platform rather than as a coffee or an island in the Pacific. Java has
generated tremendous buzz in its first five years of existence. Some of the
excitement is genuine; some is just hype. But at the end of the day, you're
probably wondering if you should learn Java. The answer is "Yes." I'll explain
why.
What is Java?
First, though, you need to understand exactly what Java is. When people
say "Java," there are actually several things they might be talking about:
-
Java is an object oriented programming language. In this respect, it is a
peer of C++ or Smalltalk. Learning the Java language is kind of like learning
the rules of grammar for a spoken language.
-
A Java Virtual Machine (JVM) is used to run Java programs. The JVM can be
made to run on different platforms, like Linux and Windows, so the same
Java programs can run on computers with different operating systems. This is
the cornerstone of one of Sun's slogans for Java: Write Once, Run Anywhere.
The JVM is infrastructure, kind of like vocal cords for spoken languages.
-
The Java Application Programming Interfaces (APIs, or class libraries) are
a set of prebuilt classes that you can use in your own programs. Essentially,
this is stuff for free, like classes that deal with disk files, network
sockets, or graphic interfaces. If the Java language is like a set of
grammar rules in a spoken language, then the APIs are dictionaries full of
words that can be used to build sentences and paragraphs and stories.
When people talk about "learning Java," they usually mean learning the Java
language and APIs. You probably won't ever have to know much about the JVM,
although a basic familiarity with it is helpful.
Object Oriented Programming
Object oriented (OO) programming with languages like C++ or Java is widely
accepted to be a step above procedural programming with languages like C or
FORTRAN. The added power of OO programming comes at the price of the relative
simplicity of procedural programming. There's a pretty steep learning curve
involved with OO programming. If you already know an OO language, learning
Java will simply be a matter of learning Java's syntax.
If you don't understand OO programming, you'll have to begin the long journey
to enlightenment. It took me about four years from when I first started
playing with OO programming (in Objective-C, on NeXT) to when I really felt
like I got it. My study was a little haphazard; you can probably cut that
time to a number of months if you are taking a good course or immerse
yourself in some good books. Don't neglect this step, however. It's not hard
to learn Java's syntax, but writing decent OO code takes some serious work.
|
There are technical and non-technical reasons for learning Java. I'll start
by describing two code-level features, garbage collection and exceptions,
that make Java cool. Then I'll talk about how Java is almost unavoidable in
the programming world and the attractive price tag on Java tools and
information.
Thank God for Garbage Collection
C++ allows you to screw up in spectacular ways, ways that Java simply
doesn't allow. The number one bug in C++ has to do with memory management.
In C++, you have to explicitly ask for memory when you need it. When you're
finished with it, you have to explicitly give it back. It sounds simple,
but it's unbelievably easy to have a memory leak, which is when your
application asks for memory and forgets to give it back. Over time,
the application grows in size until it is bursting at the boundaries of
your computer.
No such thing happens in Java. Although you still request memory explicitly,
the system recovers memory automatically using some magic called garbage
collection. You lose some efficiency at runtime, but you'll probably never
notice. Garbage collection cuts your development and debug time dramatically.
Exceptions Make for Better Code
It's easy to write bad code in any language, but it's not quite so easy in
Java. One reason is garbage collection; another big one is exceptions.
Good programmers anticipate the worst, and they design applications that
behave reasonably when things go wrong. If you couldn't open that file for
some reason, what do you do? What if there's an error reading the file?
In C++ and other languages, it's easy to ignore the return values of
functions and just assume everything will work. This most often happens
when you're in that I'm-just-trying-to-get-it-running stage. In the back of
your mind, you're planning to make it more robust later. The problem is that
as soon as you get it running, you have to rush off and work on something
else. When you finally try to run the program in a real-world situation,
your get-it-running code breaks and you're forced to go back and fix it
up anyway.
Java forces you to plan for the worst the first time you code. Java methods
can throw exceptions, which basically signals the calling code that something
has gone wrong. The trick is that you have to acknowledge the exceptions,
even if you don't do anything about them. If you write code that opens a
file, you have to write a handler for an exception in case anything bad
happens. If you plan ahead a little, you can handle exceptions in a rational
manner and end up with a program that gracefully handles trouble on your
first writing.
It's Everywhere
Java has the advantage of ubiquity. If you program for a living, you're
probably going to come across Java sooner or later. Java is showing up in
all sorts of places: You can write Java servlets that run on Apache and many
other web servers. You can write applets that run in the browsers that most
of the world uses. You can even write database stored procedures in Java
with Oracle 8i.
It's also a good bet that many of your colleagues will know Java. It
displaced C as the standard teaching language in universities sometime
around 1997. As such, it is basically a lingua franca for computer programmers.
It's probably worth learning Java even if the only benefit is being able to
improve your communications with your peers.
Don't miss
Java Jonathan's Top Ten Tips and Tricks.
Finally, the Write Once, Run Anywhere promise does hold some water, although
earlier versions of Java (1.0 and 1.1) tended to be less willing to cross
platforms seamlessly. If you want to distribute an application across multiple
platforms without rewriting significant pieces of it, Java is a great choice.
The Java 2 platform is currently available for Linux, many other flavors of
Unix, and Windows.
Support for Java on MacOS 9 and earlier is still at the Java
1.1 stage. However, Apple's next-generation operating system, OS X, is based on
a Unix core, which greatly simplifies the porting process. I don't know what
Apple's official word is, but I saw Java 2 running on OS X developer release 3
back in March. I'm pretty confident that Java 2 will run on Linux, OS X, and
Windows, which basically covers the entire known computing world.
It's Free, in Several Different Ways
Sun gives away Java development tools and the stuff you'll need (a runtime
environment) to run Java applications. There are no license fees to be paid, no
nasty multi-thousand-dollar development environments to buy. There isn't even a
catch, like that you have to shell out a few G's to get the Professional
Edition that does all the cool stuff. Sure, Sun has their own agenda; Scott
McNealy would be more than happy to take over the world. But you really can use
Java without paying anyone.
Furthermore, the APIs are a vast treasure trove of working, reliable code that
help you do all sorts of stuff. Want to use network sockets? It's in there.
Want to connect to a SQL database? It's in there. Want to put a slick user
interface on your application? It's in there. Want to digitally process images,
or manipulate music files, or parse strings, or create digital signatures?
It's all available. As a programmer, this will probably gladden your heart more
than anything: Java's APIs are chock full of wheels that you won't ever have to
invent yourself. This is really nice when compared with C++. You could spend
all sorts of time learning somebody's C++ class libraries, like the Microsoft
Foundation Classes, then start at the bottom of the learning curve again when
you had to write an application for Linux. Once you learn the Java APIs, they
work the same regardless of the underlying platform.
Finally, oodles of information about Java is available for free online. If you
have Internet access and enough time, you can teach yourself OO programming,
the Java language, and the Java APIs. Sun has a pretty good tutorial online
and tons of API documentation. For a more focused approach, try a good book
(say, for instance,
Learning Java) or find someone to explain it to you. Bingo! You've
just made your resume look a lot sweeter. Expect to be swamped with calls
from recruiters.
Get a Tan
The heart of the matter is that Java is more fun than other programming
languages. It doesn't allow you to mess up in some very important ways, which
means you can get your work done faster than with other languages. Anecdotal
evidence suggests that you will program in Java anywhere from four to ten times
faster than in other languages. This sounds preposterous, I know. Don't take my
word for it--ask around. Less time tracking down bugs means more time on the
beach, which means you can finally get a tan from the sun instead of your
monitor. Buy yourself some sunglasses with your new Java-pumped salary.
Jonathan Knudsen is the Courseware Writer for
LearningPatterns.com, a Java training company.
He is a coauthor of
Learning Java, and his other books include
The Unofficial Guide to LEGO MINDSTORMS Robots,
and
Java 2D Graphics. He works at home in New Jersey with his wife
and three (soon to be four) children.
O'Reilly & Associates recently released
Learning Java.
Copyright © 2009 O'Reilly Media, Inc.