• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Is var actually considered a data type or not?

 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
(§4.10.5).



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]
 
Master Rancher
Posts: 4002
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic