JaVa
   

HelloJavaWorld—A Simple Console Program

Well, you should have finished that cup of coffee by now, so let's get cracking with our first example program, HelloJavaWorld.java. You must first create a source file called HelloJavaWorld.java and then enter the following source code into it:

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


Now compile your source code by going to the command prompt and entering the following command (ensuring that you are in the same directory as your code):

C:\j2sdk1.4.0\bin\javac HelloJavaWorld.java


Note that you may need to change the C:\j2sdk1.4.0\bin part if you have installed Java to a directory other than the default suggested. Once you have compiled the source code without any errors, you will notice that a new file called HelloJavaWorld.class has been created in the source directory. This file is the program file that is used with the Java.exe interpreter to run your program. Here is the command that you need to execute the .class file that was created:

C:\j2sdk1.4.0\bin\java HelloJavaWorld


When you run this program, the words "Hello Java World" should be displayed in a console window. This can be seen in the following screen shot:

Java Click To expand
Screenshot-4:

All Java app programs begin executing code in the method called main, as shown in the HelloJavaWorld example. Inside the main method is a line of code that prints our chosen text to the screen. The program is basically a class containing one method, main, which is static and contains one line of code to print some text to the console window. To contain a block of code, you simply use curly brackets to begin and end the block. In this example, the first opened curly bracket and the last closed curly bracket specify the code segment for the class HelloJavaWorld, whereas the middle two curly brackets specify the code segment for the method main. Most of you know the phrase "what goes up must come down"; well there is another phrase, which is not so popular, that goes "for every opened curly bracket, there must be a closed curly bracket." Do not worry about the keyword public for the time being; this keyword concerns the control of attribute and method access, which is explained in more detail in , "Multiple Classes," where its usage becomes more topical along with the private and protected keywords and package level access. We will gradually learn about all of the bits of code that go into making this simple example as the tutorial progresses. It is important to realize that all aspects of the code are there for a reason, and all of these reasons will be explained one step at a time.

Printing Text to the Console Screen

In our first example, HelloJavaWorld.java, we used one line of code to print a text string to the console window.

System.out.println("Some text here");


The text string is entered between double quotation characters. The actual method that is invoked is println, which is a member of a static object called out, which in turn is a member of the class System. The System class provides facilities for standard input and output, among other things, and is included by default in all of your programs as part of the java.lang package. We will look in detail at packages in . Whenever we require output to the console window, we will use this command with which we can also print the value of variables. In this chapter, you will occasionally see variable values specified with a text string separated by the "+" operator as follows:

System.out.println("The value = " + value);


As well as being an arithmetic operator, which we will look at very soon, the + operator is also used in Java for string concatenation (joining a string onto the end of another one). The value of the variable value, in the case of the previous line of code, is converted by Java into a string value and appended to the end of the specified string. Do not concern yourself with this too much for the time being; we will discuss this in more detail in the next chapter when we start using strings.

Comments

Adding comments to your code is very important. Comments allow you to add sentences among your code that will be ignored by the compiler. This is important in many ways, like for setting reminders, reporting bug errors, and describing what the code actually does. You can add comments to your code using two basic methods: by line or by block. The following is an example of a comment in a line of code:

// none of my code is working, ARRRGGHH!!!


Any text entered on a line of code after the two forward slashes (//) is a comment and will not interfere with the functionality of your program. You can also use this method on the same line that you have code, but this type of comment must be entered to the right-hand side of any code.

Do some code; // this is on the right-hand side


The other method for adding comments to your code is to specify a block area. This is implemented by specifying the beginning and end of the comment, using /* to begin the block and */ to end it. The following are examples of using the comment blocks:

/* You can enter text information here */
/*
 This is some text to describe what my program does.
 You can use as many lines as the statements enclose.
*/
Some code /* they can be added between code */ More Code;
// Although this makes your code messy/less readable


JaVa
   
Comments