What are the valid characters for a Java method name?
Solution 1: Yes, method names and variable names are what’s called «identifiers». Identifiers all share the same rules regarding accepted characters.
What are the valid characters for a Java method name?
Yes, method names and variable names are what’s called «identifiers». Identifiers all share the same rules regarding accepted characters. Take a look at §3.8 from the Java Language Specification to find out exactly what an identifier may contain, and §6.2 for an explanation about how identifiers are used.
You might be surprised when having unusual characters for method, such as:
public void mój_brzuch_zacznie_burczeć()
and it works pretty nice. Take a look at this blog to see more fancy examples.
«Although a method name can be any legal identifier, code conventions restrict method names.» http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Java Valid identifier, Your answer: a. that. In java you are not allowed to use keywords as identifiers. this. is a keyword for pointing to current object. int. is a keyword for primitive integer type. double. is a keyword for primitive double type.
How to check if string is a valid class identifier?
If you really want a bullet-proof solution, split the string by «\\.» and use first
Character.isJavaIdentifierStart
Character.isJavaIdentifierPart
to check that the parts are valid identifier names.
Edit: #split(String) interprets the string as regex, so make sure you split the String by «\\.» rather than just «.» as you’d expect by the principle of least surprise. What a bad API design, yikes! That’s what you get from not having regex literals in your language…
The proper way to do this is to use javax.lang.model.SourceVersion.isName method (part of Java standard library).
Different versions of the Java platform support different Unicode versions.
So different versions will support different valid identifiers.
The following accounts for identifiers outside the BMP, and Java reserved words.
import java.util.Arrays; public enum PackageName < SIMPLE, QUALIFIED, INVALID; public static final PackageName check(String name) < PackageName ret = PackageName.INVALID; int[] codePoint; int index = 0, dotex = -1; boolean needStart = true; escape: < if(name == null || name.isEmpty()) break escape; if(name.codePointAt(0) == '.') break escape; codePoint = name.codePoints().toArray(); while (index int start = dotex + 1; int end = index; start = name.offsetByCodePoints(0, start); end = name.offsetByCodePoints(0, end); String test = name.substring(start, end); if(!(Arrays.binarySearch(reserved, test) < 0))< ret = PackageName.INVALID; break escape;>if(!(ret == PackageName.QUALIFIED)) ret = PackageName.SIMPLE; break escape; > if(codePoint[index] == '.') < if(codePoint[index - 1] == '.')< ret = PackageName.INVALID; break escape;>else < needStart = true; int start = dotex + 1; int end = index; start = name.offsetByCodePoints(0, start); end = name.offsetByCodePoints(0, end); String test = name.substring(start, end); if(!(Arrays.binarySearch(reserved, test) < 0)) break escape; dotex = index; ret = PackageName.QUALIFIED; >> else if(Character.isJavaIdentifierStart(codePoint[index])) < if(needStart) needStart = false; >else if((!Character.isJavaIdentifierPart(codePoint[index]))) < ret = PackageName.INVALID; break escape; >index++; > > return ret; > private static final String[] reserved; static < reserved = new String[] < "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "if", "goto", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" >; > >
Java — Which of these are valid variable names?, 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 …
Java environment location is ‘not a valid identifier’
hey Simon the path should be with » « in forward slash or in backward slash without qoute like this :-
export JAVA_HOME="C:\Program Files (x86)\Java\jre7" export JAVA_HOME=/Program Files (x86)/Java/jre7 #(as the root is C drive)
and in hadoop installation it is strictly mentioned that you have to set your JAVA path to
export JAVA_HOME="C:\Java\jre8"
that should not be inside the program files.
export JAVA_HOME=»C:\Program Files (x86)\Java\jre7″ or export JAVA_HOME=C:\Program Files (x86)\Java\jre7 will work.
Make sure that don’t give space between = and path export JAVA_HOME = C:\Program Files (x86)\Java\jre7(invalid)
export JAVA_HOME= $ **warn:** must <>
Java Identifiers, All Java variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Note: It is recommended to use descriptive names in order to create understandable and maintainable code:
Valid characters in a Java class name
What characters are valid in a Java class name? What other rules govern Java class names (for instance, Java class names cannot begin with a number)?
possible duplicate of Legal identifiers in Java because docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1 says: class Identifier . This is way older, but the other is way more upvoted and more general: same goes for methods, variables, etc.
That question is specific to the starting character in an identifier name. This answer to this question refers to a different part of the java spec: isJavaIdentifierPart .
8 Answers 8
You can have almost any character, including most Unicode characters! The exact definition is in the Java Language Specification under section 3.8: Identifiers.
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. .
Letters and digits may be drawn from the entire Unicode character set, . This allows programmers to use identifiers in their programs that are written in their native languages.
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
However, see this question for whether or not you should do that.
«. including most Unicode characters» — I always liked this about Java. You can literally use π as a variable name for pi.
hmm, surely some unicode characters are invalid, such as the space character? Unfortunately the docs are rather unclear. Even the JavaDoc for Character.isJavaIdentifierStart (referenced from the JLS) is a bit vague about this, including «connecting punctuation characters» (even though, again, space is presumably excluded even though it’s a connecting punctuation character)
- 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.
- If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6 , the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.
Further to previous answers its worth noting that:
I believe the usage of currency symbols originates in C/C++, where variables added to your code by the compiler conventionally started with ‘$’. An obvious example in Java is the names of ‘.class’ files for inner classes, which by convention have the format ‘Outer$Inner.class’
- Many C# and C++ programmers adopt the convention of placing ‘I’ in front of interfaces (aka pure virtual classes in C++). This is not required, and hence not done, in Java because the implements keyword makes it very clear when something is an interface.
class Employee : public IPayable //C++
class Employee implements Payable //Java
- Many projects use the convention of placing an underscore in front of field names, so that they can readily be distinguished from local variables and parameters e.g.
A tiny minority place the underscore after the field name e.g.
«Many projects use the convention of placing an underscore in front of field names».. which language? Java or C++?
As already stated by Jason Cohen, the Java Language Specification defines what a legal identifier is in section 3.8:
«An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. [. ] A ‘Java letter’ is a character for which the method Character.isJavaIdentifierStart(int) returns true. A ‘Java letter-or-digit’ is a character for which the method Character.isJavaIdentifierPart(int) returns true.»
This hopefully answers your second question. Regarding your first question; I’ve been taught both by teachers and (as far as I can remember) Java compilers that a Java class name should be an identifier that begins with a capital letter A-Z, but I can’t find any reliable source on this. When trying it out with OpenJDK there are no warnings when beginning class names with lower-case letters or even a $-sign. When using a $-sign, you do have to escape it if you compile from a bash shell, however.