Introduction to Programming in Java
Java is one of the most popular and widely-used programming languages in the world. Known for its platform independence, robustness, and object-oriented design, Java is used for building web applications, mobile apps (Android), enterprise systems, and more. Its "write once, run anywhere" philosophy makes it a versatile choice for developers.
This guide will introduce you to the fundamentals of Java programming, helping you write clean, efficient, and maintainable code.
Table of Contents
- What is Java?
- Setting Up Your Environment
- Variables and Data Types
- Control Flow
- Functions (Methods)
- Arrays and Collections
- Object-Oriented Programming
- Exception Handling
- File I/O
- Multithreading
What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent, meaning Java programs can run on any device with a Java Virtual Machine (JVM).
1// Example: Your first Java program
2public class Main {
3 public static void main(String[] args) {
4 System.out.println("Hello, Java Programming!");
5 }
6}
Setting Up Your Environment
To write and run Java programs, you need the Java Development Kit (JDK) installed on your system. You can download it from the official Oracle website.
1# Example: Compiling and running a Java program
2javac Main.java
3java Main
Variables and Data Types
Java is statically typed, meaning variable types are determined at compile time. Common data types include int
, double
, String
, and boolean
.
1// Example: Declaring variables
2public class Main {
3 public static void main(String[] args) {
4 int age = 25;
5 double height = 5.9;
6 String name = "Alice";
7 boolean isStudent = true;
8
9 System.out.println("Name: " + name + ", Age: " + age + ", Height: " + height);
10 }
11}
Control Flow
Java supports if
, else
, switch
, and loops (for
, while
, do-while
) for controlling program flow.
1// Example: Conditional statement
2public class Main {
3 public static void main(String[] args) {
4 int age = 18;
5
6 if (age >= 18) {
7 System.out.println("You are an adult.");
8 } else {
9 System.out.println("You are a minor.");
10 }
11 }
12}
Functions (Methods)
Functions in Java are called methods and are defined within classes. Methods can return values or perform actions.
1// Example: Method
2public class Main {
3 static int add(int a, int b) {
4 return a + b;
5 }
6
7 public static void main(String[] args) {
8 int result = add(5, 10);
9 System.out.println("Result: " + result); // 15
10 }
11}
Arrays and Collections
Java provides arrays and collections like ArrayList
, HashMap
, and HashSet
for storing and manipulating data.
1// Example: ArrayList
2import java.util.ArrayList;
3
4public class Main {
5 public static void main(String[] args) {
6 ArrayList<Integer> numbers = new ArrayList<>();
7 numbers.add(1);
8 numbers.add(2);
9 numbers.add(3);
10
11 for (int number : numbers) {
12 System.out.println("Number: " + number);
13 }
14 }
15}
Object-Oriented Programming
Java is an object-oriented language, supporting classes, inheritance, polymorphism, and encapsulation.
1// Example: Class and Object
2class Person {
3 String name;
4 int age;
5
6 void display() {
7 System.out.println("Name: " + name + ", Age: " + age);
8 }
9}
10
11public class Main {
12 public static void main(String[] args) {
13 Person person = new Person();
14 person.name = "Alice";
15 person.age = 25;
16 person.display();
17 }
18}
Exception Handling
Java uses try
, catch
, and finally
blocks to handle exceptions gracefully.
1// Example: Exception handling
2public class Main {
3 public static void main(String[] args) {
4 try {
5 int result = 10 / 0;
6 } catch (ArithmeticException e) {
7 System.out.println("Error: " + e.getMessage());
8 } finally {
9 System.out.println("Execution complete.");
10 }
11 }
12}
File I/O
Java provides classes like FileReader
, FileWriter
, and BufferedReader
for reading from and writing to files.
1// Example: Reading from a file
2import java.io.File;
3import java.io.FileNotFoundException;
4import java.util.Scanner;
5
6public class Main {
7 public static void main(String[] args) {
8 try {
9 File file = new File("example.txt");
10 Scanner scanner = new Scanner(file);
11 while (scanner.hasNextLine()) {
12 System.out.println(scanner.nextLine());
13 }
14 scanner.close();
15 } catch (FileNotFoundException e) {
16 System.out.println("File not found.");
17 }
18 }
19}
Multithreading
Java supports multithreading, allowing you to run multiple threads concurrently for better performance.
1// Example: Thread
2class MyThread extends Thread {
3 public void run() {
4 System.out.println("Thread is running.");
5 }
6}
7
8public class Main {
9 public static void main(String[] args) {
10 MyThread thread = new MyThread();
11 thread.start();
12 }
13}