….
🔤 Java Syntax – Writing Your First Real Code
Welcome to one of the most exciting parts of your Java journey — understanding the syntax! At GoNimbus, we believe learning Java should be both logical and enjoyable. Let’s break down the essential rules that make your Java programs run smoothly.
📂 Java File Structure
Every Java program begins with a class. Let’s take another look at a simple example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java from GoNimbus!");
}
}
🔍 Dissecting the Code
Element | What It Means |
---|---|
public class Main | Declares a class named Main . Class names in Java must start with a capital letter and match the filename (i.e., Main.java ). |
{ } | Curly braces define the scope of your class or method. |
public static void main(String[] args) | This is the main method — the entry point where Java starts executing your program. |
System.out.println(...) | A command that prints output to the screen. |
🧠 Important Java Rules
- Case Sensitivity: Java is case-sensitive.
Main
,main
, andMAIN
are treated as different identifiers. - Filename = Class Name: The filename must exactly match the public class name. Example:
MyApp.java
must containpublic class MyApp
. - Every Statement Ends with
;
(semicolon): Forgetting this causes compilation errors.
🛠 The main() Method – Your Program’s Launch Pad
public static void main(String[] args) {
// your code goes here
}
You’ll see this method in every Java application. While it may look confusing now, here’s a simplified breakdown:
Keyword | Meaning |
---|---|
public | Makes this method accessible to the Java runtime. |
static | Allows this method to run without creating an object. |
void | Means the method doesn’t return any value. |
String[] args | Accepts command-line arguments (you’ll learn about this later). |
📌 Tip: You don’t need to memorize it all right now — just know that your program must have this method to run.
📤 Print Output with System.out.println()
System.out.println("Java is awesome!");
- System – A built-in class in the
java.lang
package. - out – A static object (an instance of
PrintStream
) inside the System class. - println() – A method that prints a line of text and moves to the next line.
💬 Want to print without a new line? Use System.out.print()
instead.
🧩 Syntax Summary
Concept | Example |
---|---|
Class Declaration | public class HelloWorld { } |
Main Method | public static void main(String[] args) { } |
Output | System.out.println("Hello Java!"); |
Block of Code | Enclosed between { } |
Statement End | Each line ends with ; |
🌟 GoNimbus Bonus Tips
✅ Use indentation to improve code readability
✅ Start class names with uppercase (e.g., Student
, Employee
)
✅ Use meaningful names — avoid names like abc
, xyz
✅ Write comments to explain your code logic
📘 Up Next: Java Variables and Data Types
Now that you understand how Java syntax works, it’s time to explore how to store and manipulate data using variables and data types.