Java Easter Egg: Label and Comment Trick

Take a close look at the snippet below. At first glance, you might expect a compiler error. What is a URL doing in the middle of a method? public class Main { public static void main(String[] args) { http://www.google.com System.out.println("Hello World"); } } The Twist: This code runs perfectly and prints "Hello World." How? It’s a classic Java "Easter Egg" logic. The compiler doesn't see a broken URL; it sees two distinct things: • http: is interpreted as a Label. In Java, you can label almost any statement (often used with break or continue in nested loops). • //www.google.com is interpreted as a Single-line comment. Because the label is valid and the rest of the line is ignored as a comment, the JVM just skips right past it to the println statement. It’s a great reminder that even in a language as structured as Java, there are always unusual syntax behaviors. #Java #CodingTips #SoftwareEngineering #JavaDeveloper #CleanCode #ProgrammingLogic #TechCommunity

To view or add a comment, sign in

Explore content categories