4

Fun Java test:

Without looking up anything, answer the following question:

Which of the following variable identifiers is legal in Java and why?

1) float $50.00;
2) float $50_00;
3) float 50;
4) float else;

Comments
  • 6
    I'd say 2)
    Underscore is legal and I think period is not. And the $ character is used internally for anonymous classes and the like so I'd bet that is legal too.
  • 3
    @cafecortado Well done, well done... Yes, 2) is correct. And indeed, period is not legal, while underscore and $ are.
  • 5
    Came here to make the same guess as @cafecortado

    It makes sense that . is not legal, otherwise it would conflict with the member access syntax.
  • 2
    @Lensflare Thanks for participating. Well spotted.
  • 0
    This is for the OCA by the way. lmao
  • 1
    I thought it was 2 because you can't start with a number, and the $ makes it not start with a number. I forgot about anonymous inner-classes as mentioned by others. This just creates a variable named "$50_00," right? Nothing special about it?
  • 0
    JS (browser) equivalent:

    window["$50.00"] = "JavaScript"

    window["$50_00"] = "likes"

    window["50"] = "it"

    window["else"] = "rough"

    1-4 are valid and you can even reference #2 without PO(JS)O notation.
  • 2
    @AlgoRythm 🤨
    Anything is legal if you put it into a string (with escaping)
Add Comment