हिंदी

What are the rules for naming a variable in JAVA?

Advertisements

प्रश्न

What are the rules for naming a variable in JAVA?

विस्तार में उत्तर
Advertisements

उत्तर

  1. Valid Characters: A variable name can contain letters (A–Z, a–z), digits (0–9), underscores (_) and dollar signs ($).
  2. Starting Character: A variable name must begin with a letter, underscore (_) or dollar sign ($). It cannot begin with a digit.
  3. No Spaces: A variable name cannot contain spaces.
  4. No Special Symbols: Special symbols such as @, #, %, & and - are not allowed in variable names.
  5. Case Sensitivity: Java is case-sensitive. Therefore, age, Age and AGE are treated as different variables.
  6. No Reserved Keywords: Java keywords such as int, class, public and static cannot be used as variable names.
  7. 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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 6: Variables and Expressions - EXERCISE [पृष्ठ १३१]

APPEARS IN

धीरज मेहरोत्रा Computer Science [English] Class 12
अध्याय 6 Variables and Expressions
EXERCISE | Q 8. | पृष्ठ १३१
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×