Tools Required:
JDK Installation: Download the JDK 8 software from
https://www.oracle.com/java/technologies/javase-downloads.html
Once the Java JDK 8 download is complete, run the .exe file for installing JDK. Click on ‘Next’, ‘Next’ and ‘Close’ to complete Installation.
The PATH variable gives the location of executables like javac, java etc using which we compile and run the program.
Assuming you have installed Java in ‘C:\Program Files\Java\jdk1.8.0_291\bin’. Right-click on ‘My Computer’ and select ‘Properties’, click on ‘advanced system settings’, click the ‘Environment variables’ button and alter the ‘Path’ variable so that it also contains the path to the Java executable files.
Open cmd and execute the following commands
java -version
javac -version
Let’s create a Simple Hello world Java program:
HelloWorld.java
class HelloWorld{ public static void main(String args[]){ System.out.println("Hello world"); } }
classkeyword is used to declare a class in Java.
publickeyword is an access modifier that represents visibility. It means it is visible to all.
static is a keyword. If we declare any method as static, there is no need to create an object to invoke that method. The main() method is executed by the JVM, so it doesn’t require creating an object to invoke the main() method.
void is the return type of the method. It means it doesn’t return any value. main represents the starting point of the program.
String[] args or String args[] is used for command line argument. We will discuss it in coming section.
System.out.println() is used to print statement on the console.
Open cmd in the current directory and type javac HelloWorld.java
To execute:
java HelloWorld
About Java programs, it is very important to keep in mind the following points.
Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meanings in Java.
Class Names − For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case. Example: class MyFirstJavaClass
Program File Name − The name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append ‘.java’ to the end of the name (if the file name and the class name do not match, your program will not compile).
Method Names − All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word’s first letter should be in Upper Case. Example: public void myMethodName()
The semicolon(;) - It shows the compiler where an instruction ends and where the next instruction begins.
String - In Java, a string is a sequence of characters. We use double quotes to represent a string in Java.