| Download the white paper |
Why
Use Java? |
A white paper describing why, in my opinion, Java is
a Good Thing for embedded programming. The white paper does not include all the additional
comments below. |
| My background and perspective |
| I have been working with embedded systems for 20+ years, starting
at an IBM research lab with assembly code and hardware design of a multi-68000 CPU. This was
the original 68000 running at a mind-boggling 4 MHz (about 4X faster than other controllers
of the day), and packaged in a huge 68-lead DIP which we called the "Hershey bar package".
I hated programming until I discovered C in the mid-1980s. I'm a degreed Electrical Engineer
with a
math minor
and some
graduate course
work.
I've been an adjunct
assistant professor at the University of Utah School of Computing where I taught the CS/CE
4710 class for three years. Most of my microcontroller hardware design experience is on 8051s,
including the high speed Dallas cores 80C320/520/530/550/390/400, Intel/AMD 80188, and some
bit slice
and
other odd bits here and there. Many of these systems were "soft" realtime and some
were "hard" realtime with possible loss of life (mine, in at least one case!) if
the system failed unsafely. A lot of these systems were mixed analog and digital (like the
estuaries where fresh- and salt- water meet, that is usually where the interesting stuff happens).
I've designed successful systems which included laser
diodes, high precision optics, photodetectors, hydraulic pumps, servovalves, stepper and DC
motors, LVDTs, satellite communications links, high speed graphics coprocessors, video interfacing,
etc. I have designed ASICs, boards and multi-board systems. Systems were programmed in realtime
embedded BASIC, assembly code and C. I've also worked on some systems which were technical
and/or marketing flops.
I wrote and self-published a book documenting the Intel 8052 BASIC interpreter, and it has
sold over 10,000 copies worldwide. At the time, BASIC was the best choice for an easy to use
embedded system. In case you are wondering, this predated the BASIC Stamp. In fact, I met with
Chip Gracey of Parallax when he was still working out of his apartment. We discussed the future
of embedded systems and how we might collaborate on it. I pitched BASIC as the language of
choice, and left him a copy of my book. He was dubious about BASIC at first but the rest -as
they say - is history. This meeting planted the seed of what Chip grew into the very successful
BASIC Stamp. Later I headed a design team which wrote the first (and maybe still the only)
realtime BASIC compiler for the 8051 family. It received an EDN 100 Hot Products of the year
award in
1994.
So I come from the perspective of someone who has designed embedded control hardware and sofware using the "status quo" microcontrollers programmed in C, assembly and BASIC. |
| Why should you believe anything you read here? |
| Ha! Good question. You shouldn't swallow anyone's advice whole, including
any dispensed here. All these opinions are based on my personal experience solo and as
part of small to large design teams. Your mileage may vary. |
| What makes Java better than C/C++? |
- Java is not magical, it just uses about 30-year newer technology than C. When C first
came out, networking did not exist. Objects were unknown. Systems with 32 KBytes of
memory were considered large. All this has changed in the past 30 years. So you would
hope that programming languages would also change to keep up.
- Java has a consistent set of proven and stable APIs for TCP/IP networking,
and has had from day one. No other language can claim that. I don't know about you,
but one thing that drove me batty about C is the plethora of non-standard, arbitrary
add-on packages for serial and network I/O. How many of these do you want to learn
in your life?
- Java has standard packages for common I/O - for example javax.comm for asynchronous
serial and parallel I/O. We use javax.comm on multiple embedded and PC platforms with
little or no problem. We've never come close to that degree of portability with C.
- Java has a very robust, mature security
model which is has been pounded on by a huge community. You can't say the same for
Vendor X's custom TCP/IP stack and custom security provisions.
- Java has built-in exception handling, which C lacks completely. It's possible to
circumvent Java's good intentions but at least you have to consciously do so. C gives
you no help
at all.
- Java programs are potentially much more reliable. C has
essentially no runtime error checking, memory allocation is all manual, etc. Java does
memory management automatically, bounds-checks array
access, etc so it handles the big problems for you.
- Programmer productivity is at least 2X greater with Java. You can get a lot done
in a short amount of time with Java because it has such a rich library of functions
already
built
into
the language. There is a huge amount of open source, free software which you can probably
leverage -- and most of the time it will actually compile and run on *your* hardware!
- A good programmer can write more good, robust, re-usable code in
Java than the same programmer using other language tools. Sure, a bad programmer can
write lousy code in any language, so you still have to use your brain with Java. It's
not a panacea.
|
| So why (and where) should I use Java? |
- New projects which involve multiple levels of hardware and sofware. For example, an embedded system, a PC or local server gateway, and backend processing. Here Java is a huge win, since each team can share the same tools and APIs. (Try that in C.)
- Systems
with TCP/IP. Java has a consistent set of proven and stable APIs for networking,
and has had from day one.
- Systems where network security is important. Java has a very robust, mature security
model.
- Systems which will be maintained and updated frequently. Most of the total lifecycle cost of a hardware/software system is in maintenance, not in the initial development. Java is a clear winner here because of better built in documentation, standard libraries, built in exception handing, and programmer efficiency.
- Systems
where exception and error handling is critical. Java has a good deal of enforced exception
and error handling (C has essentially none).
- Systems where runtime reliability is important. You are thinking "where is this *not* the case? But C has essentially no runtime error checking, memory allocation is all manual, etc. Java does memory management automatically, bounds-checks array access, etc so it handles the big problems for you.
- Fast system development and prototyping. You can get a lot done in a short amount
of time with Java.
- Multi-threaded applications, if you use good design practices and some additional tools
such as CSP (see the Related Sites links).
|
| Where should I *not* use Java? | Java
is not the perfect tool for every job. Nothing is. A skilled craftsman doesn't try
to use tools indiscriminately, but picks the best tool for a specific task. Java
may not be the right choice for:
- small, fixed purpose, simple, high-volume, price-sensitive systems. The world's largest
use of 4-bit micros, at least a few years ago, was in rice cookers. We own two, and they
cost under $50. I can't imagine any benefit to using anything other than assembly code,
on a single-chip micro, in an application like this. This might change in the future
if someone develops a practical "micro-Java", and such work is underway, for
example James Caska's muvium (see the Related Sites
links).
- Systems which will never be maintained and updated. This generally means simple fixed
purpose systems. I've never upgraded the firmware in any of my car stereos, microwave
oven, dishwasher, etc, have you? In such systems the overhead of Java may not be worth
the benefit. There's a threshhold here at which the system complexity is large enough
that Java would accrue benefits.
- Legacy apps which work fine. For example, the PalmOS and its development environment is mature and stable. Why re-write it all in Java?
Don't use any tool just to use it.
- DSP
hardware. I don't know of any available Java for DSP processors, although there are some
projects underway to port Java to DSP.
- Extremely high speed control systems. If you are using a fast bit slice or RISC chip with
hand coding (by experienced people) and barely meeting your speed requirements, the last thing you
need is any high level language to get in your way.
- On a complex and already late project written in C and/or assembly. This is not the time to be learning
a new tool. Java is not a "silver bullet".
- Don't use a code converter and try to convert C to Java. Such tools exist but
the anecdotal evidence is that the Java code they create is not well-written. How
could it be? Object-oriented code needs to be well-engineered as such from the
beginning. Conversion tools are a long way from being smart enough to do that for
you.
|
| I know C. How easily will I learn Java? |
| Java is like C++ done right. All the core syntax
is derived from C. "All" you need to learn is the concept of Objects,
and the Java APIs. OK, I'm only partly serious, since learning how to apply Objects
well is not trivial. And there are a lot of Java APIs to
digest
(more
on the "big
iron" Java such as J2EE than embedded Java systems). If you use Java a lot,
you will start to be really proficient in a year. (How long did it take you to
learn
C
or assembly?)
Java
is a sophisticated language and issues such as threading and event handling can
be tricky. I can't recall anyone claiming that Java was overly easy to learn.
BASIC is probably the easiest language to learn -- but also the least standardized,
least portable, and least structured. Java scales very well as an application
grows. BASIC typically crumbles. |
|