-4

This is a question from a Java test I took at University

I. publicProtected

II. $_

III. _identi#ficador

I've. Protected

I'd say I, II, and I've are correct. What is the correct answer for this?

Source of the question in spanish: Teniendo la siguiente lista de identificadores de variables, ¿Cuál (es) es (son) válido (s)?

M--
  • 25,431
  • 8
  • 61
  • 93
Vilches Felipe
  • 53
  • 1
  • 1
  • 4
  • 2
    Why didn't you try it? (and yes, you're correct) – August Dec 07 '14 at 01:37
  • I tried it, but my teacher said it's I and II. What should I tell my teacher? – Vilches Felipe Dec 07 '14 at 01:39
  • 3
    Tell your teacher to read the Java Language Spec, which clearly describes what names are valid variable names. Or show him/her a Java program that works correctly, and contains `Protected` as a variable name. – Dawood ibn Kareem Dec 07 '14 at 01:39
  • @VilchesFelipe Maybe he mistook it for the `protected` keyword. Tell him that keywords are case-sensitive. – August Dec 07 '14 at 01:40
  • It's possible this is one of the teachers who gets "valid" mixed up with "accepted" and wanted everyone to say IV was invalid because typical JAVA code doesn't capitalize the first letter. – Silvio Mayolo Dec 07 '14 at 01:42
  • Why is this so heavily downvoted? I seems like a reasonable question to me. – user4235730 Dec 07 '14 at 01:52
  • They aren't downvoting the question, they are downvoting my teacher. – Vilches Felipe Dec 07 '14 at 01:57
  • possible duplicate of [Legal identifiers in Java](http://stackoverflow.com/questions/11774099/legal-identifiers-in-java) –  Dec 07 '14 at 02:11
  • 2
    They're probably downvoting it because they feel that you could have tried for yourself, to see which variable names actually "work". Or possibly because the answer is so readily available in the JLS. Note that Java is not like HTML - if you write invalid Java, it simply won't compile; whereas browsers tend to do their best to display HTML even if it's invalid. So "does this actually work" is a good way to see whether you're dealing with valid Java or not. – Dawood ibn Kareem Dec 07 '14 at 02:38

2 Answers2

5

From the java documentation:

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted. Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

In short: yes, you're right. You can use underscores, dollarsigns, and characters to start a variable name. After the first letter of the variable name, you can also use numbers. Note that using dollar signs is generally not good practice.

From your comment, you said that your teacher rejected "II". Under your question, II is perfectly fine (try it, it will run). However, if the question on your test asked which are "good" variable names, or which variable names follow common practice, then II would be eliminated as explained in the quotation above. One reason for this is that dollar signs do not make readable variable names; they're included because internally Java makes variables that use the dollar sign.

What is the meaning of $ in a variable name?

As pointed out in the comments, IV is not a good name either, since the lower case version "protected" is a reserved keyword. With syntax highlighting, you probably wouldn't get the two confused, but using keyword-variations as variable names is certainly one way to confuse future readers

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • No, my teacher rejected Protected (IV). I and II were considered correct during the test revision – Vilches Felipe Dec 07 '14 at 01:47
  • Well there's some ambiguity in the term "valid". Valid in terms of Java specification is unambiguous - put it in code, run the code, show her the output, no more question. There's not much debating it, the code will compile and run. "Valid" in her classroom and valid as an answer on that quiz are different, and I can't speak for those, she's free to define them as she pleases :) I'm fairly certain there's no versioning issues here, though I've never used Java 1.1 so I can't say for sure – en_Knight Dec 07 '14 at 01:51
  • Yeah, there are no version issues. The answer to this question is the same in every version of Java - the teacher is simply wrong. (I taught a Java 1.0 course back in the '90s). Unfortunately, the question of how to deal appropriately with a teacher when they make mistakes is beyond the scope of Stack Overflow. – Dawood ibn Kareem Dec 07 '14 at 02:31
0

Private protected public are reserved or keywords in java.. Use _ or to use that those words.. example int public_x; int protected_x; String private_s;