Java syntax is similar to that of C and C++ programming languages. It is a high level programming that is based on OOP object oriented programming. It uses classes and objects. So a code in Java language is a combination of objects and classes. These object communicate with each other to perform different functions.
Each application in this language, begins with a class name. Note that, the class name and file name should be same. Programmers can use any text editor like Notepad, Textpad or Eclipse, to write the application.
Steps to Create Java Code
After downloading the JDK packet, follow the below steps to create an executable java code file.
- Open Eclipse or Notepad.
- Copy paste the code given in the example below, to the notepad.
- Save the file as MyProgram.java
- Open the command prompt
- Write the command “javac MyProgram.java” in the command prompt
- Press Enter from your keyboard
The command prompt will compile the java file and execute it. The result will display “Hello World!”.
Hello World Example
For instance, the below example shows how to print the text “Hello World” in this language.
public class MyProgram { // Class Name public static void main(String []args) { System.out.println("Hello World!"); // prints Hello World! } }
Here, the name of class is MyProgram so the name of this file should also be MyProgram. Save the notepad file as MyProgram.java. class is the keyword. public is the access modifier. It is mandatory to give public access to at least one class in java code. You can read about access modifiers in detail at https://www.tutorialsart.com/java-modifiers/
Run the Java Program
To run the program, user needs to open command prompt.
and open the directory where the MyProgram.java file is saved. After opening that directory type in this command:
The above command will compile your program. If the program has no errors then it will be compiled successfully and command prompt will take the programmer to the next line. Then the programmer will write the following command:
The output of the above program after compilation will be:
This output will be printed on the command prompt. Following the above steps, user will be able to write and run code in this language.
Basic Syntax for Java
Following points should be kept in mind while writing code in this language:
- Case Sensitivity: It is a case sensitive language, that means the variables class and Class will be different and will have different meanings.
- Class Name: Every class name in the program should start with an uppercase letter. For example, in the above code, the class name is MyProgram. If class name contains more then one words then the best practice is to use camel case, like MyFirstProgram where first letter of each word is in upper case.
- File Name: The name of the program file and class should be same. For example, the name of program file in the above example is MyProgram.java. Since this language is case sensitive so the file name should also be in the same case letters as the class name. The file extension should be .java. If the program does not contain a public class then name of the file can be any name.
- Method Name: Every method name in the program should start with a lower case letter. However if the method name is made up of two or more words then the best practice is to use camel notation. In camel notation we write first letter of each word in upper case and rest are in lower case. For example, myFirstMethod() should be the name of method.
- main(): Like in C++, in this language, main() is also the compulsory method that is needed to run the program. Every executable program must contain main() method. The compiler executes the code inside main program in the curly braces {}. For example, in the code above, public static void main(String []args) is the main() method. Note that each line of code terminates at a semi colon ;.