Java by Example: Variables

In Java, variables are explicitly declared with specific data types.

public class Main
public static void main(String[] args){

Variables in Java must be declared before use, with their data type specified.

    String a = "initial";
    System.out.println(a);

You can declare multiple variables of the same type at once.

    int b, c;
    b = 1;
    c = 2;
    System.out.println(b + " " + c);

Java requires explicit type declaration for all variables.

    boolean d = true;
    System.out.println(d);

Variables declared without a corresponding initialization are zero-valued or null-valued depending on their types.

    int e;
    System.out.println(e);

In Java, there is no shorthand for declaring and initializing a variable. Each step must be done explicitly.

    String f;
    f = "apple";
    System.out.println(f);
}
Output:
initial
1 2
true
0
apple

Next example: Constants.

/