OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Comments, Programming Style

 

 Comments

The contents of a comment are ignored by the compiler. Java supports three styles of comments.

o    Single-line comment    

o   Multi-line comment

o   Documentation comment

Java supports three types of comments. Comments are non-executable lines meant for documentation and readability.

1. Single-line Comment

Used for brief notes or explanations.


// This is a single-line comment int age = 25; // Variable to store age

2. Multi-line Comment

Used for detailed documentation or disabling a block of code.


/* This is a multi-line comment. It can span multiple lines. */ int age = 25;

3. Documentation Comment

Used to generate external documentation using javadoc. Applied above classes, methods, or fields.


/** * This class represents a simple Student. * It holds student's name and roll number. */ public class Student { String name; int rollNo; /** * Displays student details. */ public void displayInfo() { System.out.println(name + " " + rollNo); } }


Programming Style

Java is a free form language. We need not have to indent any lines to make the program work properly. Java system does not care when on the line we begin typing. While this may be a license for bad programming style. We should try to use this fact to our advantage for producing readable programs.

Although several alternate styles are possible, we should select one and try to use it with total consistency.

Practice for good programming style:

ü  Appropriate comments

ü  Naming conventions

ü  Proper indentation and spacing lines

ü  Block styles

For example, the statement   

System.out.println(“Java is Wonderful!”);

can be written as System.out.println (“Java is Wonderful!”);

 or, even as

System. out.println (“Java is Wonderful!”);



A clean and consistent coding style improves readability, maintainability, and collaboration.

1. Naming Conventions

TypeConvention Example
ClassPascalCaseStudentInfo
MethodcamelCasegetDetails()
VariablecamelCasestudentName
ConstantUPPER_CASEMAX_SIZE

2. Indentation and Braces

Use 4 spaces per indentation level (or tabs as per project standard). Always use braces, even for single-line blocks.


if (marks >= 40) { System.out.println("Passed"); }

3. Line Length

Keep lines within 80–100 characters for readability.


4. White Space Usage

Use spaces around operators and after commas:


int total = marks1 + marks2; System.out.println("Total: " + total);

5. Method Structure

Keep methods small and focused. Use meaningful names.


public int calculateSum(int a, int b) { return a + b; }

6. Comment Wisely

Don't over-comment obvious code. Use comments to explain “why,” not “what”:


// Calculates interest based on principal, rate, and time double interest = (principal * rate * time) / 100;

7. Avoid Magic Numbers

Use constants instead of hardcoding numbers:


final int PASS_MARK = 40; if (marks >= PASS_MARK) { System.out.println("Passed"); }

8. Code Grouping

Group related methods together and follow logical order: fields → constructor → methods.


Example with Good Style and Comments:


/** * Calculator class provides basic math operations. */ public class Calculator { // Adds two integers and returns the result public int add(int a, int b) { return a + b; } // Multiplies two integers public int multiply(int x, int y) { return x * y; } public static void main(String[] args) { Calculator calc = new Calculator(); int sum = calc.add(5, 3); int product = calc.multiply(4, 2); System.out.println("Sum: " + sum); System.out.println("Product: " + product); } }

Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC