Few technologies have made an impact on the computer and information technology industries like Java. It has its critics and even its most ardent supporters readily sight its flaws. But Java remains the most diverse programming language today and the one Proloquor.net uses almost exclusively. Here we’ll go over the basics, point out some useful resources, and set you up for more detailed tutorials on the Java components we use.
Java can trace its heritage back to 1991 at Sun Microsytems (now part of Oracle) when 3 engineers were infamously tasked to ‘build something cool’. Their idea was a Write Once, Run Anywhere (WORA) programming language that can support a wide array of processing platforms, from small 8-bit embedded machines to large Internet servers and mainframes. Their vision was to create a ubiquitous computing platform where everything from toasters to nuclear reactors could leverage a single software environment. In a sense, Java was a precursor to the notion of an Internet of Things (IoT) today.
Over the last two decades, Java’s features have expanded considerably. It’s Standard Edition (SE) has gone through several major revisions. At the time of this writing, Java SE 8 is the most current version for Windows and the one we’ll focus on for this on subsequent posts.
Java was created with some basic goals in mind.
- Java is Object Oriented. OO allows you to arrange your source code as a collection of things, or objects, rather than an endless string of procedures like traditional languages.
- Java is Platform-Independent. All high-level programming languages claim their source code is portable across different platforms and compilers. Java is an interpreted language in that a separate virtual machine is required to run programs, not just the platform itself. This unique runtime environment separates the platform particulars from the running program.
- Java is Secure. Well, it makes a good attempt. the virtual machine can enforce policies that limit disk access and other questionable activities.
- Java is High-Performance. There we may have a problem. Interpreted languages like Java need to compile every line of code immediately before execution. This has a significant impact on performance. While the runtime has been significantly optimized over the years, Java is generally not considered a good environment for intensive numerical calculations.
There are two places to go to get started with Java, depend on whether your a user or a developer.
Getting the Runtime
If you’re interested in simply running java programs, including ‘applets’ in a browser, start at java.com. This site has basically one purpose, to download an installer program for the Java Runtime Environment (JRE) for your platform. The site can detect the type of your platform (Windows, Mac, etc.), but you’ll want to check you get the right one. Run the installer after it’s downloaded and you’re done. Take a look in a folder like C:\Program Files\Java\jdk1.8.0_11 to see what was installed.
Getting the Development Kit
The JRE will let you run Java programs, but you want to create them. For that you need to install the Java Development Kit (JDK), from java.oracle.com. Navigate to the download section and download the latest Java SE JDK. The JDK contains everything the JRE does, but includes all the source libraries that come with Java as well as tools to compile, debug, deploy, and even document your Java programs. As with the JRE, run the installer program and accept the defaults.
Your First Program
Once you install the JDK, you can begin writing programs. Let’s start with a classic one. Begin by opening a text editor like Notepad or, better yet, Notepad++. Add the following contents to c:\Example\net\proloquor\Greetings.java.
There are plenty of resources to learn the features and syntax of the language; the first stop might be Oracle’s own tutorials. For now however, let’s discuss a few key points.
- All code is within classes. Classes represent something in the real world, although possibly abstract, and are named with a noun.
- Classes include code that describes what they are (properties, like message) and what they can do (methods, like sayHello()), just like their real-world counterparts.
- Classes also include constructors that provide an opportunity to initialize an object of the class when it comes into existence. Unlike C++ and other unmanaged OO languages, deconstructors are supported, but rarely used.
- Classes can have a special ‘main’ method that first runs when the class is executed. In our example, main() creates an instance, or object, of the parent class, then calls a method of the class.
- Finally, your source files should be arranged in a directory hierarchy that matches the namespace of your packages. In this case, the net.proloquor.Greetings class file, like it’s corresponding java source file, should be in c:\Example\net\proloquor.
To compile and run our example program:
The Java compiler in the JDK, javac, is run against the Greetings.java source code and, if successful, produces a Greetings.class file in the same directory. When running the program, you may use the CLASSPATH environment variable to tell the runtime where to look for class files. To run the program, invoke the java program from the JRE with the fully qualified namespace of the class (without the .class suffix).
We’re off and running with Java, but there’s a lot more to learn. We’ll see a lot more of Java in action, both by itself and within the Eclipse IDE. In the mean time, there’s no better place to start than Oracle’s tutorials.