JAVA SYNTAX
Prepared by : Elcris M. Timbal
Java syntax is the set of
rules that defines the
combinations of symbols that
are considered to be correctly
structured Java programs.
Here’s a basic overview of Java
syntax and some key elements:
Basic Structure
A simple Java program structure looks like this:
public class Main: Defines a class named Main. In Java, everything must be
inside a class.
public static void main(String[] args): This is the entry point of any Java
application. The main method is where the program starts executing.
System.out.println("Hello, World!");: This line prints "Hello, World!" to the
Data Types
Java is a statically-typed language, which means you must declare
the type of a variable before using it. Some basic data types include:
• int: Integer values (int x = 5;)
• double: Floating-point numbers (double y = 4.5;)
• char: Single characters (char letter = 'A’;)
• boolean: True or false values (boolean isTrue = true;)
Variables and Constants
Variables: Store data that can change. They must be declared with a type and can
be initialized
Examples at the same time.
of Valid
Identifiers
Constants: Store data that should not change. Declared using the final keyword.
Operators
Exampes of Invalid
Identifiers
Java supports a variety of operators,
including:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, >, <, >=, <=
Logical Operators: &&, ||, !
Control Flow
Java provides several control flow statements:
If Statement: Executes code based on a condition.
Control Flow
Java provides several control flow statements:
Switch Statement: Chooses a block of code to execute based
on the value of a variable.
Control Flow
Java provides several control flow statements:
For Loop: Repeats a block of code a specific number of times.
Control Flow
Java provides several control flow statements:
While Loop: Repeats a block of code as long as a condition is
true.
Methods
Methods define a block of code that performs a task and can return a
result.
Classes and Objects
Java is an object-oriented language. Classes are blueprints for
creating objects.
Class Definition:
Classes and Objects
Java is an object-oriented language. Classes are blueprints for
creating objects.
Creating Objects:
Exception Handling
Java uses try-catch blocks to handle exceptions (errors).