Nearby lessons
10 of 125Java - Basic Syntax
- The small building blocks of Java: tokens
- All 8 primitive data types with their ranges
- Type casting — implicit and explicit
- Control statements: if, switch, loops
- Arrays — single, double and jagged
- Variable length arguments (var-args)
Basic Syntax is a core concept of the Java language. This lesson explains The Structure of a Java Program, Tokens — The Smallest Building Blocks and More About Tokens — Lexeme and Token with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Structure of a Java Program
Every well-written Java file follows a fixed order of sections. Memorise this format:
| Section | Purpose |
|---|---|
| Comment Section | Describes the program: author, objective, project details. |
| Package Section | Puts the classes into a named package (must be the first statement). |
| Import Section | Brings in ready-made classes from other packages. |
| Classes/Interfaces Section | Represents the real-world entities (Employee, Student, Account...). |
| Main Class Section | Contains main() — the starting point of the application. |
Tokens — The Smallest Building Blocks
A Java program is made of many small pieces. These smallest meaningful pieces are called tokens. Just like an English sentence is made of words, a Java program is made of tokens.
There are five types of tokens in Java:
| Token type | Meaning | Example |
|---|---|---|
| Identifiers | Names given by the programmer to classes, variables, methods | a, name, Student, totalMarks |
| Literals | Constant values written directly in the code | 10, 5.5, 'A', "Hello", true |
| Keywords | Words with fixed meaning, reserved by Java | class, int, if, new, return |
| Operators | Symbols that perform operations | +, -, *, /, =, == |
| Separators | Symbols that separate parts of code | {, }, (, ), ;, , |
More About Tokens — Lexeme and Token
The smallest logical unit in a Java program is called a lexeme. A token is a group of lexemes that belong to the same category.
There are four types of tokens in Java: Identifiers, Literals, Keywords (Reserved Words) and Operators.
Identifier Rules — All the Details
| Rule | Valid examples | Invalid examples |
|---|---|---|
| Must not start with a number; may start with a letter, _ or $ | int eno = 111; | int 9eno = 999; |
| After the first letter, digits are allowed | String emp9No = "E-9"; | — |
| _ and $ are allowed anywhere | emp_Addr, emp$Sal | emp-Addr, emp+No |
| No spaces inside a name | concat(), getInputStream() | for Name(), get Input Stream() |
| No duplicate names in the same scope | i in class + i in method (different scopes) | two i at class level |
| Predefined class names may be used as identifiers (with care) | int Exception = 10; | int System = 10; (then System class is shadowed) |
Literals — All the Details
A literal is a constant value written directly in code. Java has integer, floating-point, boolean, character and String literals. Since Java 7, you can also use underscores inside numbers to make them readable:
More About Tokens — Lexeme and Token
Java supports four number systems for integer literals:
| Number system | Prefix | Digits | Example |
|---|---|---|---|
| Binary (base 2) | 0b or 0B | 0, 1 | int b = 0b1010; (valid), 0b1012 (invalid) |
| Octal (base 8) | 0 (zero) | 0-7 | int o = 04567; (valid), 05678 (invalid) |
| Decimal (base 10) | none | 0-9 | int d = 123; (default) |
| Hexadecimal (base 16) | 0x or 0X | 0-9, a-f | int h = 0x89abcd; (valid), 0x9g (invalid) |
The compiler recognises the system from the prefix and converts everything to decimal internally before processing.
Keywords — The Reserved Words
A keyword is a word with both recognition and functionality (like class, int). A reserved word is only recognised but has no function in Java — the two reserved words are goto and const. Keywords are grouped by purpose:
More About Tokens — Lexeme and Token
Identifiers — Naming Rules
- An identifier can contain letters, digits, underscore _ and dollar $.
- It cannot start with a digit. 1name is wrong, name1 is correct.
- It cannot be a keyword. You cannot name a variable class or int.
- Java is case sensitive. total and Total are two different names.
- There is no length limit on the name, but keep it sensible.
Keywords
Keywords are words that Java has already reserved. You cannot use them as variable or class names. There are 53 keywords in Java. Some common ones:
Java Naming Conventions (Coding Habits)
Java is case sensitive — Name and name are different. Good developers follow these naming conventions so code is easy to read:
| Item | Convention | Examples |
|---|---|---|
| Class / interface / enum names | Start with a capital letter, then capital for each new word | String, StringBuffer, InputStreamReader |
| Variable names | Start with small letter, capital for each new word | studentName, totalMarks, in, out |
| Method names | Start with small letter, capital for each new word | concat(), getInputStream(), forName() |
| Constants | All capital letters with underscores | MAX_PRIORITY, MIN_VALUE |
| Package names | All small letters | java.util, com.example |
File Naming Rules — With Examples
The rule has two parts, and these five examples from the classic material make it crystal clear:
- If the file contains a public element (public class/interface/enum), the file must be named after it.
- If there is no public element, you can save the file with any name — but it is best to use the class that has main().
| File name | Content | Result |
|---|---|---|
| abc.java | class FirstApp { main() } (no public class) | Compiles — but not suggested |
| FirstApp.java | class FirstApp { main() } (no public class) | Compiles — suggested |
| FirstApp.java | public class A { } + class FirstApp { main() } | Error — file must be A.java |
| A.java | public class A { } + class FirstApp { main() } | Compiles |
| A.java | public class A { } + public class B { } | Error — two public classes |
The reason a file can have only one public class: the file would need more than one name, which is impossible on any operating system.
- Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
- Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
- Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
- if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
- Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
- Var-args (int... x) lets a method take any number of arguments.