Variables in Fortress

In Java, variables are explicitly declared and used by the compiler to check type-correctness of method calls.

public class Variables {
    public static void main(String[] args) {
        // String declares a variable of type String
        String a = "initial";
        System.out.println(a);

        // You can declare multiple variables at once
        int b = 1, c = 2;
        System.out.println(b + " " + c);

        // Java will infer the type of initialized variables using var (Java 10+)
        var d = true;
        System.out.println(d);

        // Variables declared without initialization are given default values
        // For example, the default value for an int is 0
        int e;
        System.out.println(e);

        // In Java, we don't have a direct equivalent to Go's := syntax
        // But we can use var for type inference in local variables (Java 10+)
        var f = "apple";
        System.out.println(f);
    }
}

To run the program:

$ javac Variables.java
$ java Variables
initial
1 2
true
0
apple

In Java, variables are declared with a specific type, or using the var keyword for local variable type inference (introduced in Java 10). Unlike Go, Java doesn’t have a shorthand syntax like := for declaring and initializing variables.

The var keyword in Java is similar to Go’s type inference, but it’s only available for local variables within methods. For class-level variables, you still need to explicitly declare the type.

Java also has the concept of default values for variables. When a variable is declared but not initialized, it’s given a default value based on its type. For example, int variables default to 0, boolean to false, and object references to null.

Remember that in Java, all code must be inside a class, and the entry point of the program is the main method within a class.