English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Grund教程

Java Prozesssteuerung

Java Array

Java objektorientiert (I)

Java objektorientiert (II)

Java objektorientiert (III)

Java Ausnahmebehandlung

Java Liste (Liste)

Java Queue (Warteschlange)

Java Map-Kollektion

Java Set-Kollektion

Java Eingabe/Ausgabe (I/O)

Java Reader/Writer

Andere Themen von Java

Java Kommentare

In diesem Tutorial werden Sie Java-Kommentare, warum sie verwendet werden und wie sie korrekt verwendet werden, kennenlernen.

In der Computersprachprogramierung sind Kommentare ein Teil des Programms, die der Java-Compiler vollständig ignoriert. Sie werden hauptsächlich verwendet, um Programmierern zu helfen, den Code besser zu verstehen. Zum Beispiel,

//Erklärung und Initialisierung von zwei Variablen
int a =1;
int b = 3;
//Druckausgabe
System.out.println("This is output");

Hier haben wir die folgenden Kommentare verwendet:

  • Erklärung und Initialisierung von zwei Variablen

  • Druckausgabe

Kommentartypen in Java

Es gibt zwei Arten von Kommentaren in Java:

  • Einzeilige Kommentare

  • Mehrzeilenkommentare

Einzeilige Kommentare

Einzeilige Kommentare beginnen und enden auf der gleichen Zeile. Um Einzeilige Kommentare zu schreiben, können wir//Symbole. Zum Beispiel,

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

Output:

Hello, World!

Hier haben wir zwei Einzeilige Kommentare verwendet:

  • "Hello, World!" Beispielprogramm

  • Druckausgabe "Hello World!"

Der Java-Compiler ignoriert von//Alles zwischen Zeilenenden. Daher wird es auchZeilenendeKommentare (Einzeilige Kommentare) verwenden.

Mehrzeilenkommentare

Wenn wir eine Mehrzeilenkommentare schreiben möchten, können wir Mehrzeilenkommentare verwenden. Um Mehrzeilenkommentare zu schreiben, können wir/*....*/Symbole. Zum Beispiel,

/* This is an example of multi-line comments.
 * This program will print “ Hello,World!” to the standard output.
 */
class HelloWorld {
    public static void main(String[] args) {}}    	
    {	
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!

Here, we use multi-line comments:

/* This is an example of multi-line comments.
 * This program will print “ Hello,World!” to the standard output.
 */

This type of comment is also calledTraditional CommentsIn this type of comment, the Java compiler will ignore from/*to all the content*/。

Proper Use of Comments

You should always know one thing, that comments should not be used as a substitute for poor code explanations. You should always write structured and self-explanatory code. Then, consider using comments.

Some people think that code should be self-explanatory and comments should be used sparingly. However, in my personal opinion, there is nothing wrong with using comments. We can use comments to explain complex algorithms, regular expressions, or solutions to problems that require choosing between different technologies.

NoteIn most cases, always use comments to explain “ Why ” instead of “ How to do