‘Hello World’ in Java & Comments


Anyone who starts to learn Java Programming Language, starts with a simple program. One of them is ‘Hello World’ in Java Programming Language.

According to Java, in order to start writing code in Java language, we just need to write one file, compile that, and it will run absolutely fine without any issues.

But Eclipse tells us to do these same activities little bit differently, in a more structured manner.

Eclipse suggests us to create proper folders and work within those folders. Eclipse wants that we create projects. And these projects can be for different kinds of topics. Within each project we try to maintain our code in proper manner.

New Java File

a. So, to write any type of code in Eclipse, the first step will be to create a new Java Project.

File -> New -> Java Project

245 - Creating a new Java Project

b. And here we will give our Project name. I am naming it Module1. Click on ‘Finish’ Button.

Entering a Project Name in Java Programming

c. A new window to create New module-info.java will appear. Click on ‘Don’t Create’ Button.

Do not create module-info.java

As soon as we do that, in our Package explorer in the left, we will see Module1 Project.

Package Explorer Java Module1

In case you cannot see the Package Explorer, Go to

Window menu -> Show View -> Package Explorer.

window - show view - package explorer

Now this Module1 is an empty project for us. Within this project there is this ‘src’ folder. All our codes that we write, will be inside this ‘src’ folder.

d. So, let’s create our first file in which we want to write Java Code.

Right click on ‘src’, go to ‘New’, and we will see a lot of options here.

Right click src and new

Now what we want is a new file inside which we want to write our Java Code. So, if we look at all the options here, the one option named ‘File’ looks like the perfect way to start as it indicates we want to create a new file.

Now, understand this that we do want to create a new file, but whenever we want to create a new Java file, we will actually choose a ‘Class’. So, instead of choosing a ‘File’, we will choose a ‘Class’.

Choose a Class Not a file

So, what a ‘Class’ is, and it’s properties, we will discuss these in detail later when we start studying Object Oriented Programming. For now, simply choose a ‘Class’.

e. A New Java Class window will appear. Give a name to the class.

Name a class and press Finish

I am naming it HelloWorld. Why HelloWorld?

Because the first program we want to create in Java is a very simple program which will just print Hello World to us. Nothing more. Simply we will run our program, and the computer will print ‘Hello World to the user.

So after giving a name to our Class, we will just press the ‘Finish’ button here.

Eclipse has created/will create HelloWorld.java for us. Notice that we never said to put .java here, we simply said HelloWorld.

HelloWorld.java file created with some code

So, Whenever we create a class with a filename, Eclipse automatically creates filename.java for us.

In the Code Editor, we can see it already has some code written for us within this file HelloWorld.java. We will use that code as it is.

Beginning to Code

In Flowcharts, we use START Block. The use of Start Block is – Whenever we look at any flowchart, Start Block is the point where we start reading that Flowchart. It is the right place from where we can begin looking at the flowchart.

Problem #1 End Block and Explanation Flowchart

Exactly like a Start Block, in Java too we need a start point. We need a start point in Java from where we can start our coding. So, a Start block in Java will be like –

public static void main(String args[]) {

}

It may seem a lot for just a Start Block. But do not worry about it. It is basically just a syntax, which we will understand very well as we start studying Functions later.

For now, just know that as the time comes we will get to know about many things and eventually everything will get clearer.

So, this statement – public static void main(String args[])’  represents our Start block. Any program we write in this file, our Java Compiler will start reading this file from this position (or Line no.) only.

Starting with public static void main (string args[]);

In the above image, the ‘public static void main(String args[])’ which is our Start Block, starts from line 3. If anything is written above this statement’s line, System will not start reading from those points. It will start to read the program from line no. 3 only, not any other lines above it.

Starting point only with main method and no other place above it

Now let’s say in the above image we shift this statement ‘public static void main(String args[])’ to line no. 10. If anything is written above 10th line, System will not start reading from those points. It will only start to read from line no. 10 and the code will start executing from here. The above lines will not get executed first, Line no. 10 will get executed first.

So here we will write our first program which is to print/display ‘Hello World’ to the user.

We are not going to write the whole logic that’s going on behind printing or displaying stuffs to the user. There is an inbuilt functionality which Java provides us, using which we can print or display things to the user.

And we are going to use this functionality to print/display anything to the user.

System.out.println();

Inside the brackets, we insert whatever we want to print. Let’s try this with ‘Hello World’.

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

So, this is how we write something we want to print. Let’s understand this “System.out.println();” a bit what’s going on here.

  • ‘System’ – This is some inbuilt file which we are using here.
  • ‘out’ – This is basically inside the ‘System’. This represents the output which we want to show as a result of our input to the system.
  • ‘print’ – This is inside the ‘out’ which basically represents that we want to print the output, which is inside the brackets, to show it to the user.
  • ‘ln’ – This tells system that after printing the output on the screen, press an enter key to go to the next line.
  • And lastly we have used a semi-colon(;) at the end here. This is the way of telling the system that our line is complete.

In English whenever we complete our sentences, we put a  dot( . or full-stop) at the end. Similarly, in Java whenever we complete our line of code, we put a semi-colon at the end. This tells the system that ‘We are done with that statement, please process it’.

So let’s run our program to see it’s working or not. To run this program, click on this green button that looks like a play button.

Green Color Play icon Run Button

As soon as we press this button, we will see something in our console area.

Hello World

Output of Hello World

Console area is at the mid-bottom section of the screen. Anything we want to print to the user, gets shown at the console. Basically, Console is a place where user will give us the Input, and we will give user the Output. It is a very important window.

If it’s not showing on your screen, go to

Window menu -> Show View -> Console

window - show view - console

This will get you a Console Window at the mid-bottom part of the eclipse screen.

Hello World Experiments

Let’s try some experiments with ‘Hello World’.

We want to print two ‘Hello World’s.

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

On running this code we get the following as output printed in Console –

Hello World

Hello World

So, we got the output as expected. Two ‘Hello World’s printed. Now here, the second ‘Hello World’ is getting printed at the next line, and not at the first line with the previous ‘Hello World’. We never pressed any Enter key anywhere in the code, Right?

This is because the statement System.out.println() contains ‘ln’ in it’s end. As stated above, this simply puts/presses an enter key at the end of it’s ouput statement and takes us to the next line.

We have a syntax in variation to this, which we can use to print statements in the same line and not go to the next line(or not press Enter key). And the syntax is –

System.out.print();

So, instead of println, we just include print. Let’s try including this syntax in our previous code –

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

And now when we run this code, we get the following as output –

Hello World

Hello WorldHello World

Let us break down the three print statements and try to understand better –

  • The first print statement contains println, so that will put a new line at the end after printing it’s statement, i.e, the first ‘Hello World’.
  • The second ‘Hello World’ will not put a new line after printing it’s statement, since the statement contains only print, and no ln with it.
  • And that’s why the third ‘Hello World’ got printed along with our second ‘Hello World’. But after printing our third ‘Hello World’, this statement will put a new line at the end as the third statement contains println.

So, we have got two options here – ‘System.out.println()’ and ‘System.out.print()’. The first will send us to the next line, and the second will let us stay on the same line.

Comments in Java

Now that we have written a simple program in Java, and along with that we have also learnt some basic concepts about differences between ‘System.out.print’ and ‘System.out.println’, we would like to add some text references in our program. These text will be like –

public class HelloWorld {
	public static void main(String args[]) {
		If we use println, Java will put a new line at the end.
		System.out.println("Hello World");
		System.out.print("Hello World");
		System.out.println("Hello World");
	}
}

At line no. 4, we have written some text references, so that at a later moment when we refer to this code, or maybe someone else sees this code, we/they can recall the difference between ‘println’ and ‘print’.

But here the problem is that Java will try to execute this line too. And we know that this line is not the exact language that Java understands, so this line is invalid in Java Programming Language.

We want to write things in our programs similar to the line no. 4 which is understandable by us, useful for us, but at the same time gets ignored by the Java compiler.

So, we will put two forward slashes (//) before the line we want to write. And then Java will basically ignore that line. We can see that line, but Java compiler cannot see that line and will move on to the next line.

public class HelloWorld {
	public static void main(String args[]) {
		//If we use println, Java will put a new line at the end.
		System.out.println("Hello World");
		System.out.print("Hello World");
		System.out.println("Hello World");
	}
}

These are called as Comments in Java Programming Language.

And Comments are very important. We will be adding lots of comments in our codes. This will make our codes easier for us or anybody to understand.

If we want to add comments in the form of paragraphs, that will take more than one line, adding two forward slashes (//) in front of every line will just be too lengthy process, and may also seem messy.

So, for that, what we do is,

  • on the line before the start of our comment paragraph, we put forward-slash followed by star (/*),
  • write our comments,
  • and on the next line of our last comment line, we put a star followed by a forward-slash (*/).
public class HelloWorld {
	public static void main(String args[]) {
		/*
		If we use println, Java will 
		put a new line at the end.
		
		If we use print, Java will 
		let us be on the same line.
		*/
		System.out.println("Hello World");
		System.out.print("Hello World");
		System.out.println("Hello World");
	}
}
  • Or, we can simply select all the lines we want to comment out, together with our mouse or keyboard, and press ‘ Ctrl Key + / ‘. This will also, comment out the selected lines. But here, before every line we will have two forward-slashes ‘//’. Benefit here is that, we can easily disable the commenting feature for any single line by pressing the same buttons ‘ Ctrl Key + / ‘.

Now we know how to write a simple program and print something in Java. We have also learnt about using comments in Java Programming Language.

You may like to Explore –

How is data stored in Java?

An overview of OOPS in Java Programming Language – 1

Cheers.

Happy Coding.

About the Author

This article was authored by Rawnak.

1 thought on “‘Hello World’ in Java & Comments”

Comments are closed.