Quiz: Variable Declarations: The Fine Print
In your answers, use the letters a - g (without parentheses) for the following descriptions:
a) variable names can only contain letters and numbers
b) it's always a good idea to include an initial value
c) variable type doesn't match initial value type
d) variable has a bad type
e) no mistake
f) variable declarations need a semicolon
g) variable declarations must have types
var x = 13; |
d | This was an error in 2013 when this course was produced. As of Java 10 in 2018, you can use var with variable declarations. Then the compiler figures out the correct type—in this case, int —from the initial value. |
int x = "13"; |
c | x is an int , but "13" is a String . |
double x = 13.0 |
f | Always put a ; after a variable declaration! |
int lucky number = 13; |
a | You cannot have a space in a variable name. |
x = 13; |
g | The type int is missing. |
int x; |
b | This is not an error, but it is a good idea to initialize variables when you declare them. |
int luckyNumber = 3; |
e | This variable declaration is practically perfect in every way. |