Categories
Learn Java

Learn Java Syntax

Learn Java Syntax

In the previous tutorial, we created a Java file named First.java. We used the code given below to print “Hello From H4C.”

First.java

public class Main {

  public static void main(String[] args) {

    System.out.println("Hello From H4C");

  }

}

 

 

Explanation

  • Every code you run in Java must be inside the class.
  • In our example, we named the class Main.
  • The class will always start with the first uppercase letter.

Note: Java language is case-sensitive. That means “Myclass” and “myclass” has different meanings.

 

The java file name should match the class name. While saving the file, save it using the class name. Then add “.java” to the end of the filename.

You can run the example given previously again. The output should be:

“Hello From H4C”

 

The “main” Method

Using main() is required, and you will explore it in every java program.

Example: public static void main(String[] args)

 

You can execute any code inside the main() method. No need to worry about the keywords before and after the main. You will get to learn about them more while reading this tutorial.

For now, just remember that:

  • Every Java program has a class name that must match the filename.
  • And that every program must contain the main() method.

 

System.out.println()

Within the main() method, we can also use the println() method. So that we can print the line of text to the screen:

public static void main(String[] args) {

  System.out.println("Hello From H4C");

}

 

Output : “Hello From H4C”

 

 

Important Notes:

  • The curly brackets mean the start and the end of the block of a code.
  • The system is a built-in Java class that contains useful members. For example, out, which is short for “output”. 
  • The println() method, is short for “print line”, is used to print a value to the screen or a file.

 

Don’t stress over System, out, and println(). You will need all of them to print something on the screen.

 

 

More On Java Syntax

Now you know how to use the println( ) method to output values or to print text in java.

 

Examples:

System.out.println("Hello From H4C");

System.out.println("I am learning Java.");

System.out.println("It is cool!");

 

In the same way, you can output numbers also using println ( ) and perform mathematical calculations.

 

Example:

System.out.println(4 + 4);

 

The complete coding will be:

public class Main {

  public static void main(String[] args) {

    System.out.println(4 + 4);

  }

}

 

 

 

The Print() Method

You can also use a print( ) method, it is similar to println( ).

However, print( ) does not insert a new line at the end of the output.

 

Example:

public class first {


    public static void main(String[] args) {


        System.out.println("Hello From H4C");
        System.out.println("Hello From Home4Cloud or H4C");
        System.out.println("I will print on the same line.");


    }


}

 

Check the Image for the compiled Java code