Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How do I write variable names in Java?
While choosing an identifier to declare a variable in Java you need to keep the following points in mind.
- The name of the variable should begin with either alphabet or, an underscore (_) or, a dollar ($) sign.
- The identifiers used for variables must not be keywords.
- No spaces or special characters are allowed in the variable names of Java.
- Variable names may contain 0 to 9 numbers (if not at the beginning).
- Variable names are case sensitive i.e. MY_NUM is different from my_num.
- If you use two words in a identifier then the you should follow camel case first letter of first word should be in lowercase and the first letters of later words should be in lower case ex: myNum.
Example
Following example shows various possible identifiers used to declare a variable in Java.
public class VariableTest {
public static void main(String args[]) {
// Declaring a variable named num
int num = 1;
int _num = 10;
int $num = 100;
int num123 = 1000;
int NUM = 10000;
int myNum = 100000;
// Printing the value of the variable num
System.out.println("value if the variable num: "+num);
System.out.println("value if the variable _num: "+_num);
System.out.println("value if the variable $num: "+$num);
System.out.println("value if the variable num123: "+num123);
System.out.println("value if the variable NUM: "+NUM);
System.out.println("value if the variable myNum: "+ myNum);
}
}
Output
value if the variable num: 1 value if the variable _num: 10 value if the variable $num: 100 value if the variable num123: 1000 value if the variable NUM: 10000 value if the variable myNum: 100000
Advertisements
