Var keyword in Java

My 2 cents to correct the question and answers:

  1. The var is NOT a Java keyword. It’s a reserved type name. It seems not a big difference but in fact, it IS:

    var var = 0;
    

    Here var is a variable name too, so the var can be used as a type name, but there is no restriction like for regular keyword (i.e. we can have a variable named var too).

  2. The actual type of the variable IS decided by the Java compiler on the line where the variable is declared, at compile-time, but the type is NOT actually the exact implementation you use on the right side of the expression. See this code:

    var i = true ? Integer.valueOf(1) : "ABC";
    

    The Java compiler needs to pick a type for variable i which will satisfy both branches. It could be a) Object, b) Serializable, c) Comparable, or combination, or all three. We don’t care and don’t know.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)