Logout

Java Building Blocks 1: Indispensible "Hello World" Java

So here it is, the classic of classic programs:

public class HelloWorld{

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

}

class

A class is the basic grouping of code in Java. To define a class we make a class block, which consists of a header and a block. The block is the code contained in the braces { } which follow the heading.

The heading consists of three parts, the public accessibility - and it has to be public, the keyword class, and the name we are choosing for our class.

Two things to note: there is no type, like int or void, in a class definition. And the class name is to be capitalized by convention.

public static void main(String[] args)

Every class which can be run must have a main method. Some classes are merely used by other classes, and so don't need an executable main method. But ultimately, all programs have at lease one main method which drives the program.

pubilc - the accessibility of the main method must be public, since it is accessed and run by "something outside of itself".

static - static is a modifier which stipulates that there can be one and only one instance of something. And it makes sense that there can only be one main method in memory at one time; think of running any application, like Microsoft Word; there is only one instance of Microsoft Word running at one time.

An important implication of this, though, is that any methods being called from the main method must also be static. The same is true of any variables used within main. (Though if we make instances of other classes, they need not be static, since they themselves will be different instances.)

void - the main method of a Java application is void of a return value. This is actually not the case for all programming lanugauges.

main - this is the name, in fact the only name, that a main Java method can have. Computers using the Java application will look for a method with the exact name "main" to run when it launches the application.

(String [] args) - this is the parameter list for the main method. In fact there is only one parameter, and it is an array of Strings, which by convention we call either arg or args. The args array makes a simple and convenient way for the programmer to take in data which will alter the way that the main method executes. See the next section for how to use the args array with Netbeans.

User Interaction With Console Input

There are various ways to get user input into our programs when using the Netbeans IDE (Integrated Development Environment). Actual GUI applications will use things such as textBoxes and drop down menus. And we can use the BufferedReader class methods to allow users running programs from within Netbeans to type in information right in the output window when prompted.

But there is a more basic way to set up a Java program to use "user" input, and this is to stipulate what the args array is, which the main method takes in as a parameter. Though not often done, since there are GUI elements etc. to do so in a more dynamic way, it would actually be possible to make an executable program which has certain values stipulated in the args array. But for us, it's just a simple way to take advantage of before learning BuffereReader and GUI techniques.

Never-the-less, this is how you do it in Netbeans 6.9.1. You right-click on the project folder, select Run, and then type a list of parameters in the Arguments field. And then within the main method any reference to args[0]etc. will access that particular element which you had set.

System.out.println()

println() is a method which prints text input within the two parentheses ( ) and then goes to the next line. The println() method belongs to the pre-made object of the System class called out.

println() should not be confused with print() which prints out the parameter without then doing the return to a new line.