- Java Introduction
- Java Getting Started
- Java Syntax
- Java Comments
- Java Variables
- Java Data Types
- Java Type Casting
- Java Operators
- Java Strings
- Java Math
- Java Booleans
- Java If ... Else
- Java Switch
- Java While Loop
- Java For Loop
- Java Break And Continue
- Java Arrays
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Scope
- Java Recursion
- Java OOP
- Java Classes And Objects
- Java Class Attributes
- Java Class Methods
- Java Constructors
- Java Modifiers
- Java Encapsulation
- Java Packages
- Java Inheritance
- Java Polymorphism
- Java Inner Classes
- Java Abstraction
- Java Interface
- Java Enums
- Java User Input (Scanner)
- Java Date And Time
- Java ArrayList
- Java LinkedList
- Java HashMap
- Java HashSet
- Java Iterator
- Java Wrapper Classes
- Java Exceptions - Try...Catch
- Java Regular Expressions
- Java Threads
- Java Lambda Expressions
- Java Files
- Java Create And Write To Files
- Java Read Files
- Java Delete Files
Java Comments
Java Comments
Explanation. Comments are used to explain the purpose of code, its functionality, or any complex logic. They provide insights into the code's intention, making it easier for other developers (or your future self) to understand the code.
int sum = num1 + num2;
Readability: Comments can improve code readability by breaking down complex algorithms or explaining tricky parts of the code.
// Check if the user is eligible for voting
// If age is 18 or above, the user is eligible
System.out.println("You are eligible to vote.");
}
Documentation: Comments serve as documentation for the codebase, helping developers understand how to use classes, methods, or functions
* This method calculates the factorial of a given number.
* @param n The number for which the factorial is calculated.
* @return The factorial of the given number.
*/
public static int factorial(int n) {
// Implementation logic here
}
Practice Excercise Practice now
Single-line Comments
- Single-line comments begin with
//
and continue until the end of the line. They are used to add brief explanations or annotations to specific lines of code. - Single-line comments are ideal for providing short, concise explanations directly alongside the code they describe.
Example
// This is a comment
System.out.println("Hello World");
This example uses a single-line comment at the end of a line of code:
Example
System.out.println("Hello World"); // This is a comment
Practice Excercise Practice now
Java Multi-line Comments
- Multi-line comments begin with
/*
and end with*/
. They can span multiple lines and are often used for longer explanations, documentation, or temporarily disabling code blocks. - Multi-line comments are suitable for providing detailed descriptions of methods, classes, or sections of code.
This example uses a multi-line comment (a comment block) to explain the code:
Example
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
Single or multi-line comments?
It is up to you which you want to use. Normally, we use //
for short comments, and /* */
for longer.
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP