Hello Friends, Today we will be understanding and writing our first java code from scratch. We will go through the pre-requisites and later will go through the code.
The requirement for running Java.
For executing any Java program, the following steps must be followed.
Install the JDK if you don't have installed it, download the JDK and install it as per your operating system. Set path of the JDK/bin directory. You can also go through this detailed setup for java.
We won't be using any IDE like Eclipse or IntelliJ to keep it simple.
Writing Hello Java Example
Save the below code in the HelloWorld.java file.
class HelloWorld{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Compiling and running the code
To Compile the code execute.
javac HelloWorld.java
Once the file is complied execute the below command to run it.
java HelloWorld
Understanding the program
- class keyword is used to declare a class in Java. We have declared HelloWorld class with it.
- public keyword is an access modifier that represents visibility. It means it is visible to all. There are other modifiers like private and default as well.
- static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class.
- void is the return type of the method. It helps us to understand what is the expected output from the method after its execution flow. void means it will not return anything.
- main represents the starting point of the java program.
- String args[] is used for command-line arguments. It's used to pass values to your application when run via the command line.
- System.out.println() is used to print statement in java.
Thanks for reading. I hope this story was helpful. If you are interested, check out my other articles.
you can also visit shubhamdeshmukh.com