Java development basics


java programming language

s menu - click a section what's new at www.eadon.com philosophy movie reviews cartoons - garden of eadon cartoons bible satire pics, images and poems about nun whipping bishops etc :) philosophy wars discussions and battles on religion and many other maddening topics Jim on diets, daft names and other musings Feng Shui Hippo's zodiac - a spoof of astrology and feng shui here is info about me, jim eadon and more read my novel madpole - the maddest but truest philosopher on this planet coincidences of readers etc read and sign my guestbook links s
body frame image body frame image
s





This page is about the programming language Java. The exposition commences with some basic info for the uninitiated, then eases up to advanced ideas. Also an explanation is provided about why Microsoft's proprietary ".NET" is evil and should be avoided like the plague.

Intro: Java Basics

This page is laden with technical jargon. Don't despair if you don't understand some of it, that's normal, just skip past the stuff you are not sure about :)

Java is often misunderstood. Foremost it should be observed that the "Java" programming language is not the same language as the slightly dodgy "JavaScript" language. JavaScript is embedded inside fine web browsers like Firefox, Opera, etc. - and also the rubbish accursed-to-God Internet Explorer (IE). (Incidentally, "JavaScript" is more properly called "ECMAScript". Alas! Both names are abominable!) As it happens, as well as "JavaScript" code, you have probably seen actual Java programs run inside browsers - as Java "Applets". An applet is a special kind of Java program that the browser executes. All you need to know is that Java and JavaScript are different languages. They have little in common except some common syntax. The syntax of both languages is based the C family of languages as it happens. The syntax of Java is based on the great but deadly C++ programming language.

Usually, Java is used to program applications, anything ranging from games that run in mobile phones, to government mega-projects, and nearly everything in between. It's probably easier to say what Java is not generally for at the time of writing. Scientific programming is usually handled by FORTRAN. Low level operating system software and drivers are usually written in C or C++. Scripting (slowish "interpreted" languages for light programming jobs) is for Python, Groovy, Ruby, Lua, Bash (Unix and Linux) etc. Java multimedia support is lacking on the desktop: Sun, the creators of Java really need to fix this hole ASAP! As well as the alternative languages mentioned above, the LISP language is making something of a comeback. There is no best language for all tasks. As they say use the best tool for the job. Java is a superior language for most generic programming these days.

Java advantages: Java is a nice OO language. OO means Object Orientated, where your program code is divided up into little mini-programs called "classes". In Java an object is represented by programming code in its own file, called a "class", which has a .java extension. For example an object representing a pig would be defined by a class in a file called Pig.java. Each class has an assigned specialised responsibility (roughly speaking, an object represents and/or does one thing) and encapsulates its own data and logic. Take an email application: one object might represent an email, and another object might represent an "in box" for example. The "in box" object can "contain" "email" objects. OO program code can be a little tricky for beginners to read and write at first - but on the whole OO is the best way to program for non-trivial tasks.

Java code runs in a "Java Virtual Machine" (JVM). The JVM is an application that is necessary to be installed on your operating system in order for Java to run. In fact, the word "Java" can interchangeably denote the Java programming language and/or the JVM. One advantage of the JVM is that it provides fast execution speed (I mention more about speed below). Another advantage is improved security.

Why improved security? Java, which has C++ like syntax, has superseded the C++ programming language for many programming tasks for several reasons. C++ is shockingly powerful, you can do just about anything with C++, but most of the time it is neither safe nor convenient to program in C++. Java eschews three C++ horrors: pointers, memory management and preprocessor macros. Pointers are great fun, but they often lead to security problems (butter overflows for example), and can make code needlessly tricky to understand and maintain. Memory management is automatic in Java: the JVM has a "garbage collector" that prevents memory leaks and abolishes the need for code to release memory. In my opinion, Java should go the whole hog and expel those gruesome arrays too. Arrays cause many errors. Security is also enhanced by having the Java code run inside the JVM, which acts as a kind of sandbox that "manages" the code to help prevent programming errors or would-be hostile code from causing damage.

Perhaps the most important Java advantage of all is that JVMs exist for all major platforms. So whether you are running Linux or Windows or OS X on a Mac, UNIX, etc. on various hardware you should be able to run exactly the same Java applications and have them work perfectly (with some exceptions for less popular platforms). A Java program is "write once, run anywhere". This portability of Java code is a serious boon. If you write a program in Java on Linux, you do not have to rewrite it (or port it, to use the techie term) so that it runs on Windows. This means that not only can you reuse your software on all the popular operating systems, but other people can most probably run the Java software you wrote on their platform. This not only saves you time and money, but also you can be assured that your Java software will have a very long life indeed, as JVM's will always be ported to new systems even when today's operating systems are obsolete, abandoned and basking in software Heaven (Unix/Linux) or languishing in Hell (Windows).

At time of writing, Sun is opening up Java, making Java fully Free Open Source Software "FOSS". This guarantees that Java will always be around, so that your program code will always be able to run. There are other advantages to FOSS which I talk about here: open source.

Java is fast. There is an out-of-date misconception that it is slow. Maybe back in the 1990's it was, but these days Java is speedy, sometimes faster even than C or C++. This speed improvement is due to the Java "Just In Time" (JIT) compiler that dynamically optimises the code to best suit a computer's state in real time. The only negatives are that Java programs can consume a little more memory than C/C++ programs, and can be relatively slow to start up (at time of writing) as the JVM prepares to run them. Once a Java application is running, it is snappy compared with most other languages.

A great free open source Java programming/development tool is Eclipse. This "Integrated Development Environment" (IDE) provides a Java editor, plus integrated compiler and a wealth of features to assist writing, organising and testing software. Any IDE can be a bit intimidating if you are new to them. The best way to learn is to follow a getting started Hello World-like tutorial or two. Of course you can use any text editor to write Java software. I sometimes use editors to compliment my use of Eclipse. JEdit is handy for its superb "search in directory" feature. Occasionally I use vim (which improves on vi) to edit source code. Vim (or vi) is tricky to learn, but has some super-user features for plain text editing (especially source code) that most IDE's lack.

A spin off language of Java is Groovy. Groovy is designed from the ground up to be interchangeable with Java code: groovy can call Java and vice versa. Groovy is loosely based on Java syntax and runs on the JVM. Where as Java is a compiled, strictly typed OO language, Groovy is a "scripting language" that is "dynamically typed". Groovy is useful for quick and dirty programming tasks where you are not too worried about performance or type checking of variables. Likewise it is suited to writing test code. Most industrial strength business programming is best handled in Java, but groovy is designed for writing code quickly, and ironically groovy has superb language powers that Java lacks at time of writing, such as "closures". Closures are defined elsewhere on the web but essentially they are a neat way of passing code for other code to execute.

Note to Java designers: anonymous classes cannot do what closures do - and anonymous class syntax is horrendously clumsy and ugly. Java needs closures ASAP, and if that goes against some sacrosanct design principal then please compromise the principal. Not all sins are unjust.

Testing

When you write software it is usually a good idea to write test code. In fact it is better to write the test code first! JUnit, a unit test framework (which is built into Eclipse by default), is the way to go for Java.

When it comes to sophisticated unit testing techniques, sometimes it is handy to use "mock objects". Mock objects are place holder objects that dispense dummy replies in response to calls. For example a mock object representing a database could return fake data that you predefine in Java code. This predefined data can be used to test code that calls that database object. Such tests can then test code that would normally require a database to be present. A very nice framework for testing with mock objects is JMock. An alternative framework to JMock is EasyMock, but do not be fooled by the cosy "Easy" aspect of that framework's name: JMock test code is easier to maintain than EasyMock test code, especially when it comes to "refactoring". (To refactor loosely means to improve the code by rearranging and restructuring it so that it conforms to a superior design. Sometimes quick and dirty prototype code can be refactored to improve its quim777ality, so that it can be incorporated into production quality code for example. Eclipse has some superb refactoring tools).

Odds and sods: for performance testing try JMeter. For integration testing (for expert java developers) there is Cruise Control.

Some useful libraries for helping with day-to-day Java development tasks are "Jakarta Commons" libraries hosted by Apache. The most used of these are Commons Lang, which has useful code for manipulating Strings (StringUtils) and Commons Collections, which has useful enhancements to the default Java data structures. A good book on Jakarta Commons can provide some very nice examples that can help save you oodles of time and effort over the years, and can help to simplify your programs, or make them better engineered.

One word of caution: copycat supremo Microsoft blatantly copied (oh sorry, "innovated") the Java language and JVM and called their copycat platform, "C# / .NET / WinFX" or whatever the brand is these days. It is prudent to avoid this stuff, which I'll call .NET for short for several reasons. 1) MS is unethical and screws people over (customers, would-be competitors, the tax payer etc.) far more than other IT megacorps like IBM (IBM is larger than MS). 2) .NET locks you into MS Windows. 3) if you live in a country where the insanity of "software patents" is enforced (or may be one day), then using .NET technology could expose you to patent lawsuits in the future, should MS decide to change the licensing terms and shake you down. 4) did I mention that .NET restricts you to MS Windows? No Linux, Unix or Mac options for you! Bear in mind that Windows is proprietary, closed, expensive, insecure, makes a lousy server, and makes a lousy desktop too, come to that. 5) MS may one day abandon .NET in the same way that they have killed VB6. Java is free and open source, and therefore is safe.

Microsoft bribes schools colleges and universities into teaching shoddy proprietary products like Windows and .NET so that the next generation of students will become addicted to and buy (personally or on behalf of an employer) Microsoft products. That is flagrant Corruption! Schools and Universities have a moral duty to teach kids free languages such as C++ and Java on free platforms such as Linux or BSD, using open standards, such as Open Document Format (ODF). There are no licence fees for any of those! Many schools/colleges/universities are acting as a marketing and training branch of Microsoft. Is your Uni a whore? If so, write and complain. Prefer to study at a competent, moral University. In general, boycott MS wherever possible or you are personally supporting corruption. Do the right thing, as they say.

More Advanced Java development - frameworks

In the next two pages various ideas on Java frameworks and tools are presented, based on personal experience, programming Java systems is how I make a living these days. What's the difference between a common and garden Java library and a framework? There is some overlap between the two but a normal library is designed to do a specific narrow job, such as parse XML (e.g. Xerces). You tend to call a library, and most of the time the library doesn't call you back (with some exceptions, like SAX). Whereas a framework is generally broader in approach, providing code to solve swathes of related problems, such as persisting data, or creating a user interface. Frameworks are more likely than normal libraries to fire "events" that you are expected to "handle" (which is a fancy way of saying that a framework loves to call your code).

About producing Java user interfaces, whether local applications or web apps: Java user interfaces and web apps

Advanced java development: Java on the server

See also a list of Java related links.



Add your comment to this page

add a talkback

no talkback comments yet
Add your comment to this page (it is a bit like adding a guestbook entry)

help: how to add your comment

Page hits: 191






body frame image body frame image
s


www.eadon.com home sweet home contents: more stuff next page


www.eadon.com