Java'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
12345public class Main { public static void main(String[] args) { } }
public class Maindefines 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 mainis the entry point of the program:publicallows the method to be accessed from anywhere;staticmeans the method belongs to the class itself;voidmeans it returns no value;String[] argsis 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
1234567package 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
1234567package com.example; public class Main { public static void main(String[] args) { System.out.println("Custom message for you"); } }
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.7
Java'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
12345public class Main { public static void main(String[] args) { } }
public class Maindefines 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 mainis the entry point of the program:publicallows the method to be accessed from anywhere;staticmeans the method belongs to the class itself;voidmeans it returns no value;String[] argsis 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
1234567package 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
1234567package com.example; public class Main { public static void main(String[] args) { System.out.println("Custom message for you"); } }
Thanks for your feedback!