Looking at Section 14.4 of the old Java SE 16 JLS, I was thinking no, it is a conditional keyword that tells us the compiler will infer the actual type of a local variable if the declaration follows certain rules, otherwise it is a compiler error.
I was reviewing (IMHO) the Worst, Nastiest Chapter of the whole OCJP book: the one on control flow and switch statements (There are problems on there that I can't do in less than 6 or 7 minutes, which would be suicide to even attempt on a real graded timed exam, but I digress).
If the LocalVariableType is var, then let T be the type of the initializer expression
when treated as if it did not appear in an assignment context, and were thus
a standalone expression (§15.2). The type of the local variable is the upward
projection of T with respect to all synthetic type variables mentioned by T
I did poorly enough anyway (barely passed, with way over the time) but was incensed over the selected answer for this question:
Which of the following data types can be used in a switch statement?
I had no problem with enum, int, Byte, String and Char being obviously correct choices and long being obviously invalid.
The problem is since this is a Java 11 mock test, they felt the need to say that:
var is a data type that can be used in a switch statement.
My brain was so frazzled at this point that I went back and forth three times, decided it was a trick because var isn't actually a real data type, and moved on.
The question wanted us to say "Yes" for var because:
Finally, switch supports var if the type can be resolved to a supported switch data types, making option G correct.
Does this anything mean even?
I'm really just grumpy because the questions from that chapter reinforce negative cartoon stereotypes of what programming is about, that some people form in junior high school or high school and never get past, but "Hey, do we say var is a data type or not?"
I am thinking no, and that question was...possibly sillier than the others from that chapter [not blaming the authors, there is a Dark Side to the exams, which is probably appropriate to be there, but it is Not Fun]
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
I agree - var is not a data type, and referring to it this way creates a confusing distraction from the intended point of the question. If they had asked, "which of the following keywords can be used to declare a variable that will be used in a switch statement", then their answers would be fine. An unfortunate choice of wording.
Always! Wait. Never. Shut up. Look at this tiny ad.