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).
// Example: Your first Java program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java Programming!");
}
}
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.
# Example: Compiling and running a Java program
javac Main.java
java 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
.
// Example: Declaring variables
public class Main {
public static void main(String[] args) {
int age = 25;
double height = 5.9;
String name = "Alice";
boolean isStudent = true;
System.out.println("Name: " + name + ", Age: " + age + ", Height: " + height);
}
}
Control Flow
Java supports if
, else
, switch
, and loops (for
, while
, do-while
) for controlling program flow.
// Example: Conditional statement
public class Main {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
}
}
Functions (Methods)
Functions in Java are called methods and are defined within classes. Methods can return values or perform actions.
// Example: Method
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 10);
System.out.println("Result: " + result); // 15
}
}
Arrays and Collections
Java provides arrays and collections like ArrayList
, HashMap
, and HashSet
for storing and manipulating data.
// Example: ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int number : numbers) {
System.out.println("Number: " + number);
}
}
}
Object-Oriented Programming
Java is an object-oriented language, supporting classes, inheritance, polymorphism, and encapsulation.
// Example: Class and Object
class Person {
String name;
int age;
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = "Alice";
person.age = 25;
person.display();
}
}
Exception Handling
Java uses try
, catch
, and finally
blocks to handle exceptions gracefully.
// Example: Exception handling
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Execution complete.");
}
}
}
File I/O
Java provides classes like FileReader
, FileWriter
, and BufferedReader
for reading from and writing to files.
// Example: Reading from a file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
Multithreading
Java supports multithreading, allowing you to run multiple threads concurrently for better performance.
// Example: Thread
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}