Intro to Memtech: Java - Deconstructing Hello World

Last time we took a rapid fire tour of the basics of Java syntax. Somehow this still doesn't explain everything you find in the simplest "Hello, World" Java app.  In this post we'll try to make sense of the following code.

public class Example
{
  public static void main(String args[])
  {
    System.out.println("Hello, World");
  }
}

We already know that 'System.out.println("Hello, World");' prints to Console output, but how and why are still beyond us. First let's take a look at the first line. Java is an object oriented language, and 'public class Example' is defining an object type Example. It is public, so any code can access it. 'public static void main(String args[])' should look very familiar to a C++ programmer. This line defines a function that is called when this file is called from the command line. The 'String args[]' is an array of the command line arguments. 'Static' means the function can be called without creating an instance of the Example object; 'void' means it doesn't return anything. 'main' is the special sauce that gets us that command line behavior.

Additional object oriented concepts such as inheritance and polymorphism are well supported in Java. Check the directory InheritanceAndPolymorphism in the code sample link below for examples.

I was lucky enough to be taught Java as part of my undergraduate college experience. I don't say that sarcastically, Java is a wonderful language despite what you might hear online. Every language is designed in a series of trade offs to fit the use cases it might find itself in. As we've already discussed, Java has a very wide selection of use cases in large part to the JVM. The JVM somewhat muddies the waters between traditional compiled languages vs. interpreted ones, even more so because of JIT or Just in Time compilation. You compile Java code ( .java files) once to Java bytecode (marked as .class files). Then the JVM can JIT compile or interpret the bytecode. There is also software which will perform native compilation, AOT or Ahead of Time compilation (which I'd call traditional compilation).

The JVM was a bigger accomplishment than the Java syntax James Goslings created at Sun Microsystems in 1995. It has created a whole ecosystem of languages from dynamic Groovy to functional Scala. Java is a great way to introduce yourself to that platform and to explore the options it opens to you.

All of this was written in anticipation of the "Intro to Memtech: Java" talk that I gave last Monday. The talk went well and I'm happy to share my code samples and slides from the talk.

Code: https://github.com/thomaslangston/IntroToMemtechJava

Slides: http://www.slideshare.net/thomaslangston/intro-to-memtech-java

I also used the fantastic code samples from Ben Von Handorf's DevLink 2011 Mobile Smackdown presentation. Details on the presentation and a link to his code samples can be found on his blog post linked below.
http://www.benvonhandorf.com/blog/post/Devlink-Mobile-Smackdowne28093Code-and-Retrospective.aspx

Intro to Memtech: Java - Java Syntax

In the last post, we setup an Android development environment. We also setup our first Android app in "Hello, World" in cut and paste fashion.  That's all fine and good if you already have a good handle on Java syntax, but what if you're still in the dark on that subject? In this post, we'll cover the basics of Java syntax.

//this is a comment
int i;  //this is a primitive integer variable declaration
i = 2 + 2; //this is an assignment statement

Java has a C-style syntax, so chances are it already looks pretty familiar. It is a statically typed language and uses infix notation for most operations. Semicolons delimit the end of statements, so it is possible to stack multiple operations on one line, but not usually recommended.

String s = "This string is immutable."; //Strings are objects, not primitives
String s = "This assignment creates a new string." 
//The old string is garbage collected by the JVM

Java is an object oriented language.  Two other important features are that Strings are immutable and unreferenced objects are automatically deleted by the Garbage Collector.  Garbage collection can be expensive, especially in real-time applications.  So performance can take a dive on something something like:

String s = "";
/*This "for loop" repeats this block of code 99 times
  It subtracts 1 from the initial value of 99 assigned to 'i' on each pass.
  It exits when the boolean operation 'i  > 0' equates to false */
for(int i = 99; i  > 0;  i--)
{
  //The '+' operator appends strings.
  s = s + i + " bottles of beer on the wall. " + i + " bottles of beer. \n";
  //The primitive i is automatically converted to a string.
  s = s + " Take one down, pass it around. " + i + " bottles of beer on the wall. \n";
  //Notice the '\n' end line character
}

Each time we make an assignment to 's' we create a new unreferenced string which must be garbage collected. Java does have a few primitive types, such as integers. Luckily, primitives each have a Wrapper class that allows you to use it when you need it to act like an object. In the last example we get the 'i.toString()' method called each time we concatenate 'i' with a string. Some additional syntactic sugar known as auto-boxing makes other conversions invisible.

int primitiveInt = 42;
Integer integerObject = new Integer(13); 
//Create a new Integer object that wraps a value of 13
integerObject = primitiveInt;
System.out.println(integerObject);  //prints 42 in the console

If you've noticed we also snuck in Console output, a control structure (the for loop), and a boolean operation ('i > 0' or 'i' is greater than zero).  Chances are that all looks familiar and you had no trouble reading it, even if you don't 'know' Java. In the next post I'll go into how to get a running program with these types of statements, what the 'System.out' means in the last example, and an explanation of packages, classes, and methods.

Intro to Memtech: Java - Setting up your Android environment

Last time we went over the spectrum of use cases for Java as well as took a glance at the variety of web frameworks available for the JVM. That's great for self-exploration, but what else do I have to show you about Java?

While web application frameworks obviously have a lot to say (in a lot of different ways), Java's role in the mobile space (in my opinion) is more compelling. While Java has been in the mobile space for a while, the popularization of the Android OS by Google has really changed the landscape. Android runs a Java compatible virtual machine called Dalvik. If you've been following along so far in this post series, you already have 2 of the 3 downloads needed to develop on Android, namely the JDK and Eclipse.

Go to developer.android.com and download the Android SDK or Software Development Kit. Windows gets a nifty installer which fires off a separate download for the Android SDK Tools. On other systems you'll need to refer to the documentation to fire off that download from the Android SDK and AVD Manager utility. Refer to the documentation for the Android SDK setup while those download to get your bearings. The next step is to get the Android Development Tool or ADT Plugin for Eclipse (remember that readily supported plugin architecture?).

You can follow the directions linked above but note that there is currently a bug in the ADT plugin that causes the emulator to fail to launch if the SDK location contains any spaces (by default in Windows 7 it will be in the "Program Files" directory). When firing up Eclipse you can use the default workspace. After completing the ADT Plugin installation, you'll still need to setup an AVD or Android Virtual Device, a kind of system image for the emulator. You can reach the Android SDK and AVD Manager utility now from your OS (Start menu in Windows 7) or from within Eclipse under "Window -> Android SDK and AVD Manager". Follow the directions in the last link, but I suggest you use SDK 2.1 as that will let you reach a high percentage of devices (unless you're making an app for a tablet, for which I suggest the 3.x branch).

The instructions for setting up an AVD end by landing you squarely in midst of a Android "Hello,World" style example, which I suggest you finish to test your setup. When you finish you should be able to create a new Android project in Eclipse and successfully get the "Hello, World" app to run within the emulator. In my next post, I'll start digging into the syntax of Java so you can make sense of the app you've brought to life.

Intro to Memtech: Java - Use cases and Web Frameworks

If you've already read my last post, you're already setup with a Java development environment.  Hopefully, you've take the time to explore a bit on your own.  In this post I'll talk briefly about where you could expect to use Java as well as what choices you have in terms of web application frameworks for the JVM.

You can do anything with Java, really nearly anything. You can make a game or embed software in device. At my day job, we write Java that controls the HVAC systems of our clients via the Niagara framework. Java also is used extensively for server side back-end programming for sites such as Twitter. Java has had a role in mobile applications for a long time, most recently in its role with Android (more on that later). You can make a web application with it, either via applets (not exactly modern, but not useless) or using a web application framework.

I don't claim to have deep insight into selecting a Java web framework, but out of the choices there are four I'll mention.  JSF or Java Server Faces, Apache Struts, and Spring appear from a cursory search to be the most broadly used, although the error bars on my meta-survey are pretty wide. The dark horse in this race is a cute framework named Play. All of these purport to be MVC style web frameworks, although many others are closer to adapted desktop or MVVM style application development. My ill informed suggestion is to check out Spring and Play first, based purely on watching a single presentation on the former and a couple of articles on the later.

Alternatively, the JVM supports a number of other languages, complete with their own web frameworks such as Groovy on Grails or Scala using Lift.  Clojure, jRuby, and Jython also have associated web frameworks. Each language/framework combination is going to express a different opinion on software development. Correspondingly, some of these combinations are going to be much better or worse depending on your needs and style (e.g. enterprise vs. startup, dynamic vs. functional) so make sure to take your time evaluating at least a few of the major contenders before investing a lot of time in any.

Hopefully you've now got an idea of where you should be looking for tools and ideas for future projects.  In my next post I'll be taking a look at setting up an Android development environment in preparation for our code sample.

Intro to Memtech: Java - Setting up your Java environment

I'll be presenting an introductory Java talk at LaunchMemphis in just a few short days.  I've been working hard to get prepared, including updating my Java development environment.  I don't plan on spending too much time on it during my presentation, so this post will be a little more in depth explanation of what I've got going on.

For all the downloads listed I used the x86/32bit versions.  I tried an all 64bit environment, but it wasn't stable.  I'm not certain which download was responsible for this instability. Your mileage may vary.

The first thing to know about developing for Java is that you need Java installed.  Most computers already have Java installed, but the Java you know and love (or love to hate) is actually the JRE or Java Runtime Environment.  This includes the JVM or Java Virtual Machine, libraries, and other components needed to run Java applications. But you already run Java applications just fine, you want to develop them too. For that you'll need the JDK or Java Development Kit. Info on the Java 7 JDK can be found at http://jdk7.java.net/.  From there you can download the Java 7 SDK from  as well as browse the API documentation produced from the javadoc tool. On Windows an automated installer with default options will get your JDK ready. I do suggest uninstalling any previous versions of the JRE (e.g. Java 6 or earlier).

While it is completely possible to develop in Java with nothing but a text editor and a command line, I don't recommend it.  The de facto standard of Java development is the Eclipse IDE or Integrated Development Environment. Eclipse gives you all the breadth, depth, and weight of an open source IDE. It also gives you all of the negative characteristics you could infer from that description as well. Eclipse is big, slow, and a grab bag of features. It is built extensively around a plugin model that allows it to be used with a variety of languages, in a variety of contexts. But that diversity and malleability comes at the cost of a unified user experience and speed.

Because of the breadth of usage and plugin model, you're probably going to bump into Eclipse anyway. New technologies often have Eclipse plugin support before other IDEs. Once you've been in the Java space for a while, be sure to try out Netbeans and IntelliJ. Both have cleaner, snappier UI but lack some of the flexibility of Eclipse. Getting back to Eclipse, you're going to get a lot of options when you go to download Eclipse. I suggest you pick Eclipse Classic. Eclipse comes in a lovely archived folder which can run from anywhere, but I suggest you don't put it in the program files directory. Windows 7 at least doth protest too much. It is quiet as a lamb if you extract it to the root of the C:\ drive.  Dig into the folder and pin it to your taskbar, or make a shortcut to access Eclipse more easily.

If you've followed along so far, you should have your Java development environment setup and Eclipse installed.  Congrats, you have all you need to start exploring Java. In my next post I'll talk about the areas where Java is used and the web frameworks available for the JVM.