“Hello Java”, the First Encounter - Java Bootcamp: Learn the Basics of Java Programming in 2 Weeks (2014)

Java Bootcamp: Learn the Basics of Java Programming in 2 Weeks (2014)

Chapter 2. Welcome to the World of Java Programming

The “Hello” program is the first lesson that beginners at programming learn, and for so many reasons. The program introduces you to the first things that you need to do first to begin coding your program as well as some of the syntax that you need to learn and use.

For the samples, we will use the online coding (the link was provided earlier) for uniformity. You can use your own Java.exe later. The actual online Java console presents different font colors. We will only use different color fonts in our sample if there is certain command or character / symbol that must be explained in details.

Hello Java, the Basic Syntax

Take a look at the sample program below and we will analyze is line by line:

1

public class FirstProgram {

2

3

/* This is the first sample.

4

* We will print 'Hello Java' as the output

5

*/

6

7

public static void main(String []args) {

8

System.out.println("Hello Java"); // prints Hello Java

9

}

10

}

By the way, it is extremely important to keep in mind that Java is case sensitive. The identifier ‘Hello’ and ‘hello’ are not the same when it comes to Java programming. Make sure to define everything correctly and use the identifiers in their right cases consistently throughout the program.

The first line bears the class name ‘FirstProgram’; do not put spaces in between the words that make up the class name. The first letter of the next word, that’s included in the class name, should be in upper case. Your program should always begin with ‘public class’ (all in lower case) followed by the class name. Do you see the yellow bracket at the end of the class name? Don’t forget to include it as well. It serves as a mark that the things after it are the code of your program.

Your program file name should bear the same name that you assigned as class name. When saving the file name, just add ‘.java’ after the file name. Take a look at the example above. When you save it, write ‘FirstProgram.java’.

The second line is empty to make the codes more readable.

The third line starts with ‘/*’, and on the fifth line you have ‘*/’. You use them when you need to include a multi-line comment, which the compiler will not treat as part of the program. You usually include comments to explain a little about the program, a sort of reminder to yourself or to help the programmer assigned to maintain the program.

The seventh line contains the method name, and all the method names should be written in lower case. The method name is always written like this:

public static void main(String []args) {

The bracket at the end of the method name prompts the program to do everything enclosed within the brackets (in our example the closing bracket is found in line 9).

In the example, line 8 contains a command that tells the program to print ‘Hello Java’ on screen. There should be a semi colon (;) at the end of the command sentence. The ‘//’ symbol is another comment, and it is the symbol to use if you only need to make a single line comment.

The ‘.println’ is the command that tells the program to print the ones inside the parentheses.

Make sure that each beginning bracket has a closing bracket. In the example, the beginning bracket in line 1 has closing bracket in line 10. The beginning bracket in line 7 has closing bracket in line 9.

Here’s another:

1

public class FirstName {

2

public static void main(String []args) {

3

System.out.println("Erika");

4

}

5

It will yield this result:

Erika

Also, make sure to indent properly to make the codes more readable.

You will learn more about the data types and variables in the next chapter.