What are the rules for naming a variable in JAVA?
Advertisements
प्रश्न
What are the rules for naming a variable in JAVA?
सविस्तर उत्तर
Advertisements
उत्तर
- Valid Characters: A variable name can contain letters (A–Z, a–z), digits (0–9), underscores (
_) and dollar signs ($). - Starting Character: A variable name must begin with a letter, underscore (
_) or dollar sign ($). It cannot begin with a digit. - No Spaces: A variable name cannot contain spaces.
- No Special Symbols: Special symbols such as
@,#,%,&and-are not allowed in variable names. - Case Sensitivity: Java is case-sensitive. Therefore,
age,AgeandAGEare treated as different variables. - No Reserved Keywords: Java keywords such as
int,class,publicandstaticcannot be used as variable names. - Meaningful Names: Variable names should be meaningful and indicate the purpose of the variable.
Examples:
- Valid Variable Names:
studentName,totalMarks,age1,_count - Invalid Variable Names:
1age,student name,class,total-marks
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
