Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Java's Syntax | Getting Started
Java Basics

bookJava's Syntax

The Syntax We'll Use

Java syntax might seem intimidating at first, especially when you're writing a basic main class with phrases like public static void. But it's not as complicated as it appears. This syntax is just the starting point for your program, and the Java compiler looks for it to run your code.

You can easily remember how it should look, but here's a quick breakdown of what each part does:

Main.java

Main.java

copy
12345
public class Main { public static void main(String[] args) { } }
  • public class Main defines a class, which is the foundation of Java programs. Since Java is an Object-Oriented language, classes are essential and will be covered in a separate course;
  • public static void main is the entry point of the program:
    • public allows the method to be accessed from anywhere;
    • static means the method belongs to the class itself;
    • void means it returns no value;
    • String[] args is an array for command-line arguments;
  • Java code is written inside curly braces { }, which define the code body;
  • Each line of code ends with a semicolon ;, except for class and method declarations.

Output Operation

We can print a message to the console using the command System.out.println().

Keep in mind that we need to place this command inside the curly braces of the main method. Here's an example:

Main.java

Main.java

copy
1234567
package com.example; public class Main { public static void main(String[] args) { System.out.println("Message for you"); } }

The package com.example groups related classes in a Java project, helping keep code organized and preventing conflicts when classes share the same name—similar to organizing files into folders.

This code prints a message to the console using System.out.println(). Any text you want to display must be enclosed in double quotation marks (""). Remember, double quotes are used for text, while single quotes (' ') represent a different data type that we’ll cover later.

Here’s an example of printing custom text:

Main.java

Main.java

copy
1234567
package com.example; public class Main { public static void main(String[] args) { System.out.println("Custom message for you"); } }
question mark

How can we output message to the console?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookJava's Syntax

Swipe to show menu

The Syntax We'll Use

Java syntax might seem intimidating at first, especially when you're writing a basic main class with phrases like public static void. But it's not as complicated as it appears. This syntax is just the starting point for your program, and the Java compiler looks for it to run your code.

You can easily remember how it should look, but here's a quick breakdown of what each part does:

Main.java

Main.java

copy
12345
public class Main { public static void main(String[] args) { } }
  • public class Main defines a class, which is the foundation of Java programs. Since Java is an Object-Oriented language, classes are essential and will be covered in a separate course;
  • public static void main is the entry point of the program:
    • public allows the method to be accessed from anywhere;
    • static means the method belongs to the class itself;
    • void means it returns no value;
    • String[] args is an array for command-line arguments;
  • Java code is written inside curly braces { }, which define the code body;
  • Each line of code ends with a semicolon ;, except for class and method declarations.

Output Operation

We can print a message to the console using the command System.out.println().

Keep in mind that we need to place this command inside the curly braces of the main method. Here's an example:

Main.java

Main.java

copy
1234567
package com.example; public class Main { public static void main(String[] args) { System.out.println("Message for you"); } }

The package com.example groups related classes in a Java project, helping keep code organized and preventing conflicts when classes share the same name—similar to organizing files into folders.

This code prints a message to the console using System.out.println(). Any text you want to display must be enclosed in double quotation marks (""). Remember, double quotes are used for text, while single quotes (' ') represent a different data type that we’ll cover later.

Here’s an example of printing custom text:

Main.java

Main.java

copy
1234567
package com.example; public class Main { public static void main(String[] args) { System.out.println("Custom message for you"); } }
question mark

How can we output message to the console?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
some-alt