JAVA
JAVA
---
```java
```
**Explanation**:
- `public static void main(String[] args)`: The entry point of a Java program. The `main`
method is where execution starts.
---
Java is a statically-typed language, meaning you must declare the type of a variable before
using it.
```
---
Java supports the basic operators used in arithmetic, comparison, and logical operations.
- `+`: Addition
- `-`: Subtraction
- `*`: Multiplication
- `/`: Division
```java
int x = 5 + 3; // 8
int y = 10 - 2; // 8
```
#### **Comparison Operators**:
- `==`: Equal to
```java
int a = 5;
int b = 10;
```
- `||`: Logical OR
```java
boolean x = true;
boolean y = false;
```
---
### **5. Control Flow**
Control flow statements allow you to change the flow of execution in your program.
```java
if (number > 5) {
} else {
```
```java
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("Invalid day");
```
#### **Loops**:
- **For Loop**: Used when you know how many times you want to iterate.
```java
System.out.println(i); // Prints 0 to 4
```
- **While Loop**: Used when you want to loop until a condition is met.
```java
int i = 0;
while (i < 5) {
System.out.println(i); // Prints 0 to 4
i++;
}
```
- **Do-While Loop**: Similar to a `while` loop, but it guarantees to execute at least once.
```java
int i = 0;
do {
System.out.println(i); // Prints 0 to 4
i++;
```
---
In Java, functions are called **methods**, and they are defined inside classes.
```java
System.out.println("Hello, world!");
}
// Method with a return value
return a + b;
System.out.println(result); // Output: 15
```
---
```java
// Fields (attributes)
String make;
String model;
int year;
// Method (behavior)
System.out.println("Engine started");
```
```java
myCar.model = "Camry";
myCar.year = 2022;
myCar.startEngine();
```
---
```java
```
```java
System.out.println(numbers[i]); // Prints 1, 2, 3, 4, 5
}
```
---
```java
try {
} catch (ArithmeticException e) {
} finally {
```
- `finally`: Executes code after `try` and `catch`, whether an exception occurred or not.
---
### **10. Conclusion**
Java is a versatile and powerful language used for a wide variety of applications, from web
development to mobile applications. The basic concepts such as variables, loops,
conditionals, functions, and object-oriented principles are fundamental to mastering Java.
As you get comfortable with the basics, you can explore more advanced topics like:
Happy coding! Let me know if you'd like to dive deeper into any of these topics.