Java Basic Syntax
- Java syntax defines the rules and structure used to write Java programs.
- Every Java program follows a specific format that includes classes and methods.
- Java is a case-sensitive language, meaning uppercase and lowercase letters are different.
- A Java program starts execution from the main() method.
- Understanding basic syntax is the first step toward learning Java programming.
What is Java Syntax?
Java syntax refers to the set of rules that define how Java programs are written. These rules specify how classes, methods, variables, statements, and other elements should be arranged in a program.
Like every programming language, Java has its own syntax structure that developers must follow to create error-free programs. If the syntax rules are not followed, the Java compiler will generate errors.
Basic Structure of a Java Program
A basic Java program contains a class and a main method. The main method is the entry point of a Java application where program execution begins.
class{ Main
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Example Explained
class
The class keyword is used to create a class in Java. Every Java program must have at least one class.
Main
Main is the name of the class. Developers can choose any valid class name.
main()
The main method is the starting point of execution for Java programs.
System.out.println()
This statement is used to display output on the screen.
Java Class Syntax
In Java, every program is written inside a class. A class is a blueprint that contains variables, methods, and other programming elements. The class keyword is used to create a class.
Syntax of Java Class
class ClassName{
// Code goes here
Example of Java Class
class Student{
String name;
int age;
}
Main Method Syntax in Java
The main() method is a special method in Java where program execution starts. The Java Virtual Machine (JVM) searches for this method to run a program.
Syntax of main() Method
public static void main(String[] args) {
// Program statements
}
Understanding the main() Method Components
public
The public keyword allows the JVM to access the main method from anywhere.
static
The static keyword allows the method to run without creating an object of the class.
void
The void keyword specifies that the method does not return any value.
String[] args
It stores command-line arguments passed to the Java program.
Java Statements
A statement in Java is an instruction that performs a specific action. Every Java
statement usually ends with a semicolon (;).
Example of Java Statement
int number = 10;
System.out.println(number);
Java Code Blocks
A block in Java is a group of statements enclosed within curly braces
({ }). Blocks are used to define the scope of classes, methods,
loops, and conditional statements.
Example of Java Block
class Main{
{
System.out.println("Java Block");
}
}
Java Identifiers
Identifiers are names used to identify classes, variables, methods, and other elements in a Java program.
- Identifiers can contain letters, digits, underscore (_), and dollar sign ($).
- Identifiers cannot start with a number.
- Java keywords cannot be used as identifiers.
- Identifiers are case-sensitive.
Example of Valid and Invalid Identifiers
Valid:
studentName
_age
totalMarks
Invalid:
2student
class
my-name
Java Keywords
Java keywords are reserved words that have predefined meanings in the Java programming language. These words cannot be used as names for variables, classes, or methods.
Java provides a fixed set of keywords that are used to perform specific operations, define data types, control program flow, and create classes and objects.
class
public
static
void
int
if
else
return
Java Case Sensitivity
Java is a case-sensitive programming language. It means Java treats uppercase and lowercase letters as different characters.
For example, a variable named name and another variable named
Name are considered different in Java.
Example of Case Sensitivity
class Main{
public static void main(String[] args) {
int age = 20;
int Age = 30;
System.out.println(age);
System.out.println(Age);
30
Complete Java Basic Syntax Example
The following example demonstrates the basic structure of a Java program including class creation, main method, variable declaration, and output statement.
class Student{
public static void main(String[] args) {
String name = "John";
int marks = 90;
System.out.println(name);
System.out.println(marks);
}
}
90
Best Practices for Writing Java Code
Following good coding practices helps developers write clean, readable, and maintainable Java programs.
- Use meaningful names for classes, variables, and methods.
- Follow proper indentation and formatting.
- Write comments to explain complex logic.
- Use consistent naming conventions.
- Avoid unnecessary code and maintain clean program structure.
Java Basic Syntax Summary
Java basic syntax provides the foundation required to write Java programs. Understanding classes, methods, statements, keywords, and identifiers helps beginners create their first Java applications.
Every Java program follows a specific structure where execution starts from the main() method. By learning these basic rules, developers can easily move toward advanced Java concepts.
Key Points to Remember
- Java programs are written inside classes.
- The main() method is the starting point of Java program execution.
- Java statements usually end with a semicolon (;).
- Java is a case-sensitive programming language.
- Keywords cannot be used as identifiers.
- Curly braces define code blocks in Java.
Frequently Asked Questions
What is Java syntax?
Java syntax is a set of rules that defines how Java programs should be written and structured.
Where does Java program execution start?
Java program execution starts from the main() method.
Why is Java called a case-sensitive language?
Java is case-sensitive because it treats uppercase and lowercase letters differently.
What is the purpose of the class keyword in Java?
The class keyword is used to create a class that contains program data and methods.