Cari Halaman

Dasar Java

Sarah Johnson
1 January 0001
4-5 jam

Dasar Java - Memulai dengan Java

Java adalah bahasa pemrograman yang powerful dan object-oriented. Mari kita mulai dengan dasar-dasar Java.

Apa itu Java?

Java adalah bahasa pemrograman high-level, object-oriented, dan platform-independent yang dikembangkan oleh Sun Microsystems (sekarang Oracle).

Keunggulan Java

  • Platform Independent: Write Once, Run Anywhere (WORA)
  • Object-Oriented: Mendukung OOP principles
  • Robust: Strong type checking dan exception handling
  • Secure: Built-in security features
  • Multithreaded: Support untuk concurrent programming
  • Large Ecosystem: Rich library dan framework
  • Enterprise Ready: Ideal untuk aplikasi enterprise

Setup Development Environment

1. Install Java Development Kit (JDK)

1
2
3
4
5
6
7
# Download JDK dari Oracle atau OpenJDK
# https://www.oracle.com/java/technologies/downloads/
# https://adoptium.net/

# Verify installation
java -version
javac -version

2. Set Environment Variables

1
2
3
4
5
6
7
# Windows
set JAVA_HOME=C:\Program Files\Java\jdk-17
set PATH=%JAVA_HOME%\bin;%PATH%

# macOS/Linux
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
  • IntelliJ IDEA: Professional IDE
  • Eclipse: Free, powerful IDE
  • VS Code: Lightweight dengan extensions
  • NetBeans: Free IDE dari Oracle

Your First Java Program

1. Hello World

1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

2. Compile and Run

1
2
3
4
5
# Compile
javac HelloWorld.java

# Run
java HelloWorld

Variables and Data Types

1. Primitive Data Types

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class DataTypes {
    public static void main(String[] args) {
        // Integer types
        byte byteNum = 127;           // 8-bit
        short shortNum = 32767;       // 16-bit
        int intNum = 2147483647;      // 32-bit
        long longNum = 9223372036854775807L; // 64-bit
        
        // Floating-point types
        float floatNum = 3.14f;       // 32-bit
        double doubleNum = 3.14159265359; // 64-bit
        
        // Character type
        char charVal = 'A';           // 16-bit Unicode
        
        // Boolean type
        boolean boolVal = true;       // true or false
        
        System.out.println("Byte: " + byteNum);
        System.out.println("Int: " + intNum);
        System.out.println("Double: " + doubleNum);
        System.out.println("Char: " + charVal);
        System.out.println("Boolean: " + boolVal);
    }
}

2. Reference Data Types

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ReferenceTypes {
    public static void main(String[] args) {
        // String
        String message = "Hello Java!";
        
        // Arrays
        int[] numbers = {1, 2, 3, 4, 5};
        
        // Objects
        Person person = new Person("John", 25);
        
        System.out.println("Message: " + message);
        System.out.println("First number: " + numbers[0]);
        System.out.println("Person: " + person.getName());
    }
}

class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
}

Operators

1. Arithmetic Operators

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Operators {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        
        System.out.println("Addition: " + (a + b));        // 13
        System.out.println("Subtraction: " + (a - b));     // 7
        System.out.println("Multiplication: " + (a * b));  // 30
        System.out.println("Division: " + (a / b));        // 3
        System.out.println("Modulus: " + (a % b));         // 1
        
        // Increment/Decrement
        int x = 5;
        System.out.println("x++: " + x++);  // 5 (post-increment)
        System.out.println("x: " + x);      // 6
        System.out.println("++x: " + ++x);  // 7 (pre-increment)
    }
}

2. Comparison Operators

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class ComparisonOperators {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        
        System.out.println("a == b: " + (a == b));  // false
        System.out.println("a != b: " + (a != b));  // true
        System.out.println("a > b: " + (a > b));    // true
        System.out.println("a < b: " + (a < b));    // false
        System.out.println("a >= b: " + (a >= b));  // true
        System.out.println("a <= b: " + (a <= b));  // false
    }
}

3. Logical Operators

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class LogicalOperators {
    public static void main(String[] args) {
        boolean x = true;
        boolean y = false;
        
        System.out.println("x && y: " + (x && y));  // false (AND)
        System.out.println("x || y: " + (x || y));  // true (OR)
        System.out.println("!x: " + (!x));          // false (NOT)
    }
}

Control Structures

1. Conditional Statements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Conditionals {
    public static void main(String[] args) {
        int age = 18;
        
        // if-else
        if (age >= 18) {
            System.out.println("You are an adult");
        } else if (age >= 13) {
            System.out.println("You are a teenager");
        } else {
            System.out.println("You are a child");
        }
        
        // switch statement
        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("Other day");
        }
        
        // Ternary operator
        String status = (age >= 18) ? "adult" : "minor";
        System.out.println("Status: " + status);
    }
}

2. Loops

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Loops {
    public static void main(String[] args) {
        // for loop
        System.out.println("For loop:");
        for (int i = 0; i < 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
        
        // while loop
        System.out.println("While loop:");
        int j = 0;
        while (j < 5) {
            System.out.print(j + " ");
            j++;
        }
        System.out.println();
        
        // do-while loop
        System.out.println("Do-while loop:");
        int k = 0;
        do {
            System.out.print(k + " ");
            k++;
        } while (k < 5);
        System.out.println();
        
        // for-each loop
        System.out.println("For-each loop:");
        int[] numbers = {1, 2, 3, 4, 5};
        for (int num : numbers) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

Arrays

1. Array Declaration and Initialization

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Arrays {
    public static void main(String[] args) {
        // Array declaration
        int[] numbers = new int[5];
        
        // Array initialization
        numbers[0] = 1;
        numbers[1] = 2;
        numbers[2] = 3;
        numbers[3] = 4;
        numbers[4] = 5;
        
        // Array literal
        int[] scores = {85, 90, 78, 92, 88};
        
        // Accessing elements
        System.out.println("First number: " + numbers[0]);
        System.out.println("Array length: " + numbers.length);
        
        // Iterating through array
        System.out.println("All numbers:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println();
        
        // For-each loop
        System.out.println("All scores:");
        for (int score : scores) {
            System.out.print(score + " ");
        }
        System.out.println();
    }
}

2. Multi-dimensional Arrays

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MultiDimensionalArrays {
    public static void main(String[] args) {
        // 2D array
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        // Accessing elements
        System.out.println("Element at [1][1]: " + matrix[1][1]);
        
        // Iterating through 2D array
        System.out.println("Matrix:");
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Methods

1. Method Declaration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Methods {
    public static void main(String[] args) {
        // Calling methods
        greet("John");
        int sum = add(5, 3);
        System.out.println("Sum: " + sum);
        
        // Method with return value
        int factorial = calculateFactorial(5);
        System.out.println("Factorial of 5: " + factorial);
    }
    
    // Method without return value
    public static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
    
    // Method with return value
    public static int add(int a, int b) {
        return a + b;
    }
    
    // Method with multiple parameters
    public static int calculateFactorial(int n) {
        if (n <= 1) {
            return 1;
        }
        return n * calculateFactorial(n - 1);
    }
}

2. Method Overloading

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class MethodOverloading {
    public static void main(String[] args) {
        System.out.println(add(5, 3));           // 8
        System.out.println(add(5, 3, 2));       // 10
        System.out.println(add(5.5, 3.5));      // 9.0
    }
    
    // Overloaded methods
    public static int add(int a, int b) {
        return a + b;
    }
    
    public static int add(int a, int b, int c) {
        return a + b + c;
    }
    
    public static double add(double a, double b) {
        return a + b;
    }
}

Object-Oriented Programming

1. Classes and Objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class ClassesAndObjects {
    public static void main(String[] args) {
        // Creating objects
        Car car1 = new Car("Toyota", "Camry", 2020);
        Car car2 = new Car("Honda", "Civic", 2021);
        
        // Calling methods
        car1.displayInfo();
        car2.displayInfo();
        
        car1.start();
        car2.start();
    }
}

class Car {
    // Instance variables
    private String brand;
    private String model;
    private int year;
    
    // Constructor
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }
    
    // Methods
    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
    
    public void start() {
        System.out.println(brand + " " + model + " is starting...");
    }
    
    // Getters and setters
    public String getBrand() {
        return brand;
    }
    
    public void setBrand(String brand) {
        this.brand = brand;
    }
}

2. Inheritance

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Inheritance {
    public static void main(String[] args) {
        Animal animal = new Animal("Generic Animal");
        Dog dog = new Dog("Buddy", "Golden Retriever");
        
        animal.makeSound();
        dog.makeSound();
        dog.fetch();
    }
}

class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public void makeSound() {
        System.out.println(name + " makes a sound");
    }
}

class Dog extends Animal {
    private String breed;
    
    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " barks");
    }
    
    public void fetch() {
        System.out.println(name + " fetches the ball");
    }
}

Exception Handling

1. Try-Catch Blocks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class ExceptionHandling {
    public static void main(String[] args) {
        try {
            int result = divide(10, 0);
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("General error: " + e.getMessage());
        } finally {
            System.out.println("This always executes");
        }
    }
    
    public static int divide(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException("Division by zero");
        }
        return a / b;
    }
}

Next Steps

Di modul selanjutnya, kita akan mempelajari:

  • Advanced OOP concepts (Polymorphism, Abstraction)
  • Collections Framework
  • File I/O
  • Multithreading
  • Java Web Development
  • Spring Framework

Mari lanjutkan ke modul berikutnya!

Modul Sebelumnya
Modul Selanjutnya

Progress Seri

Lanjutkan pembelajaran Anda

1/13
Modul
8%
Sedang Berjalan