Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java Comments
Java Comments
Java comments are text notes written in the code to provide an explanation about the source code. The comments can be used to explain the logic or for documentation purposes. The compiler does not compile the comments. In Java, comments are very similar to C and C++.
In Java, there are three types of comments:
- Single-line comments
- Multiline comments
- Documentation comments
Let's discuss each type of comment in detail.
1. Single Line Comment
The single-line comment is used to add a comment on only one line and can be written by using the two forward slashes (//). These comments are the most used commenting way.
The single line comments are the most used commenting way to explain the purpose (or to add a text note) of the line.
Syntax
Consider the below syntax to write a single line comment in Java:
// comment
Example 1: Java Single Line Comment
// if divisor is 0 throw an exception if (divisor == 0) { throw new IllegalArgumentException("divisor cannot be zero"); }
Example 2: Java Single Line Comment
Following code shows the usage of single line comments in a simple program. We've added comments to code lines to explain their purpose.
package com.tutorialspoint; public class MyFirstJavaProgram { public static void main(String[] args) { MyFirstJavaProgram program = new MyFirstJavaProgram(); double result = program.divide(100, 10); System.out.println(result); } private double divide(int dividend, int divisor) throws IllegalArgumentException { // if divisor is 0 throw an exception if (divisor == 0) { throw new IllegalArgumentException("divisor cannot be zero"); } return (double) dividend / divisor; // returns the result of the division as double } }
Output
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0
2. Multiline Comment
The multiline (or, multiple-line) comments start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/) and they are used to add comment on multiple lines.
The multiline comments are very useful when we want to put a long comment spreading across multiple lines or to comment out the complete code.
Syntax:
Consider the below syntax to write multiline comment in Java:
/* Comment (line 1) Comment (line 2) ... */
Example 1: Java Multiline Comment
/* This is an example of multi line comment. */ /* if (dividend == 0) { throw new IllegalArgumentException("dividend cannot be zero"); } */
Example 2: Java Multiline Comment
Following code shows the usage of multiple comments in a simple program. We've commented out extra code from a method using Multiline comments.
package com.tutorialspoint; public class MyFirstJavaProgram { public static void main(String[] args) { MyFirstJavaProgram program = new MyFirstJavaProgram(); double result = program.divide(100, 10); System.out.println(result); } private double divide(int dividend, int divisor) throws IllegalArgumentException { if (divisor == 0) { throw new IllegalArgumentException("divisor cannot be zero"); } /* if (dividend == 0) { throw new IllegalArgumentException("dividend cannot be zero"); } */ return (double) dividend / divisor; } }
Output
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0
3. Documentation Comment
The documentation comments are used for writing the documentation of the source code. The documentation comments start with a forward slash followed by the two asterisks (/**), end with an asterisk followed by a backward slash (*/), and all lines between the start and end must start with an asterisk (*).
The documentation comments are understood by the Javadoc tool and can be used to create HTML-based documentation.
Syntax
Consider the below syntax to write documentation comment in Java:
/** * line 1 * line 2 ... */
Example 1: Java Documentation Comment
/** * This is a documentation comment. * This is my first Java program. * This will print 'Hello World' as the output * This is an example of multi-line comments. */ public class MyFirstJavaProgram {}
The above commenting style is known as documentation comments. It is used by Javadoc tool while creating the documentation for the program code. We can give details of arguments, exception and return type as well using following annotation in documentation comments.
/** * @param dividend * @param divisor * @return quotient * @throws IllegalArgumentException if divisor is zero */ private double divide(int dividend, int divisor) throws IllegalArgumentException { }
Example 2: Java Documentation Comment
Following code shows the usage of documentation comments in a simple program. We've defined a comments on the class declaration to give details of the class. In case of method, we're adding details of parameters, return value and exception raised in documentation block of the method comments section.
package com.tutorialspoint; /** * This is a documentation comment. * This is my first Java program. * This is an example of multi-line comments. * We're printing result of divison of two numbers in this program */ public class MyFirstJavaProgram { public static void main(String[] args) { MyFirstJavaProgram program = new MyFirstJavaProgram(); double result = program.divide(100, 10); System.out.println(result); } /** * @param dividend * @param divisor * @return quotient * @throws IllegalArgumentException if divisor is zero */ private double divide(int dividend, int divisor) throws IllegalArgumentException { if (divisor == 0) { throw new IllegalArgumentException("divisor cannot be zero"); } return (double) dividend / divisor; } }
Output
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0