Syntax Recap — Java
15 topic(s) — definitions, syntax, and examples.
Definition
A class is a blueprint for creating objects in Java.
Purpose
Define the structure and behavior of objects.
General Syntax
Example
Declares a Student class with name and age fields.
Definition
The main method is the entry point of any Java application.
Purpose
JVM calls the main method to start program execution.
General Syntax
Example
The main method prints 'Hello' to the console.
Definition
Conditional statement that executes different code blocks based on a boolean condition.
Purpose
Make decisions in program flow.
General Syntax
Example
Since x is 10 which is greater than 5, it prints 'Greater'.
Definition
A for loop repeats a block of code a fixed number of times.
Purpose
Iterate over a range of values or array elements.
General Syntax
Example
Prints numbers 0 through 4, each on a new line.
Definition
An array stores multiple values of the same type in a single variable.
Purpose
Store and access collections of data efficiently.
General Syntax
Example
Creates an integer array of size 5 and assigns 10 to the first element.
Definition
A method is a block of code that performs a specific task and can return a value.
Purpose
Encapsulate reusable functionality.
General Syntax
Example
Defines a method that takes two integers and returns their sum.
Definition
A constructor initializes objects when they are created.
Purpose
Set initial values for object attributes.
General Syntax
Example
Constructor takes a name parameter and assigns it to the name field.
Definition
Inheritance allows a class to inherit properties and methods from another class.
Purpose
Reuse code and establish hierarchical relationships.
General Syntax
Example
Dog inherits the eat() method from Animal and adds its own bark() method.
Definition
Try-catch handles exceptions that may occur during program execution.
Purpose
Prevent program crashes by gracefully handling errors.
General Syntax
Example
The try block attempts division by zero, which throws an exception caught by the catch block.
Definition
ArrayList is a resizable array implementation of the List interface.
Purpose
Store dynamic collections of elements that can grow or shrink.
General Syntax
Example
Creates an ArrayList, adds two names, and prints the first one.
Definition
A while loop repeatedly executes a block of code as long as a condition is true.
Purpose
Repeat code when the number of iterations is not known in advance.
General Syntax
Example
Prints 0, 1, 2. The loop stops when i becomes 3.
Definition
Switch executes one of many code blocks based on the value of an expression.
Purpose
Replace multiple if-else statements for cleaner code.
General Syntax
Example
Since day is 2, it prints 'Tuesday'.
Definition
An interface defines abstract methods that implementing classes must define.
Purpose
Achieve abstraction and multiple inheritance in Java.
General Syntax
Example
Circle implements the Drawable interface and provides the draw() method.
Definition
String class provides methods like length(), charAt(), substring() for string manipulation.
Purpose
Manipulate and analyze text data.
General Syntax
Example
Prints 5 (length) and 'H' (first character).
Definition
File handling in Java uses classes like FileReader, BufferedReader to read and write files.
Purpose
Read from and write to external files for data persistence.
General Syntax
Example
Opens data.txt, reads the first line, and closes the file.