CodeGym /Courses /JAVA 25 SELF /Compiler and Comments

Compiler and Comments

JAVA 25 SELF
Level 1, Lesson 5
Available

1. The Java compiler

By the way, you will hear more than once that Java’s undeniable advantage over other languages is its cross-platform capability. What is it and how does it work? Let’s start from the beginning.

The thing is, a computer can execute only the simplest instructions.

There are commands for dogs such as “Heel”, “Paw”, and others; when a dog hears one, it does something important. For a computer, numbers play the role of such commands: each instruction is encoded as some number (also called machine code).

Writing a program as numbers is very hard, so people invented programming languages and compilers. Such a language is understandable to a human on the one hand and to a compiler on the other. A compiler is a special program that translates the text of a program written in a programming language into a set of machine codes.

Usually, a programmer writes a program in a programming language and then runs a compiler, which, based on the files with the program’s code written by the programmer, produces a single file with machine code — the final (compiled) program.

  • Program in C++
  • Compiler
  • Program consisting of machine code
Compilation stages for C++

The resulting program can be executed on the computer right away. The downside of this approach is that the code of the resulting program strongly depends on the processor and operating system. A program compiled for Windows will not work on a phone running Android.

If you wrote a program for Android, it will not run on Windows!

But Java takes a much more innovative approach.

  • Program in Java
  • Java compiler
  • Program consisting of special codes (bytecode)
  • Java VM
  • Program consisting of machine code
Compilation stages for Java

The Java compiler does not compile all classes into a single program of machine code. Instead, it compiles each class separately, and not into machine code but into a special intermediate code (bytecode). Compilation into machine code is performed when the program is launched.

So who compiles the program into machine code at runtime?

There is a special program called the JVM (Java Virtual Machine) — the Java Virtual Machine. First you launch it, and then the program composed of bytecode. And the JVM compiles the program into machine code before it is executed.

If you have a powerful processor that supports a larger set of machine instructions, then during the “second compilation” the machine code will be generated with your specific CPU and OS in mind. That’s why Java is sometimes faster than C++, which is compiled directly into machine code and can use only the most common processor instructions.

2. Typical compilation errors

Speed and code optimization are not very important to us yet, but what matters is that the compiler checks your program for errors. It validates the code and will not let it through if it finds even a small mistake.

Example of an error:

You are trying to assign a number to a variable that can store only text.

String userName = 42; // Oops! You can’t do that.

The compiler will immediately report that the types do not match: Cannot implicitly convert type 'int' to 'string'.

Another example — a typo in the println call:

System.out.printline("Hello!"); // Error in the method name

The compiler will say: “No such method — check your syntax!”

How to read errors?

Most compilers hint at where the error is in the code and sometimes even suggest fixes. Don’t be afraid of errors: each one is a step toward enlightenment and defeating your inner “boss”.

3. Comments

So, imagine you’re writing a complex program. Everything works, you’re a genius! But a month later you open that same code and… you understand nothing. A couple of lines here, a strange variable there. All you can think is: “This was clearly written by anyone but me!” That’s where comments save the day.

Comments are like notes in the margins of a textbook or labels on the fridge (“Beer. Do not drink. For the morning!”). The compiler ignores them, while people do the opposite. Comments let you explain the meaning of the code, mark important details, and even “disable” chunks of a program without deleting them.

Important: no compiler reads your comments; they are completely ignored!

Single-line comments

Java has two kinds of comments — single-line and multi-line. Single-line comments start with a double slash // and go to the end of the line. Everything after // is considered a comment and is completely ignored by the compiler.

Example:

System.out.println("Hello, world!"); 	// This prints a greeting to the screen

Here, // This prints a greeting to the screen is a comment. If it’s unclear why a line is needed, write a comment like this!

Another example:

int x = 42; 	// The programmer’s age value

You can also write single-line comments on their own lines:

// Initializing the user name variable
String userName = "John";

Almost like lines in a script: the comment explains the what and why.

Multi-line comments

What if you’ve got a lot to say? A real story? For that, there are multi-line comments that start with /* and end with */. Everything between these symbols is ignored by the compiler, even if it spans multiple lines.

Example:

/*
  Here we print the user’s name to the screen.
  If the name is missing, we will write "Guest".
*/
System.out.println("Enter your name:");

Or like this:

int age = 18; /* Initial age value
                 for a new user */

Attention: you cannot nest multi-line comments. Don’t try to outsmart the compiler — it won’t work.

Illustration:

Type Syntax Example
Single-line
// comment
int x = 1; // explanation
Multi-line
/* stuff here */
/* here you can explain anything you want */

4. Real-life examples

“Disabling” a line of code:

Sometimes you temporarily don’t want a certain fragment of code to run. Comments are perfect for that:

// System.out.println("Text that is temporarily not needed");

The line remains, but the compiler doesn’t “see” it.

TODO and FIXME notes

Programmers are people too, and sometimes you need to remind yourself that the code isn’t finished yet or that there’s a bug here. For that, they often write:

// TODO: add a check for empty input
// FIXME: this function calculates the sum incorrectly

Many editors and IDEs even highlight such words!

Programmers joke

// I am not responsible for this code. I was forced to write it against my will.
// Dear, I_am_from_the_future! Please forgive me for this code.
// If I see this again, I’ll have to start carrying a weapon to work.
// If this condition ever becomes true, please contact me at tel. xxx-xxx-xxx for a reward.
// Dear programmer:
//
// When you finish "optimizing" this subroutine
// and realize what a big mistake that was,
// please increment the counter below as a warning
// to the next guy:
//
// number_of_hours_spent_here = 42
// When I started writing this, only God and I understood what I was doing. 
// Now only God does.
// Sometimes I think the compiler ignores all my comments.
// I dedicate all my code, all my work to my wife Darlin, who 
// will have to support me, our three kids, and the dog when 
// this goes to production.

Yes, comments can be very funny. After all, they’re written by real people.

1
Task
JAVA 25 SELF, level 1, lesson 5
Locked
Notes to self 📝
Notes to self 📝
1
Task
JAVA 25 SELF, level 1, lesson 5
Locked
Code block for testing 🛠️
Code block for testing 🛠️
1
Task
JAVA 25 SELF, level 1, lesson 5
Locked
Fixing bugs 🐛
Fixing bugs 🐛
1
Survey/quiz
Commands and the first program, level 1, lesson 5
Unavailable
Commands and the first program
Commands and the first program
Comments (11)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #11803696 Level 2, Riyadh, Saudi Arabia
12 July 2026
how about you fix your bugs
Muksud Alam Level 4, Bangladesh, Bangladesh
6 June 2026
amazing way to walk in the realm of Java
Aurelian07 Level 2, Delhi, India
8 March 2026
even there solution is not working on the third task
yDc-Develop Level 2, Tienen, Belgium
8 March 2026
public static void main(String[] args) { int number = 10; //int number = 20; int uninitialized; //System.out.println(uninitialized); //String text = 123; //System.out.println(undeclaredVariable); //System.out.println(number); } <-- is this not correct?
GustavoSantosgcs Level 2, Brazil
20 February 2026
There's an erro in bugs task
Anonymous #11749386 Level 2, Mumbai, India
29 January 2026
You need to comment all statement.
Md Ismail Level 2, Bangladesh, Bangladesh
11 January 2026
Hello, I’m confused by this task. The requirements say the program must not print anything, but the checker says not to comment out System.out.println(number); because it is not a compilation error. These two instructions seem to conflict. and your solution also fails Could you please clarify which behavior is expected? Thank you.
Anonymous #11748419 Level 2, Georgetown, Guyana
19 January 2026
Yes I am having the exact same issue. It is in fact conflicting.
Anonymous #11751331 Level 2, Mumbai, India
27 January 2026
comment out second initialization of number As uninitialized does not contain any value we cannot print it , so comment out that also the string text will also cause error due to "" so comment out that also. we have not declared a variable named undeclaredVariable so comment that also
agirosion Level 2, Brierley Hill, United Kingdom
7 January 2026
Fixing bugs.... have bug??? In requirements, "The program must not print anything to the console after the fix." and this "No other changes are allowed in the program except adding comments to comment out erroneous lines." How it do same time? Help 😢
Anonymous #11745459 Level 2, Norwalk, United States
9 January 2026
I'm having the same issue! No clue on how to resolve, talk about a bug amirite?!