bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

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:

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 ( ; ).

Previous

Java Getting Started

Next

Java Statements

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

Example explainedThe main MethodSystem.out.println()