Writing Our First Program in Java - HELLO WORLD !
Before we can start writing our first lines of code we need to download and install an IDE that supports Java. For our tutorials I will be using the IntelliJ IDEA, which you can download from here. Once you have downloaded and installed the IDE we can begin.
First of all we need to create a new java project, which can be done by clicking on new project
Next window select java and select the JDK and move to the next step by clicking next. If you don't have any available JDKs then simply go to download JDK and download the Oracle Open JDK.
In the next step select create project from template and select command line app and click on next
You will be presented with a window with a few lines of code that looks as follows.
At first it can be intimidating, but fear not as we will going through everything slowly and understanding them as the tutorials proceed. Right now the only thing you need to worry about is the few lines of code that is right in-front of you.
Lets break down the code and see what it means.
package com.company;A can be seen above the very first line says "package com.company;", a package is used to group or organize related classes, for ease of understanding it can be thought of as a folder which contains images or text documents or related or same type of files. furthermore the "com.comapny" is the name of the package,
public class Main {
public static void main(String[] args) {
// write your code here
}
}
The second line indicates "public class Main", in java for anything to be executed it needs to be inside a class, this class is named "Main" as this is the main class and will be executed first. The word "public" indicates who can access this class, these keywords are called access modifiers, which we will discuss further in a later tutorial.
The next line "public static void main(String [] args)" is known as a method, a method resides within a class and this as the name specify is the Main method, which is the access point of our java program for it to be able to run, this is the first thing that will be executed. We'll talk in depth about methods later on.
ALRIGHT ! NOW FOR THE EXCITING STUFF !
public class Main {
public static void main(String[] args) {
System.out.print("Hello World!");
}
}
As you can see we've added the line of code, "System.out.print("Hello World!");"
where;
System is a predefined class in java provided for us which contains important methods and variable
out is a static variable which represents the output of the program
print is the method that can be used to print content
"Hello World!" this is text that we want to print, anything that we put inside the brackets within the double quotations will be printed as it is.
Once we have written our code we can simply Run it where it will compile and execute, to do this on IntelliJ IDEA we can click on the little green play button on the top or we can use the keyboard shortcut shift + F10. once the code complies and executes you will be presented with the results as follows;
Now try printing some more stuff !! Maybe you can print more than one line ? here's a hint println("");

Comments
Post a Comment