Practical
Embedded Java

What's so great about embedded Java?


A practical engineering approach to using embedded Java in real-world applications.


...



There is a lot of (mis)information flying around about what Java is and what it isn't. At trade shows, conferences, email lists, and on the phone, customers ask similar questions. Here are some of the most common questions and answers. Updated 2009 Oct 29.

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++?
  1. 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.
  2. 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?
  3. 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.
  4. 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.
  5. Java has built-in exception handling, which C lacks completely. It's possible to circumvent Java's good intentions (too often this is the case - please, Java coders - don't do this!) but at least you have to consciously do so. C gives you no help at all.
  6. 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. Of course this is not free - there is a performance penalty for this. But compared to the "penalty" of a crashing application, which would you rather have?
  7. 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!
  8. In a given amount of time, 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?
  1. 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.)
  2. Systems with TCP/IP. Java has a consistent set of proven and stable APIs for networking, and has had from day one.
  3. Systems where network security is important. Java has a very robust, mature security model.
  4. 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.
  5. Systems where exception and error handling is critical. Java has a good deal of enforced exception and error handling (C has essentially none).
  6. 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.
  7. Fast system development and prototyping. You can get a lot done in a short amount of time with Java.
  8. 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:

  1. 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).
  2. 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.
  3. Legacy apps which work fine. For example, the PalmOS and its development environment is mature and stable. Why re-write it all in Java?
  4. Don't use any tool just to use it.
  5. DSP hardware. I don't know of any available Java for DSP processors, although there are some projects underway to port Java to DSP.
  6. 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.
  7. 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".
  8. 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.
  9. But wait a minute - isn't the underlying JVM that provides all these benefits written in C? Yes, in general, it is. So your Java code *is* only as robust as the underlying JVM. But a huge amount of resources have gone into JVM development to achieve just that goal. So it makes sense to leverage that (relatively small) amount of highly reliable, extensively tested code. It's similar to why the original Macintosh and Palm devices were so popular with programmers - all the critical runtime library support was contained in system ROM and could be recycled endlessly by legions of coders.

What about Android and the Dalvik Virtual Machine? Is it Java?

Android is stirring up a lot of interest and controversy. If it walks like a duck and quacks like a duck, is it a duck?

Google maneuvered around the Sun Java licensing requirements by not using the Sun Java Virtual Machine. The JVM is what has to be licensed, not the use of Java Syntax or Java development tools. So Google purchase Android and developed the Dalvik virtual machine which does not create Java byte codes. This is a mixed blessing - Android apps cannot benefit from existing Java accelerators (e.g. Jazelle) or optimizers (e.g. HotSpot).

But the good news is that as far as the source code is concerned, Android is really Java.

Dalvik is, IMO, nothing like PhoneME. In my (admittedly limited) experience this is a good thing.


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.

 
Systronix® 939 Edison St, Salt Lake City, Utah, USA 84111
Tel +1-801-534-1017, Fax +1-801-534-1019
contact us     Time Zone: MDT (UTC-6)
 

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Systronix is independent of Sun Microsystems, Inc.
TStik, JStik, JCX, JStamp, JSimm, JDroid, and JRealTime are trademarks of Systronix, Inc.
1-Wire, iButton and TINI are trademarks of Dallas Semiconductor
Simmstick is a trademark of Dontronics
LEGO® is a trademark of Lego A/S, Denmark