This blog, beyond the basic concepts, is primarily a C# programming blog. Nonetheless, I figured it would be nice to show examples of a simple “Hello, World!” application in four different languages… and then break each line down. The four languages I’ll be covering are:

  • Java
  • C#
  • PowerShell
  • Visual Basic

So what makes the simplest application tick? In the case of Java, here’s example source code for a simple program. All it does is print “Hello, World!” and then exits.

When we run the program, we get this:
helloworld-java

Now, let’s break each line down.

This line defines a class named HelloWorld. In Java, a class is a blueprint of an object. For example, you could have a class called Microwave. There are many manufacturers of microwaves, but let’s assume we’re talking about one particular model. Because each microwave was built from the same engineering diagrams, you can expect it to contain the same components each time you create a new one. In this case, we’re only going to be creating one HelloWorld object – the one that gets created automatically when the program is run. Future posts will talk about nifty things you can do with classes, such as inheriting and abstracting, but for now, let’s move onto the next line.

This line is pretty much standard Java convention for “Here’s where the entry point of the program will be.” It begins with public, which essentially means that the method, which is called main, can be accessed from anywhere in the application that creates an instance of a HelloWorld object. In java, a static method is a method that you can call without first instantiating the object. For example, your Microwave class might have a convertLitresToGallons method, which would be static, because you may want to use it without first setting up your Microwave object. It might also have a beginCooking method, which would require the microwave to be ready for use.

The String[] args defines an array – of type “String” – which is automatically populated with your command-line arguments. Those of you who use command prompts, or did in a distant past when DOS ruled the computing world, may recall that you can add switches to a command to modify its behavior. For example, while “dir” would give you a list of files in the directory at a DOS Prompt, “dir /a” would list files including hidden ones. Java acts the same way – you can pass parameters to your programs, which will then get populated into the args String array.

The first part of this line, System.out.println, calls a system function called println, which outputs a line of text to the standard output stream. In this case, it outputs “Hello, World!” to the screen. Every functional line in a Java program ends with a semicolon, hence the ; after the function call.

At the right of the line, you see  // Print to the screen.  This is called a comment, as mentioned in Basic Concepts – Part 3. It does not affect the functionality of the program… only the functionality of the programmer.

The last two lines consist of a sole } character, which closes up the two { characters we see above us. These are called blocks of code – anything between a { and } is grouped both functionally and aesthetically.

There are always several ways to create a program, but it’s best to stick to convention when possible. This gives your program a competitive advantage in the maintenance world, since people can easily pick up on what you are trying to accomplish. This is particularly true when a code maintainer has seen a block of code several hundred times before, and recognizes what it does in less than a second – now that’s efficient!

Share

Comments are closed.

Post Navigation