Java Beginner Syntax Guide (Expanded & Exam-
Focused)
This guide is designed for absolute beginners and engineering / CS students preparing for Java exams.
It explains every concept clearly, step by step, with syntax you must memorize and rules examiners
expect you to know.
1. What is Java? (EXAM THEORY)
• Java is a high-level, object-oriented, compiled programming language.
• Java code is first compiled into bytecode using the Java compiler ( javac ).
• Bytecode runs on the Java Virtual Machine (JVM).
• This allows Java programs to run on any operating system.
📌 Exam phrase to remember:
Java follows the principle of "Write Once, Run Anywhere".
2. Structure of a Java Program (VERY IMPORTANT)
public class Main {
public static void main(String[] args) {
[Link]("Hello World");
}
}
Line-by-line explanation
• public → accessible everywhere
• class → used to define a class
• Main → class name (must match file name [Link] )
• { } → define the body of the class
main method
• Program execution always starts from main()
• Without main , the program will not run
1
public static void main(String[] args)
• public → JVM can access it
• static → JVM does not need to create an object
• void → returns nothing
• String[] args → command-line arguments
3. Comments
Comments are ignored by the compiler and used for explanation.
Single-line
// This is a comment
Multi-line
/* This is a
multi-line comment */
Documentation comment
/** Used to generate documentation */
4. Variables (CORE CONCEPT)
A variable is a named memory location.
Declaration
int x;
Initialization
int x = 10;
2
General syntax
dataType variableName = value;
Rules (EXAM FAVORITE)
• Must start with letter, _ , or $
• Cannot start with a digit
• Cannot use keywords
• Case-sensitive ( age ≠ Age )
5. Data Types
Primitive Data Types (store values directly)
Type Size Example
byte 1 byte byte b = 10;
short 2 bytes short s = 100;
int 4 bytes int x = 5;
long 8 bytes long l = 10L;
float 4 bytes float f = 2.5f;
double 8 bytes double d = 3.14;
char 2 bytes char c = 'A';
boolean 1 bit boolean flag = true;
⚠️ float must end with f , long with L .
Reference Data Types
• String
• Arrays
• Classes
They store addresses, not actual values.
3
6. Strings (EXAM TRAP SECTION)
String name = "Java";
Important rules
• Strings are objects
• Strings are immutable (cannot be changed)
Common String methods
[Link]();
[Link]();
[Link]();
[Link](0);
[Link]("Java");
❌ WRONG:
name == "Java";
✅ CORRECT:
[Link]("Java");
7. Operators
Arithmetic
+ - * / %
Assignment
= += -= *= /=
Relational / Comparison
== != > < >= <=
4
Logical
&& || !
Increment / Decrement
x++;
--y;
8. Input and Output
Output
[Link]("Hello");
[Link]("Hello");
Input using Scanner
import [Link];
Scanner sc = new Scanner([Link]);
int x = [Link]();
double d = [Link]();
String s = [Link]();
⚠️ nextLine() after numbers may require extra handling in exams.
9. Type Casting
Widening (Automatic)
int x = 5;
double d = x;
Narrowing (Manual)
double d = 5.7;
int x = (int) d; // x = 5
5
10. Decision Making
if
if (x > 0) {
}
if-else
if (x > 0) {
} else {
}
else-if ladder
if (x > 0) {
} else if (x == 0) {
} else {
}
switch
switch (day) {
case 1:
break;
default:
break;
}
11. Loops
for
for (int i = 0; i < 5; i++) {
}
6
while
while (x > 0) {
}
do-while
do {
} while (x > 0);
12. Loop Control
• break → exits loop
• continue → skips iteration
13. Arrays
Declaration
int[] arr = new int[5];
Initialization
int[] arr = {1, 2, 3};
Access
arr[0];
Length
[Link];
7
14. Methods
Definition
static int add(int a, int b) {
return a + b;
}
Call
int sum = add(2, 3);
void method
static void show() {
}
15. Method Overloading
Same name, different parameters.
16. Classes and Objects
class Car {
int speed;
}
Car c = new Car();
17. Constructors
class Car {
int speed;
Car(int s) {
speed = s;
8
}
}
18. Access Modifiers
Modifier Access
public Everywhere
private Same class
protected Package + subclass
default Same package
19. static Keyword
Belongs to class, not object.
20. final Keyword
Used to make constants.
21. Packages and Import
import [Link];
22. Common Exam Errors
• Missing semicolons
• Wrong brackets
• Using == for strings
• Forgetting static
• Array index out of bounds
9
23. Keywords to Memorize
class, public, static, void, int, double, if, else, for, while, switch, break,
continue, return, new, final
24. Compilation
javac [Link]
java Main
25. Final Exam Advice
• Syntax first, logic second
• Write clean, readable code
• Don’t panic — partial marks exist
END OF GUIDE
10