Welcome back, Java enthusiasts! Now that you’ve set up your development environment and written your first Java program, it’s time to dive deeper into the basics. Understanding the syntax is crucial as it forms the foundation of your coding skills. In this article, we’ll cover the basic syntax of Java, including data types, variables, operators, and control statements. Let’s get started!
Basic Structure of a Java Program
Every Java program consists of one or more classes, and each class can contain fields (variables) and methods (functions). The entry point of any Java program is the main
method.
Here’s a quick refresher on the “Hello, World!” program to illustrate the structure:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Class Declaration:
public class HelloWorld
declares a class namedHelloWorld
. - Main Method:
public static void main(String[] args)
is the entry point of the program. Themain
method must bepublic
,static
, and returnvoid
. - Statements: Inside the
main
method, we have a statement:System.out.println("Hello, World!");
which prints the text to the console.
Comments
Comments are ignored by the compiler but are useful for documenting your code.
- Single-line Comment: Use
//
for single-line comments. - Multi-line Comment: Use
/* ... */
for comments that span multiple lines. - Documentation Comment: Use
/** ... */
to create documentation comments that can be used by tools like Javadoc.
// This is a single-line comment
/*
This is a
multi-line comment
*/
/**
* This is a documentation comment
* for the class HelloWorld.
*/
Data Types
Java is a strongly typed language, which means every variable must have a declared type. Java has two main categories of data types:
- Primitive Data Types: byte, short, int, long, float, double, char, boolean
- Reference Data Types: Objects, Arrays
Here’s an example demonstrating the use of different data types:
public class DataTypesExample {
public static void main(String[] args) {
int number = 10; // integer
double price = 19.99; // double
char letter = 'A'; // character
boolean isJavaFun = true; // boolean
System.out.println("Number: " + number);
System.out.println("Price: " + price);
System.out.println("Letter: " + letter);
System.out.println("Is Java Fun? " + isJavaFun);
}
}
Variables
Variables are containers for storing data values. In Java, you need to declare a variable before you can use it. A variable declaration includes the type and the name of the variable.
int age; // Declaration
age = 25; // Initialization
int year = 2024; // Declaration and Initialization
Operators
Operators are special symbols that perform specific operations on one, two, or three operands and then return a result. Here are some common types of operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
- Relational Operators:
==
,!=
,>
,<
,>=
,<=
- Logical Operators:
&&
,||
,!
- Assignment Operators:
=
,+=
,-=
,*=
,/=
,%=
Example:
public class OperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Arithmetic Operators
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
// Relational Operators
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
// Logical Operators
boolean x = true;
boolean y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
}
}
Control Statements
Control statements allow you to control the flow of your program. The main control statements in Java are:
- If-Else: Conditional execution
- Switch: Multiple conditional branches
- For Loop: Repeated execution with a known number of iterations
- While Loop: Repeated execution while a condition is true
- Do-While Loop: Similar to while loop but checks the condition after executing the block
Examples:
public class ControlStatementsExample {
public static void main(String[] args) {
int number = 10;
// If-Else
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number or zero");
}
// Switch
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
break;
}
// For Loop
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
// While Loop
int i = 1;
while (i <= 5) {
System.out.println("Iteration " + i);
i++;
}
// Do-While Loop
int j = 1;
do {
System.out.println("Iteration " + j);
j++;
} while (j <= 5);
}
}
Conclusion
Understanding the basic syntax of Java is the first step in becoming proficient in the language. In this article, we’ve covered the structure of a Java program, comments, data types, variables, operators, and control statements. Practice writing and running simple programs to reinforce these concepts.
Next up: We’ll explore Java’s object-oriented programming features, such as classes, objects, inheritance, and polymorphism. Stay tuned and happy coding!