Writing your first executable program in Java

Let's start with our basic coding in this section. If we want to print something in our output, there is a command in Java called System.out.println(). This command will print the output in the console. Let's say we would like to print hello world, and when we run the following code, hello world will be printed in our output console:

Firstclass.java

So let's run the code. There are two methods to run the code:

  • Right-click on the filename in the Project Explorer, click on Run As, and select Java Application.
  • Or, we could click on the run icon given in the toolbar and click on OK on the Save and Launch window. The icon looks like this:

This will run our code and print our output. The following screenshot shows the hello world message on our editor:

Output displaying hello world as per the code

In short, System.out.println() is used to print in our console. We will be using this in almost all our examples for our demonstration of practical examples. If we remove ln from the statement, it will not print the output in the next line.

Let's try printing a statement that will display the output of two print commands on the same line. Here, we add a System.out.println("hi") statement before the hello world statement. If we run the code, the output will be as follows:

Output is displayed on two separate lines

Observe how hi is displayed on one line and then hello world is displayed on the next line. Here, ln displays the output in the next line. If we remove ln from both the statements and run the code, the message will be displayed as follows:

Output is displayed on the same lines

We see, hihello world printed on the same line.

If we write our code, and then we want to check the output partially, we don't need to remove the line of code; all we need to do is just comment it out. We can comment it out by simply putting double slashes (//) at the beginning so that Java will not pick the line. This is shown in the following screenshot:

Commenting using double slashes

If you remove the slashes and the statement is just some random words, then it will throw an error. We will see a red underlined code. This means there is an error at the line with a cross mark. This is shown in the following screenshot:

Error is flagged with a cross mark besides the line number

Add the backslashes again to comment out the error.

Remember, here we are writing our actual code in the main block only. What if we want to print an integer?

Let's say we want to print the number 4. To print it, we first need to store it in a variable and then we will print the variable. So when we print the variable, the value presenting that variable will be printed automatically. For this example, we pick the number 4, and we assign the number in a variable called a. The problem here is that a does not know what data type is being assigned to it. So, we have to explicitly mention that a is an integer. If we do not mention that a is an integer, it throws an error.

In short, we are first creating a variable called a which only acts an integer and then places an integer value 4 into this. The following screenshot illustrates the example we are talking about:

Value 4 is assigned to the variable a

So, with this type of code, we can type it outside, but if we want to print it, we will have to type it in the main block. In this example, we want to print the value of a so we add another System.out.println(a) statement. The editor will throw an error for the variable a in the print statement. To know what the error is, we hover our mouse over the error and a pop up is displayed showing the error with a possible fix, as shown in the following screenshot:

Error details is displayed when the mouse is hovered over it

There will be an option to click on in the error detail. This will automatically resolve the error by adding the required content. This is an amazing feature that the editor has and it is very helpful as we move onto more complex examples.

In our example, when we click on Change 'a' to 'static' in the error detail pop up, static is added to the variable a and we are able to run the code. On running the code, the console will look like this:

Output displaying the value of a as per code

We will be getting into the details of what exactly static is in the later chapters

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.193.232