Lesson 1: Java Basics
1.1 Introduction to Java
Java is a powerful, object-oriented programming language widely used in web development, mobile applications (Android), and enterprise systems. It is known for its platform independence and robustness.
1.2 Why Learn Java?
- Platform Independence: Java programs can run on any device with the Java Virtual Machine (JVM).
- Object-Oriented: Java is built around objects and classes, making it modular and reusable.
- Large Community: Extensive libraries and frameworks are available.
- High Demand: Java is widely used in enterprise applications and Android development.
Writing Your First Java Program
Create a file named HelloWorld.java
and write the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compile and run the program:
javac HelloWorld.java
java HelloWorld
This prints Hello, World! to the screen.
1.4 Understanding Variables and Data Types
Variables: A variable stores a value that can change.
String name = "Alice";
int age = 25;
System.out.println(name + " is " + age + " years old.");
Data Types:
- int: Whole numbers (e.g.,
10
,-5
). - double: Decimal numbers (e.g.,
3.14
,-0.5
). - String: Text (e.g.,
"Hello"
). - boolean:
true
orfalse
.
1.5 Basic Operations
Java supports basic mathematical operations:
int x = 10;
int y = 5;
System.out.println(x + y); // Addition
System.out.println(x - y); // Subtraction
System.out.println(x * y); // Multiplication
System.out.println(x / y); // Division