What is Main Method in Java

Main Method in Java, Print Method and Output

In this article, we learn the what is main method in java, the print method in java, and output in java.

Syntax In Java

  • In the previous article, we write the first Java program called First.java, and we used the following code to print “Hello World” to the screen.
  • Every line of code that runs in Java must be inside a class. In our example, we named the class First. A class should always start with an uppercase first letter.
  • Java is case-sensitive: “FirstProgram” and “firstprogram” has different meaning.
  • The name of the java file must match the class name. When saving the file, save it using the class name and add “.java” to the end of the filename. To run the example above on your computer, make sure that Java is properly installed.
First.Java
First.Java

What is Main Method in Java?

  • Without main method we cannot run program hence you will see it in every Java program.
Main Method
Main Method

 

  • Any code inside the main method will be executed. Don’t worry about the keywords before and after main. You will get to know them step by step while reading this articles.
  • For now, just remember that every Java program has a class name which must match the filename and that every program must contain the main method.

What is Println() Method in Java?

  • Inside the main method, we can use the println() method to print a line of text to the screen.
Println Method
Println Method

 

  • The curly braces {} marks the beginning and the end of a block of code.
  • System is a built-in Java class that contains useful members, such as out, which is short for “output”. The println() method, short for “print line”, is used to print a value to the screen.
  • Don’t worry too much about System, out and println(). Just know that you need them together to print stuff to the screen.
  • You should also note that each code statement must end with a semicolon (;).

Output In Java

  • You learned from the previous concept that you can use the println() method to output values or print text in Java.
Output In Java
Output In Java

 

  • You can add as many println() methods as you want. Note that it will add a new line for each method.
Println Example
Println Example

 

  • You can also output numbers, and perform mathematical calculations.
Numbers Output
Numbers Output

 

  • Note that we don’t use double quotes (“”) inside println() to output numbers.

What is Print() Method In java?

  • There is also a print() method, which is similar to println().
  • The only difference is that it does not insert a new line at the end of the output.
Print Method
Print Method

 

  • Note that we add an extra space (after “Hello World!” in the example above), for better readability.
  • We will only use println() as it makes it easier to read the output of code.

Other Popular Articles

What is Java and Why Use Java

Leave a Comment