Java Lesson 1 - Syntax

    In this tutorial, we will discuss the structure of Java code. This lesson will not discuss all the keywords and their meaning. This mostly focuses on showing you what a Java code looks like, briefly explaining what the main components of a Java code are and how to compile and run a Java code. We will be learning deeply about Java keywords and their functions in later tutorials.

Setup

    To continue with the tutorial, we need to have a text editor. There are plenty of text editors available for Java development. Some of them are IntelliJ IDEA, Netbeans, Eclipse, Visual Studio Code, Sublime Text. We can choose any of the text editors available on the internet.

Frist Java code

Create a new java file called Main.java using our text editor and save it in a preferred folder. (C:\Java\Main.java). Then enter the following code to Main.java

    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello Java");
        }
    }

Now open up the command prompt (Terminal) and navigate to the folder where you saved our Java file.

    cd C:\Java

Let's Compile our Java code using the javac command.

    javac Main.java

Finally, we can run our Java code using the java command.

    java Main

It should print "Hello Java" on our command prompt as the output.

Now we have written and executed our first and one of the simplest Java code.

Explanation

1. public class Main :

    Here we have declared a class named Main. Every line of code that runs in Java must be inside a class. When declaring a class and saving a java file, the file name and class name must be the same. Java is a case-sensitive language. Hence MyClass and myClass are different. You will learn more about classes later in OOP tutorials.

    The public keyword is used to make our class accessible to other classes when there are multiple classes in your project. These are called access modifiers. These will also be discussed later in the tutorial series.

2. public static void main(String[] args) :
    
       This is the main method of our Java program. A method is a set of codes that carries out a specific task. Every Java program has a main method, and it is the entry point of our Java program. This means, whenever we run a Java program, it starts executing from the main method. If we have not defined the main method in the class, we will get an error stating that there is no main method found.

3. System.out.println("Hello Java") :

    This is a statement. A statement is one line of code that instructs to do something. In our case, we ask to print. "Hello Java" to our command prompt. Every statement should end with a semicolon.

3. Curly Brackets - {} :
    
   Curly brackets are used to group a set of statements. It defines a clear scope to work. We use curly brackets to define the scope of our Main class. Then inside a class, we use curly brackets to define our main method. In the scope of our main method, we execute the statement System.out.println("Hello Java");.

    According to our simple example, we learned that,
  • Java code has classes, and each line of code exists inside a class.
  • Java code should contain the main method inside a class to be able to run the code.
  • Java code consists of lines of codes ending with semicolons called statements that instruct a program to do small things.
  • Curly brackets are used to define the scope of classes, methods, etc.
  • Every Java code follows this structure or a similar structure.
    We will discuss some of the topics that we did not discuss in this tutorial later in OOP tutorials. In the next lesson, we will discuss Java compiler, JVM, Bytecode, and other Java environment-related topics.

Comments

Popular posts from this blog