Hello, World!

Goal

  • Writing and executing your first program.
  • Outputting text to the screen.
  • Reading text from the keyboard.
  • Learning the structure of a Java program.

Lesson

Writing to the Console

It has become a tradition for programmers learning a new programming language to first learn how to write a program known as “Hello, world!” (named so because it teaches the programmer how to write text output to the screen that says hello world!). We follow this tradition because it teaches a valuable Java feature that you will use repeatedly in future lessons.

The “Hello, world!” program for Java is written in the listing below. You should type this code (not copy/paste) into your IDE and try to run it for yourself.

When you run this program in Eclipse, you should see the output shown in Figure XYZ.

 

The program consists of two JavaDoc comments (shown in orange), a class definition (lines 8 – 16) and a method definition (lines 13 – 15).

JavaDoc comments are ignored by the compiler and are explanatory remarks intended to be read only by the programmer. The comment from lines 8 – 16 documents the “class” named Application (we explain what a class is in a moment). JavaDoc comments begin with the characters “/**” and end with the characters “*/”. The first line of a JavaDoc comment is usually a brief summary describing the class or method. More detailed information can be given by in separate paragraphs that follow the summary. The class comment in this example includes two tags: @author and @version. The @author tag is used to document who wrote the class. If a class has more than one author (for example, a second person made additional changes to a class sometime after the first author was finished with the class) then you can include additional @author tags on separate lines. The @version tag is used to mark the version of the class. We will not be using the @version tag in future programs. Professional software developers use versioning tools in practice that can keep track of multiple versions of a file so there is not much need for the @version tag in real world code. There are other tags that can appear in JavaDoc comments and we will cover some of those in later lessons. Lines 10 – 12 are JavaDoc comments for the “method” named main. JavaDoc comments on methods provide information to developers about what types of values are required by the method (if any) and what is the type of the value returned by the  method (if any!). The JavaDoc tags @param and @return are used for denoting this. We will encounter these in later lessons.

On line 8 we begin to define a new class named Application. The entire definition is given between an opening brace “{” and a closing brace “}”. The definition of Application in this example consists of a single method named main. For now let us not dwell on the meaning of a class but it will become obvious later we begin to write object-oriented programs. It suffices to say for now that a class is like a blueprint that describes how something can be built.

On line 13 we define  a method named main. This method is special because it is where our program will start to execute. The Java runtime environment knows it is special because it is named “main” and because it accepts multiple String values. The words “public”, “static” and “void” are special Java keywords. They will be explained in better detail in later lessons. For now though when you want to write a program you can copy the structure of this application and place your starting code inside main.

On line 14, we invoke the “println” method that is made available to us by the System class using an object called “out”. We said that a class is like a blueprint so if we continue the analogy then an “object” is just something we built using that blueprint. Do not worry about these details for now. When we call the println method we provide it with an value (or argument) of “Hello, world!”. In a Java program when we write some piece of text within quotation marks then that becomes known as a “String literal”. When this program is run, a call will be made to the println method which is part of the standard Java library. The println method will write the provided argument to the screen.

Reading from the Keyboard

The most useful computer programs are interactive, which means that as well as writing output to the screen the program must be capable of reading input from a user as well. We now modify the program given above to read a name that will be typed on the keyboard. The value that is entered into the program will be echoed back on the console with the word “Hello” written before.

 

The overall structure of this program remains the same: we have two JavaDoc comments that have been edited to reflect the new purpose of the program and we have changed the code inside the main method to allow our program to read a line of text from the keyboard and write the value back to the screen.

On line 13, we write text to the console screen that prompts the user to enter their name. We have already seen how to do this in the previous version of this program but there is subtle difference with how we are doing it this time. In our previous application we invoked the “println” method which automatically moves the cursor for the console onto the next line after it has displayed the provided message. But this time we are calling the “print” method. It also displays messages onto the console window but it does not move the cursor on to the next line. We do this here because when the program runs we want the user to enter their name next to display prompt (although it would work equally as well if they had to type their name on a new line…it is a matter of taste!).

On line 15 we declare a new variable named keyboard that has the type Scanner. A variable represents space in the computers memory that can hold particular types of things. This variable is for storing things that are Scanners. A variable declared inside a method initially stores nothing. We can assign something to a variable by using the assignment operator “=”. Here we assign the keyboard variable with a new Scanner object. That object is constructed by invoking a special method that is called a constructor (that has the same name as the type of object we want to create). We must provide the “in” object to the Scanner constructor if we want to read from the keyboard.

On line 16, we declare another variable that we have called “name”. We assign it a value that must be entered at the keyboard (Java will know the user has finished entering their name when the user presses the enter key which indicates the end of line). Until this program terminates (or until we were to assign a new value to name) the variable name will contain the characters that the user typed at the keyboard.

Line 17 contains another println call, which will print a message to the screen followed by a new line. The argument, or value, that we pass to println is one String that is formed by “adding up” or “concatenating” three smaller String objects. The “+” symbol represents String concatenation as well as numerical addition that we will see in later lessons.

It should be noted that there is nothing special about using the names Application, keyboard or name in this program. We could easily have called our class something other than Application and the program would still compile and execute correctly. Similarly, the variables keyboard and name could be changed to something else and as long as we updated their usages in all places in the program then the program will compile and execute correctly.

Try it Yourself

  1. Modify the program so that Scanner variable uses the name “kb” instead of keyboard.
  2. Enhance the program so that it asks the user to enter their last name as well as their first name and stores their surname in a variable named surname. The final output of the program should print their surname as well as their first name using this code: System.out.println(“Hello ” + name + ” ” + surname + “!”);
  3. When you complete exercise 2 try to enter numerical values for your first and last name. For example if you enter the value 1 for your first name and 2 for your surname the program should print out Hello 1 2!. Do you understand why it does not print out Hello 3!? Hint: The type of name and surname is String, Strings are concatenated by writing them side-by-side. To do numerical addition then the types of those variables would need to be something else. You will learn more about this later.

Summary

  • A String literal is a series of characters enclosed within quotation marks, for example: “Hello”
  • You can write content to the screen using System.out.println(“What I want to say…”)
  • Statements in Java end with a semi-colon (“;”) just like sentences in English end with a full-stop/period (“.”).
[Total: 0    Average: 0/5]

Be the first to comment

Leave a Reply

Your email address will not be published.


*