Why a // Comment Can Still Execute Code in Java

This article explains Java’s three comment types, highlights a surprising case where a line starting with // still runs due to a Unicode escape sequence, and shows how the compiler processes \u000d to execute the code.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Why a // Comment Can Still Execute Code in Java

Java provides three kinds of comments: single‑line comments starting with //, block comments enclosed by /* ... */, and Javadoc comments written as /** ... */ for generating API documentation.

In normal situations, a line that begins with // is ignored by the compiler, serving only as a human‑readable note and having no effect on program execution.

However, there is an edge case where code placed after // still runs. This occurs when the comment line contains a Unicode escape sequence such as \u000d (carriage return) immediately after the slashes.

package com.sample.interview.core.foundation;

public class CommentExample {
    public static void main(String[] args) {
        // \u000d System.out.println("Hello World!");
    }
}

Running the above class prints:

Hello World!

The reason is that Unicode escapes are processed by the compiler before any lexical analysis. The sequence \u000d is translated into an actual carriage‑return character, which terminates the comment. Consequently, the System.out.println statement is no longer part of the comment and is executed.

This behavior is counter‑intuitive and demonstrates that even comment syntax can be subverted by Unicode escapes, a detail that developers should be aware of when writing or reviewing Java code.

backendcommentsJavadocunicode-escape
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.