Java Syntax & Structure
Master the basics of Java syntax. Learn about classes, the main method, comments, and identifiers.
Master the basics of Java syntax. Learn about classes, the main method, comments, and identifiers. This hands-on tutorial focuses on practical implementation of java syntax & structure concepts.
Java Syntax & Structure
Java has strict rules (syntax) that must be followed. It is case-sensitive and uses curly braces {} to define blocks of code.
The Class Structure
Every line of code that runs in Java must be inside a class.
public class MyClassName {
// Code goes here
}
[!IMPORTANT] The filename must exactly match the class name. If the class is
MyClass, the file must beMyClass.java.
The main() Method
As seen before, this is where execution starts.
public static void main(String[] args) {
// Execution starts here
}
Comments
Comments are ignored by the compiler. Use them to explain your code.
// This is a single-line comment
/*
* This is a
* multi-line comment
*/
Identifiers & Naming Conventions
Identifiers are names for classes, variables, and methods.
- Classes: PascalCase (e.g.,
MyFirstClass) - Methods: camelCase (e.g.,
calculateTotal) - Variables: camelCase (e.g.,
userAge) - Constants: UPPER_SNAKE_CASE (e.g.,
MAX_VALUE)
Interactive Example
Try modifying the code below to print your own name!
println vs print
System.out.println(): Prints text and moves to a new line.System.out.print(): Prints text and stays on the same line.
AI Mentor
Confused about "Java syntax rules, naming conventions and print methods"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4If a class is named 'Car', what must the filename be?
Next Steps
Now we have the structure down. Next, we'll learn about storing data using Variables.