🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #4

Java Syntax


Java Syntax

In the previous chapter, we created a Java file called Main.java, and we used the following code to print "Hello World" to the screen:

Example

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

Example explained

Every line of code that runs in Java must be inside a class. The class name should always start with an uppercase first letter. In our example, we named the class Main.

Note: Java is case-sensitive. MyClass and myclass would be treated as two completely different names.

The name of the Java file must match the class name. So if your class is called Main, the file must be saved as Main.java. This is because Java uses the class name to find and run your code. If the names don't match, Java will give an error and the program will not run.

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: Go to the Get Started Chapter for how to install Java. The output should be:

<code class="notranslate">
Hello World
</code>

The main Method

The main() method is required in every Java program. It is where the program starts running:

public static void main(String[] args)

Any code placed inside the main() method will be executed.

For now, you don't need to understand the keywords public, static, and void. You will learn about them later in this tutorial. Just remember: main() is the starting point of every Java program.


System.out.println()

Inside the main() method, we can use the println() method to print a line of text to the screen:

Example

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

Note: The curly braces {} mark the beginning and the end of a block of code. System.out.println() may look long, but you can think of it as a single command that means: "Send this text to the screen." Here's what each part means (you will learn the details later): System is a built-in Java class. out is a member of System, short for "output". println() is a method, short for "print line". Finally, remember that each Java statement must end with a semicolon (;).

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java Syntax – FAQs

Quick answers about learning Java Syntax in Java.

This free note from CodingNow 2.0 explains Java Syntax in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java Syntax, is 100% free with no signup required.
With focused practice, most students grasp Java Syntax in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now