Variables in Squirrel

Variables in Java are explicitly declared and used by the compiler to check type-correctness of method calls, among other things.

public class Variables {
    public static void main(String[] args) {
        // String variables can be declared and initialized
        String a = "initial";
        System.out.println(a);

        // You can declare multiple variables of the same type 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);
    }
}

When you run this program, you’ll see:

initial
1 2
true
0
apple

In Java:

  1. Variables are declared using their type (like String, int, boolean) or using var for local type inference (Java 10+).

  2. Multiple variables of the same type can be declared and initialized in one line.

  3. Java has type inference for local variables using the var keyword (introduced in Java 10). This is similar to Go’s type inference, but only works for local variables.

  4. Variables declared without initialization are given default values. For example, int is initialized to 0, boolean to false, and object references to null.

  5. Java doesn’t have a direct equivalent to Go’s := syntax. However, the var keyword (Java 10+) provides similar functionality for local variables.

  6. In Java, all code must be inside a class, and the entry point of a program is the main method.

Remember that Java is a statically-typed language, so once a variable is declared, its type cannot be changed. Also, Java requires semicolons at the end of statements, unlike Go.