Video Lecture – 1?

1 - Introduction and Installing the java (JDK) Step by Step Tutorial 2 - Installing Eclipse IDE and Setting up Eclipse 3 - Creating First Java Project in Eclipse IDE

Video Lecture – 2?

4 - Variables and Types in Java 5 - Getting User Input using Java

Video Lecture – 3?

6 - Math and Arithmetic Operators in Java 7 - Increment Operator and Assignment Operator 8 - IF ... ELSE Statements and Relational Operators 9 - Logical Operators in Java

Video Lecture – 4?

10 - switch Statement in Java 11 - The while Statements (while Loops) 12 - The do-while Statements (do-while Loops)

Video Lecture – 5?

13 - Arrays in Java 14 - The for Statement in Java (for loops) 15 - Java String 16 - Introduction to Methods

Video Lecture – 6?

17 - Parameter passing and Returning a Value from a Method 18 - Classes and Objects in Java 19 - Class Constructor in Java 20 - Method Overloading in Java

Video Lecture – 7?

21 - 'static' keyword in Java 22 - 'static' keyword Example in Java 23 - Public, Private, Protected and this (Java Access Modifiers) 24 - The final keyword in Java

Video Lecture – 8?

25 - Inheritance in Java 26 - Polymorphism in Java 27 - Method Overriding in Java 28 - Abstract Methods and Classes

Video Lecture – 9?

29 - Java Interfaces 30 - Recursion in java 31 - Arraylist in Java 32 - LinkedList in Java 33 - Difference between LinkedList vs ArrayList in Java

Video Lecture – 10?

34 - ListIterator in Java 35 - HashSet in Java 36 - Catching and Handling Exceptions in Java using Try Catch Blocks 37 - Java Finally block (try-catch-finally Exception Handling in Java )

Video Lecture – 11?

38 - Create a File and Write in it Using PrintWriter and File class 39 - How to Read file using Java 40 - Using Date & Time + formatting Date using SimpleDateFormat

Revision

Java Flow Control

Java Arrays

Java OOP (I)

Java OOP (II)

Java OOP (III)

Java Exception Handling

Java List

Java Queue

Java Map

Java Set

Java I/O Streams

Java Reader/Writer

Additional Topics

In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way.

In computer programming, comments are a portion of the program that are completely ignored by Java compilers. They are mainly used to help programmers to understand the code. For example,

// declare and initialize two variables
int a =1;
int b = 3;

// print the output
System.out.println("This is output");

Here, we have used the following comments,

  • declare and initialize two variables
  • print the output

Types of Comments in Java

In Java, there are two types of comments:

  • single-line comment
  • multi-line comment

Single-line Comment

A single-line comment starts and ends in the same line. To write a single-line comment, we can use the // symbol. For example,

// "Hello, World!" program example
 
class Main {
    public static void main(String[] args) {    	
    {
        // prints "Hello, World!"
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!

Here, we have used two single-line comments:

  • “Hello, World!” program example
  • prints “Hello World!”

The Java compiler ignores everything from // to the end of line. Hence, it is also known as End of Line comment.


Multi-line Comment

When we want to write comments in multiple lines, we can use the multi-line comment. To write multi-line comments, we can use the /*….*/ symbol. For example,

/* This is an example of  multi-line comment.
 * The program prints "Hello, World!" to the standard output.
 */

class HelloWorld {
    public static void main(String[] args) {    	
    {	
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!

Here, we have used the multi-line comment:

/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/

This type of comment is also known as Traditional Comment. In this type of comment, the Java compiler ignores everything from /* to */.


Use Comments the Right Way

One thing you should always consider that comments shouldn’t be the substitute for a way to explain poorly written code in English. You should always write well structured and self explaining code. And, then use comments.

Some believe that code should be self-describing and comments should be rarely used. However, in my personal opinion, there is nothing wrong with using comments. We can use comments to explain complex algorithms, regex or scenarios where we have to choose one technique among different technique to solve problems.

Note: In most cases, always use comments to explain ‘why‘ rather than ‘how‘ and you are good to go.