Java Basic Syntax
Before diving into Java's advanced features, mastering its basic syntax is crucial. This chapter will introduce a basic Java program structure and explain its key components, such as case sensitivity, class names, method names, and keywords.
First Java Program: Hello, World!
Let's start with the traditional beginner program from the programming world—outputting "Hello, World!". Through this simple example, we can understand what a complete Java program looks like.
// This is a simple Java program
// The filename must be HelloWorld.java
public class HelloWorld {
// The main method is the entry point of the program
public static void main(String[] args) {
// Use System.out.println() to print text to the console
System.out.println("Hello, World!");
}
}To compile and run this program, you can:
- Save the code as a file named
HelloWorld.java. - Open a terminal or command prompt and navigate to the directory where the file is located.
- Compile: Run
javac HelloWorld.java. This will generate a bytecode file namedHelloWorld.class. - Run: Run
java HelloWorld. The console will outputHello, World!.
Java Program Basic Structure Analysis
Now, let's break down the above code line by line:
public class HelloWorld:- The
classkeyword is used to declare a class. In Java, all code must reside within a class. HelloWorldis the name of the class we defined. In Java, the name of a public class must exactly match the filename (including case). Therefore, this file must be namedHelloWorld.java.publicis an access modifier, meaning this class can be accessed by any other class.
- The
public static void main(String[] args):- This is the program's main (
main) method. When you run a Java program, the JVM starts execution from this method. It is the entry point for all Java applications. public: Means this method can be called from outside the class.static: Indicates this method belongs to theHelloWorldclass itself, not to a specific instance (object) of the class. This allows the JVM to call it without creating aHelloWorldobject.void: Indicates this method does not return any value.main: Is the standard name for this method.String[] args: This is the method's parameter. It's an array of strings that allows you to pass arguments from the command line when running the program.
- This is the program's main (
System.out.println("Hello, World!");:- This is the core statement that performs the output.
Systemis a built-in final class located in thejava.langpackage.outis a static member variable of theSystemclass, which is an object of thePrintStreamclass.println()is a method of thePrintStreamclass that prints the string passed to it and adds a newline character at the end.- Note: Every statement in Java must end with a semicolon (
;).
Key Syntax Rules
Case-Sensitive
- Java is case-sensitive. This means
HelloWorldandhelloworldare two completely different identifiers. Keywords (likepublic,class) must also use lowercase.
- Java is case-sensitive. This means
Identifiers
- Identifiers are names used to name classes, methods, variables, etc.
- All identifiers must begin with a letter (A-Z or a-z), dollar sign (
$), or underscore (_). - After the first character, identifiers can contain letters, dollar signs, underscores, or digits (0-9).
- Keywords cannot be used as identifiers.
Keywords
- Keywords are reserved words in the Java language with special meanings. Examples include
class,public,static,void,if,else,for,while, etc. You cannot use these words as variable names or class names.
- Keywords are reserved words in the Java language with special meanings. Examples include
Naming Conventions
- Following standard naming conventions makes your code easier to read and understand. These are not mandatory syntax rules, but best practices followed by all Java developers.
- Class names: Use "Upper CamelCase", where the first letter of each word is capitalized. Examples:
HelloWorld,MyFirstProgram. - Method and variable names: Use "lower CamelCase", where the first letter of the first word is lowercase and the first letters of subsequent words are capitalized. Examples:
myVariable,calculateSum(). - Constant names: All letters are uppercase, with words separated by underscores. Examples:
MAX_VALUE,PI.
Mastering these basic syntax rules is the first step to writing any Java program. In the following chapters, we will build more complex concepts based on this knowledge.