Installation Guide
First thing we need to download to write java code is JDK(Java Development kit) which has all the required libraries and development tools.
So go to browser and search for JDK and download the latest version.
Then we need an environment to write code in a specific language. Just as we need Microsoft Word to write documents, we need an IDE(Integrated Development Environment) to write java program. My suggestion is to use IntelliJ Idea to write java code as a beginner.
Go to browser, search for IntelliJ Idea and download the community edition.
Open IntelliJ Idea and click New project. Give you project a name and leave the other settings as is. Then click create to create a new project.
Congratulation you are now ready to write some java code.
Hello World
If you have created a project in IntelliJ Idea, you will already see a code written like this.
class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
If you see an empty page go write this code and hit run. If you can see a console which prints the sentence 'Hello, World!' then congrats you have executed your first code in java.
Now let us move on to the code.
Firstly to execute our java code we need a class definition and inside a class definition we need to create our main method.
class Main {
public static void main(String[] args) {.....}
}
Here class Main{}
is the class and public static void main(String[] args){}
is the main method. Java compiler always executes the code inside the main method. So it is necessary to always create a class and inside a class create a main method. As this is the beginning of Java code, we will only focus on the code we write inside the main method between the curly braces. We will learn more about classes and methods later on.
Now the code System.out.println();
is a built in java function which shows us the element in the console which we pass inside the parenthesis. In this case we pass a String "Hello, World!"
inside the function like this System.out.println("Hello, World!");
.
String is a Data Type defined inside double quotation mark. We write anything inside double quotes is a string. If we write "My Name is Nihal"
, this will also be a string. And if we pass the string inside the print function, it will print the exact sentence in the console. Try to write your name as a string and print it in your console.
We will learn more about Data Types in the chapter (Data Types).
Things to Remember
- Always create a class and a main method before writing your code.
- Always put a semi colon after writing a line of code. We need semi colon to let the compiler know that we have ended our line of code.
- class name can be anything but it needs to match the filename.
class Main{}
Here the class name is Main and the filename should also be Main.java.
Top comments (0)