{"id":5170,"date":"2021-03-23T11:36:26","date_gmt":"2021-03-23T11:36:26","guid":{"rendered":"https:\/\/ded9.com\/?p=5170"},"modified":"2025-12-27T13:22:53","modified_gmt":"2025-12-27T13:22:53","slug":"learning-comments-in-java-in-quite-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learning-comments-in-java-in-quite-simple-language\/","title":{"rendered":"Learning Comments in Java in Quite Simple Language"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn about comments in Java and why and how to use them.<span id=\"more-50063\"><\/span><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">In programming, comments are considered part of the program and are used to aid understanding of the code. <span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><span style=\"font-size: 12pt;\">However, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_compiler\" target=\"_blank\" rel=\"noopener\">Java compilers<\/a> completely ignore them and do not execute them<\/span><\/span>.<\/span><\/p>\n<p>So in the Java programming language, there are two types of comments:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/ *\u2026 * \/\r\n\/\/\u2026.<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Common comment \/*\u2026\u2026.*\/<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258234 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/66545fa02d1b0_comment_in_c__banner_-e1744820324340.png\" alt=\"Common comment \/*\u2026\u2026.*\/\" width=\"1200\" height=\"702\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/66545fa02d1b0_comment_in_c__banner_-e1744820324340.png 1200w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/66545fa02d1b0_comment_in_c__banner_-e1744820324340-300x176.png 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/66545fa02d1b0_comment_in_c__banner_-e1744820324340-1024x599.png 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/66545fa02d1b0_comment_in_c__banner_-e1744820324340-768x449.png 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/span><\/h1>\n<p>This is a multi-line comment. The Java compiler ignores everything from \/* to *\/. For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/ * This is a multi-line comment.<\/li>\n<li dir=\"ltr\">* The problem prints \u201cHello, World!\u201d\u00a0to the standard output.<\/li>\n<li dir=\"ltr\">* \/<\/li>\n<li dir=\"ltr\">class HelloWorld {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">System.out.println (\u201cHello, World!\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>In the above code, the comment is equal to:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/ * This is a multi-line comment.\r\n\r\n* The problem prints \u201cHello, World!\u201d to the standard output.\r\n\r\n* \/<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">One line comment \/\/<\/span><\/h2>\n<p>The compiler ignores everything from \/\/ to the bottom of the line.\u00a0For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ \u201cHello, World!\u201d\u00a0program<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">class AssignmentOperator {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">System.out.println (\u201cHello, World!\u201d);\u00a0\/\/ prints \u201cHello, World!\u201d<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>The above program includes two one-line comments:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\"Hello, World!\u201d\"program\r\n\r\nAnd\r\n\r\n\/\/ prints \"Hello, World!\"<\/pre>\n<\/div>\n<h2>Use the comments in the right way<\/h2>\n<p>Comments should not be a substitute for explaining poorly written code in English. Write code with good structure and readability, and use comments.<\/p>\n<p>Some believe the code should be documented and include a few comments. However, we strongly oppose this (this is our personal opinion). There is no problem with using comments to explain complex algorithms, regex, or scenarios.<\/p>\n<p>Use comments to explain &#8220;why&#8221; instead of &#8220;how.&#8221;<\/p>\n<h2><span style=\"font-size: 18pt;\">Types of Java Comments<\/span><\/h2>\n<h2><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"aligncenter wp-image-258238 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Types-of-comments-in-java-chart.webp\" alt=\"Types of Java Comments\" width=\"500\" height=\"297\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Types-of-comments-in-java-chart.webp 500w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Types-of-comments-in-java-chart-300x178.webp 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/span><\/h2>\n<p>Commenting code is a good practice for Java developers, enhancing code readability and maintainability. Java provides three types of comments:<\/p>\n<ol start=\"1\">\n<li><strong>Inline (Single-line) Comments<\/strong> \u2192 <code>\/\/<\/code><\/li>\n<li><strong>Block (Multi-line) Comments<\/strong> \u2192 <code>\/* ... *\/<\/code><\/li>\n<li><strong>JavaDoc Comments<\/strong> \u2192 <code>\/** ... *\/<\/code><\/li>\n<\/ol>\n<h3><strong>Inline Comments (<\/strong><code>\/\/<\/code><strong>)<\/strong><\/h3>\n<p>Also known as single-line comments, inline comments start with <code>\/\/<\/code> and affect only the code on that line.<\/p>\n<p>Example:<\/p>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>public class JavaCommentsExample {\r\n  \/\/ This is a single-line comment\r\n\r\n  public static void main(String[] args) {\r\n    System.out.println(\"This line will run.\");\r\n    \/\/System.out.println(\"This line will not run.\");\r\n  }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>Sometimes, inline comments are placed at the end of a line to clarify a variable or operation:<\/p>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>int number = 10;  \/\/ Initialize number to 10\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h3><span style=\"font-size: 14pt;\"><strong>Block Comments (<\/strong><code>\/* ... *\/<\/code><strong>)<\/strong><\/span><\/h3>\n<p>Block or multi-line comments allow developers to comment out multiple lines at once. This is useful for temporarily turning off code or adding detailed explanations.<\/p>\n<p>Example:<\/p>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>public class JavaCommentsExample {\r\n  \/* This block comment spans one line. *\/\r\n  public static void main(String[] args) {\r\n    System.out.println(\"This line will run.\");\r\n\r\n    \/* This block comment spans multiple lines.\r\n       System.out.println(\"These lines will not run.\");\r\n       None of the code in this block comment will be executed.\r\n    *\/\r\n  }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h3><span style=\"font-size: 14pt;\"><strong>JavaDoc Comments (<\/strong><code>\/** ... *\/<\/code><strong>)<\/strong><\/span><\/h3>\n<p>JavaDoc comments are designed explicitly to generate documentation. Unlike inline or block comments, which are meant for developers maintaining the code, JavaDoc comments help <strong>users<\/strong> understand how to use a class or method.<\/p>\n<p>Example:<\/p>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>\/** This JavaDoc comment describes the class. *\/\r\npublic class JavaCommentsExample {\r\n\r\n  \/** This JavaDoc comment describes the method. *\/\r\n  public static void main(String[] args) {\r\n    System.out.println(\"This line will run.\");  \/\/ Inline comment\r\n    \/* System.out.println(\"This is in a block comment.\"); *\/\r\n  }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h2><span style=\"font-size: 18pt;\"><strong>Best Practices for Using Java Comments<\/strong><\/span><\/h2>\n<ul>\n<li><strong>Use JavaDoc (<\/strong><code>\/** ... *\/<\/code><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"> To<\/span>\u00a0describe classes and methods that need documentation.<\/li>\n<li><strong>Use inline comments (<\/strong><code>\/\/<\/code><strong>)<\/strong> for brief explanations of specific lines.<\/li>\n<li><strong>Use block comments (<\/strong><code>\/* ... *\/<\/code>to comment out multiple lines when necessary.<\/li>\n<\/ul>\n<p>While well-written code should be self-explanatory, good developers use comments wisely to enhance clarity.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Multi-line Comments in Java<\/strong><\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><strong><img decoding=\"async\" class=\"aligncenter wp-image-258241 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/IMG12505_Java-Comments.png\" alt=\"Multi-line Comments in Java\" width=\"1024\" height=\"768\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/IMG12505_Java-Comments.png 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/IMG12505_Java-Comments-300x225.png 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/IMG12505_Java-Comments-768x576.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/strong><\/span><\/h1>\n<p>Multi-line comments allow developers to write explanatory notes that span several lines. To create a multi-line comment, use the <code>\/* ... *\/<\/code> syntax.<\/p>\n<h4><strong>Example: Multi-line Comment<\/strong><\/h4>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>\/* This is a multi-line comment.\r\n * The program prints \"Hello, World!\" to the standard output.\r\n *\/\r\n\r\nclass HelloWorld {\r\n    public static void main(String[] args) {\r\n        System.out.println(\"Hello, World!\");\r\n    }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>Hello, World!\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>In this example, the Java compiler ignores everything within <code>\/* ... *\/<\/code>, making it helpful to include detailed descriptions or temporarily disable large code sections.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Preventing Code Execution Using Comments<\/strong><\/span><\/h2>\n<p>During debugging, if a portion of code produces errors, we can use comments to prevent its execution instead of deleting it.<\/p>\n<h4><strong>Example: Debugging with Comments<\/strong><\/h4>\n<div>\n<div class=\"rounded-b-xl bg-background-static-850 px-4 pb-1.5 dark:bg-background-static-900\">\n<div>\n<pre><code>public class Main {\r\n    public static void main(String[] args) {\r\n        System.out.println(\"some code\");\r\n        \/\/ System.out.println(\"error code\"); \/\/ Commented out for debugging\r\n        System.out.println(\"some other code\");\r\n    }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>Using <code>\/\/<\/code> Before problematic code, temporarily disable it without removing it, making it a valuable debugging strategy.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Why Use Comments?<\/strong><\/span><\/h2>\n<ul>\n<li><strong>Improve Code Readability:<\/strong> Makes the code easier to understand for future reference.<\/li>\n<li><strong>Assist in <a href=\"https:\/\/ded9.com\/what-is-debugging-2\/\">Debugging<\/a>:<\/strong> Helps identify and prevent errors during Development.<\/li>\n<li><strong>Enhance Collaboration:<\/strong> Enables peer developers to understand the code structure efficiently.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are comments in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Comments are text inside Java code that the compiler ignores; they are used to explain code, make it easier to read, or temporarily disable lines during development.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What types of comments does Java support?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Java has single\u2011line comments starting with \/\/, multi\u2011line comments between \/* \u2026 *\/, and documentation comments that begin with \/** \u2026 *\/ for tools like Javadoc.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why should I use comments in my Java code?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Comments improve readability, help others (and yourself) understand logic later, and can be used to temporarily comment out code during testing or debugging.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about comments in Java and why and how to use them. In programming, comments are considered part of the program and are used to aid understanding of the code. However, Java compilers completely ignore them and do not execute them. So in the Java programming language, there are two [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5171,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[3926,840],"class_list":["post-5170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-debug","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5170"}],"version-history":[{"count":7,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5170\/revisions"}],"predecessor-version":[{"id":266521,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5170\/revisions\/266521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5171"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}