rules for variable names
Title
Question
The tutorial states that variable names can start only with a letter or an underscore.
Any identifier in Java can start with a letter, a seperator(Underscore) or a currency symbol like $ or even \u20b9
letters can also be \u0a85 any letter from the Unicode character set.
Java Numerical-Datatypes 06-07 min 40-50 sec
Answers:
The only allowed characters for java variables names are all alphanumeric characters, any currency symbol and underscore.
Variable names should not start with digits and java keywords can’t be used as variable names.
1. Only allowed characters for java identifiers are a to z, A to Z, 0 to 9, _, $. If we are using any other character, compile time error will be thrown by compiler.<o:p></o:p>
2. Identifiers can’t start with digits.<o:p></o:p>
3. Java identifiers are case sensitive. Java language is considered as case sensitive programming language.<o:p></o:p>
4. There is no length limit for java identifiers, but it is not recommended to take too lengthy identifiers.<o:p></o:p>
5. We can’t use reserved word as identifiers.<o:p></o:p>
6. All predefined java class names and Interface names can be used as identifiers. Ex. String, Runnable. Even though it is valid, it is not good programming practice, as it reduces readability and creates confusion.<o:p></o:p>
Point 1 is incorrect. You can use Letters, Digits, Separator and Currency Symbol from the entire Unicode character set and not just ASCII.
Use of currency symbol \u20b9 indian Rupee symbol is also valid, so are all the letters and digits from all the other languages. eg. \u0905 the devenagari letter A is also valid, you can compile and check.
Java has good support for Unicode characters since the beginning and the size of char data type is 16-bit and not restricted to 8-bits as is the case in C.
Login to add comment
In Java there are a few simple rules for variable names:
Names must start with a letter. Use only letters, digits or the underscore ( _ ) in the rest of the name.
Don’t start with a numbers or use special characters ($,%,@)
Separate words with ‘camelHump’ notation
Variable names are case-sensitive
Don’t use reserved ‘Java’ words
Variables start with lower case, class names with upper case (by convention, not a ‘rule’)
Names must start with a letter. Use only letters, digits or the underscore ( _ ) in the rest of the name.
Don’t start with a numbers or use special characters ($,%,@)
Separate words with ‘camelHump’ notation
Variable names are case-sensitive
Don’t use reserved ‘Java’ words
Variables start with lower case, class names with upper case (by convention, not a ‘rule’)
Login to add comment