- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Syntax
Java syntax is the set of foundational rules that define how a Java program is written and interpreted. Think of it as the grammar of the language; without following these rules, the compiler won't understand your instructions. Java is known for being "strongly typed" and "verbose," which means it requires explicit instructions but offers great clarity in return.
Class Declaration:
- In Java, every single piece of executable code must live inside a class.
- Classes are declared using the
classkeyword. By convention, class names start with an uppercase letter (e.g.,UserAccount). - A class acts as a container for fields (data) and methods (behavior).
public class MyProgram, the file must be named MyProgram.java.
Main Method:
- The main method is the "entry point" for any Java application. When you run a program, the Java Virtual Machine (JVM) looks for this specific method to start execution.
- The standard signature is:
public static void main(String[] args).
Statements and Expressions:
- Statements are complete units of execution—think of them as sentences that end in a semicolon (
;). For example:int x = 5; - Expressions are fragments of code that evaluate to a single value. For example,
5 + 3is an expression that evaluates to8.
Comments:
- Comments are notes written for humans to read. The Java compiler completely ignores them.
- Single-line: Use
//for short notes. - Multi-line: Use
/* content */for longer explanations or to temporarily disable blocks of code.
Variables and Data Types:
- Variables are reserved memory locations used to store data.
- Java requires you to declare the "type" of data a variable will hold.
- Primitive types: Lightweight types like
int(whole numbers),double(decimals), andboolean(true/false). - Reference types: More complex objects like
String(text) or custom objects you create.
Control Flow:
- Control flow allows your program to make decisions and repeat actions.
- Conditional Statements: Use
if,else if, andswitchto execute different code based on specific criteria. - Loops: Use
for,while, anddo-whileto repeat a task multiple times (e.g., processing every item in a list).
Methods:
- Methods (often called functions in other languages) are reusable blocks of code.
- They help keep your code "DRY" (Don't Repeat Yourself). Instead of writing the same logic five times, you write one method and call it five times.
Classes and Objects:
- A Class is a blueprint. For example, a class called
Cardefines what a car is (color, engine type). - An Object is the actual physical item built from that blueprint. Your neighbor's "Red Honda" is an instance (object) of the
Carclass.
Packages and Import Statements:
- Packages are used to group related classes together, similar to how folders organize files on your computer.
- Import statements allow you to use classes that belong to other packages. For example, if you want to generate a random number, you would
import java.util.Random;.
com.myapp.models or com.myapp.utils) to keep your project manageable as it grows.
Example
Here is a complete example that brings these syntax elements together into a functional program:
// Every file starts with a class
public class HelloWorld {
// The main method: where the magic starts
public static void main(String[] args) {
// A variable declaration (Data Type + Name)
String message = "Hello, World!";
// A statement that calls a method to print text to the console
System.out.println(message);
// Using a simple loop (Control Flow)
for (int i = 1; i <= 3; i++) {
System.out.println("Iteration: " + i);
}
}
}
Summary
Java syntax is the foundation of your journey as a developer. By mastering class declarations, the main method signature, and the use of variables and control flow, you gain the ability to build structured, efficient, and readable software. Remember that Java is case-sensitive and strict about its rules—precision here pays off in fewer bugs later.