Control Structures and Functions

Welcome to our lesson on Control Structures and Functions in Java! As a JavaScript developer, you’ll find many familiar concepts here, but with some important differences in syntax and functionality. Let’s dive in and explore how Java handles these fundamental programming constructs.

If/Else Statements

In Java, if/else statements work similarly to JavaScript, but with a key difference: the condition must always be enclosed in parentheses.

// Java
if (condition) {
    // code block
} else if (anotherCondition) {
    // code block
} else {
    // code block
}
// JavaScript
if (condition) {
  // code block
} else if (anotherCondition) {
  // code block
} else {
  // code block
}

As you can see, the structure is identical, making this an easy transition for JavaScript developers.

Switch Cases

Switch cases in Java are also similar to JavaScript, but with stricter type checking:

// Java
switch (variable) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // code block
}

In Java, the switch statement can be used with byte, short, char, int, enum types, String (since Java 7), and will work with the wrapper classes Byte, Short, Character, and Integer through auto-unboxing.

Loops

Java offers several types of loops, most of which will be familiar to JavaScript developers:

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++;
}

Do-While Loop

// Java
int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 10);

Enhanced For-Loop (For-Each)

Java also offers an enhanced for-loop, similar to JavaScript’s for...of loop:

// Java
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}

Methods (Functions)

In Java, functions are called methods and must be defined within a class. Here’s how you declare a method in Java:

public class MyClass {
    public static void myMethod(String param) {
        System.out.println("Hello, " + param);
    }
}

To call this method:

MyClass.myMethod("World");

Key differences from JavaScript:

  1. Methods must specify a return type (or void if they don’t return anything).
  2. Parameters must have their types declared.
  3. Methods are called on objects or classes (for static methods).

Method Overloading

Java supports method overloading, which allows multiple methods with the same name but different parameters. This concept doesn’t exist in JavaScript:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

Static vs Instance Methods

In Java, similar to JavaScript, methods can be static (belonging to the class) or instance (belonging to an object):

public class MyClass {
    public static void staticMethod() {
        System.out.println("This is a static method");
    }

    public void instanceMethod() {
        System.out.println("This is an instance method");
    }
}

Static methods are called on the class:

MyClass.staticMethod();

Instance methods are called on an object:

MyClass obj = new MyClass();
obj.instanceMethod();

So, this works just like in JavaScript.

Conclusion

In this lesson, we’ve explored how Java handles control structures and functions (methods). While many concepts are similar to JavaScript, Java’s static typing and class-based structure introduce some key differences. Practice writing these structures in Java to solidify your understanding.

Next, we’ll delve into Object-Oriented Programming in Java, where you’ll learn how Java’s approach to classes and objects compares to JavaScript’s prototype-based system. Get ready to explore inheritance, interfaces, and the power of Java’s OOP features!