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