type of testing

Search

Total Views

Total Views:

Comments & Identifiers

 

🟩 1. COMMENTS in Java

Comments are used to make code more readable, explain what it does, or temporarily disable code. They are ignored by the Java compiler during execution.


Types of Comments:

1. Single-line Comment (//)

Used for short explanations or notes.

java

// This is a single-line comment int age = 25; // declaring age

2. Multi-line Comment (/* */)

Used when the comment spans multiple lines.

java
/* This is a multi-line comment Used to describe a method or block of code */ int salary = 50000;

3. Documentation Comment (/** */)

Used to generate external documentation (API docs) using tools like javadoc. It’s written before class, method, or interface declarations.

java

/** * This class performs basic arithmetic operations. * @author Swami * @version 1.0 */ public class Calculator { /** * Adds two integers. * @param a First number * @param b Second number * @return Sum of a and b */ public int add(int a, int b) { return a + b; } }

🟨 2. IDENTIFIERS in Java

Identifier Rules in Java:

1. Identifiers can contain only:
     - Letters (A-Z, a-z)
     - Digits (0-9)
     - Special characters: $ (dollar sign) and _ (underscore)

2. Identifiers **cannot start** with a digit.
     - Example: 1name → ❌ Invalid
     - Example: name1 → ✅ Valid

3. Identifiers cannot contain **any special characters** other than $ and _.
     - Example: na#me → ❌ Invalid

4. Identifiers **cannot contain spaces**.
     - If needed, use underscore `_` as a connector.
     - Example: first_name → ✅ Valid

5. Java is **case-sensitive**.
     - Example: `Name` and `name` are different identifiers.

6. Identifiers **cannot be Java keywords or reserved words**.
     - Example: `int`, `class` → ❌ Invalid
     - But you can use predefined class/variable/method names, if referred properly using packages.

7. From Java 9 onwards, a single underscore `_` **is not allowed** as an identifier as it became a keyword.

Note:
- There's **no limit** on identifier length in Java (unlike C).
- But using long identifiers is **not recommended** for readability and maintainability.
*/

// Java program demonstrating Identifier Rules
public class IdentifierRulesDemo {

    public static void main(String[] args) {

        // Valid identifiers
        int _age = 25;
        int $salary = 50000;
        int first_name = 10;
        int age1 = 30;
        int Age = 35; // Different from age due to case sensitivity

        // Invalid identifiers (Uncommenting will cause compile-time errors)
        // int 1number = 100;       // ❌ Cannot start with digit
        // int first name = 20;     // ❌ Cannot contain space
        // int na#me = 50;          // ❌ Cannot contain special character #
        // int int = 60;            // ❌ Cannot use reserved keyword
        // int _ = 5;               // ❌ Invalid from Java 9 onwards

        System.out.println("All valid identifiers compiled and executed successfully!");
    }
}


An identifier is the name used to identify:

  • Variables

  • Methods

  • Classes

  • Packages

  • Interfaces


Rules for Naming Identifiers:

RuleDescriptionExample
1. Allowed charactersLetters (A-Z, a-z), digits (0–9), _ and $ onlystudentName, roll_no, $_count
2. Must not start with a digitCannot begin with 0–91name
3. No special charactersExcept _ and $, no @, #, ! etc.user#id
4. No spacesUse _ to separate wordsfirst name ❌ → first_name
5. Case-sensitiveage and Age are differentscore, Score → separate identifiers
6. Cannot use keywordse.g. int, class, public, new etc.int = 5;
7. _ is reserved from Java 9+Single underscore cannot be used aloneint _ = 5;

🔷 Examples of Valid Identifiers:

java
int age; String userName; double $amount; int _counter;

🔴 Examples of Invalid Identifiers:

java
// int 2value; // Starts with digit ❌ // int first name; // Contains space ❌ // int class; // Java keyword ❌ // int user@id; // Special character ❌ // int _; // Single underscore (Java 9+) ❌

📘 Best Practices for Naming Identifiers

ElementConventionExample
ClassPascalCaseStudentInfo, BankAccount
MethodcamelCasegetDetails(), calculateTotal()
VariablecamelCasetotalMarks, isVerified
ConstantUPPER_SNAKE_CASEMAX_LIMIT, PI_VALUE
InterfacePascalCaseRunnable, Serializable
Packagelowercase with dotscom.company.project

🔁 Comparison Summary

FeatureCommentsIdentifiers
PurposeExplain or document codeName programming elements like variables
TypesSingle-line, Multi-line, JavadocVariable, Method, Class, Interface names
CompilerIgnoredUsed in compilation
Syntax//, /* */, /** */Custom names following rules
VisibilityNot part of code logicDirectly affects program behavior

Comments

🌟🌟🌟🌟☆ Great platform with helpful onboarding! Some areas could be streamlined, but overall a very positive experience.