**Introduction to Java**
Java is a versatile, object-oriented programming language widely used for building
various types of applications, from web and mobile apps to large-scale enterprise
systems. Here’s an easy and understandable guide to get you started with Java.
**1. Basics of Java**
- **Syntax**: Java syntax is similar to C/C++ but with a simpler object model.
- **Hello World**: The basic structure of a Java program.
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
- **Comments**: Single-line (`//`) and multi-line (`/* ... */`).
**2. Data Types and Variables**
- **Primitive Data Types**: byte, short, int, long, float, double, char, boolean.
- **Variables**: Declare and initialize variables.
```java
int number = 10;
double price = 19.99;
char grade = 'A';
boolean isJavaFun = true;
```
**3. Operators**
- **Arithmetic Operators**: `+`, `-`, `*`, `/`, `%`.
- **Comparison Operators**: `==`, `!=`, `>`, `<`, `>=`, `<=`.
- **Logical Operators**: `&&`, `||`, `!`.
**4. Control Statements**
- **If-Else Statement**:
```java
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
```
- **Switch Statement**:
```java
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
, // code
}
```
- **Loops**:
- **For Loop**:
```java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
```
- **While Loop**:
```java
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
```
**5. Arrays**
- **Declaration and Initialization**:
```java
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[5];
```
- **Accessing Elements**:
```java
int firstNumber = numbers[0];
numbers[1] = 10;
```
**6. Object-Oriented Programming (OOP)**
- **Classes and Objects**:
```java
public class Car {
String color;
int year;
void displayInfo() {
System.out.println("Color: " + color + ", Year: " + year);
}
}
Car myCar = new Car();
myCar.color = "Red";
myCar.year = 2020;
myCar.displayInfo();
```
- **Constructors**:
```java
public class Car {
String color;
int year;
// Constructor
Java is a versatile, object-oriented programming language widely used for building
various types of applications, from web and mobile apps to large-scale enterprise
systems. Here’s an easy and understandable guide to get you started with Java.
**1. Basics of Java**
- **Syntax**: Java syntax is similar to C/C++ but with a simpler object model.
- **Hello World**: The basic structure of a Java program.
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
- **Comments**: Single-line (`//`) and multi-line (`/* ... */`).
**2. Data Types and Variables**
- **Primitive Data Types**: byte, short, int, long, float, double, char, boolean.
- **Variables**: Declare and initialize variables.
```java
int number = 10;
double price = 19.99;
char grade = 'A';
boolean isJavaFun = true;
```
**3. Operators**
- **Arithmetic Operators**: `+`, `-`, `*`, `/`, `%`.
- **Comparison Operators**: `==`, `!=`, `>`, `<`, `>=`, `<=`.
- **Logical Operators**: `&&`, `||`, `!`.
**4. Control Statements**
- **If-Else Statement**:
```java
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
```
- **Switch Statement**:
```java
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
, // code
}
```
- **Loops**:
- **For Loop**:
```java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
```
- **While Loop**:
```java
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
```
**5. Arrays**
- **Declaration and Initialization**:
```java
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[5];
```
- **Accessing Elements**:
```java
int firstNumber = numbers[0];
numbers[1] = 10;
```
**6. Object-Oriented Programming (OOP)**
- **Classes and Objects**:
```java
public class Car {
String color;
int year;
void displayInfo() {
System.out.println("Color: " + color + ", Year: " + year);
}
}
Car myCar = new Car();
myCar.color = "Red";
myCar.year = 2020;
myCar.displayInfo();
```
- **Constructors**:
```java
public class Car {
String color;
int year;
// Constructor