C++ vs Java

How to Write "I Love You" in Java

Print the message I love you in Java with a complete program, the commands to compile and run it, and the mistakes beginners make most often.

On this page
  1. Compile and run it
  2. What each part does
  3. Store the message in a variable
  4. Print the message more than once
  5. The same message in C++
  6. Common mistakes

To print “I love you” in Java, create a class with a main method and print the message with System.out.println. Here is a complete program:

public class Main {
    public static void main(String[] args) {
        System.out.println("I love you");
    }
}

Compile and run it

Save the code in a file named Main.java. Then, from a terminal in the same folder, compile and run it:

javac Main.java
java Main

The output is:

I love you

What each part does

  • public class Main declares a class. Java requires every program to sit inside a class. The file name must match the public class name, so the file is Main.java.
  • public static void main(String[] args) is the starting point. The JVM calls this method first when you run the program, so its spelling and parameter list must match exactly.
  • System.out.println("I love you"); prints the text and then moves to a new line. Use System.out.print if you don’t want a line break.
  • javac compiles the source file into Main.class, which holds bytecode. java Main runs that compiled class.

Each statement ends with a semicolon.

Store the message in a variable

If you want to reuse the message, store it in a String variable:

public class Main {
    public static void main(String[] args) {
        String message = "I love you";
        System.out.println(message);
    }
}

String starts with a capital letter because it is a class from the Java library, not a basic type.

A for loop repeats the print statement. This version prints the message three times:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("I love you");
        }
    }
}

The same message in C++

For comparison, here is the C++ version:

#include <iostream>

int main() {
    std::cout << "I love you" << std::endl;
    return 0;
}

C++ needs an #include line for input and output, and its main function does not belong to a class. std::cout with << does the job that System.out.println does in Java. For the C++ side of the same task, see how to write “I love you” in C++.

Common mistakes

  • File name mismatch. If the public class is Main but the file is named hello.java, javac reports an error. The class and file names must match.
  • Missing semicolon. A missing semicolon stops compilation, and the compiler points to the line where it expects one.
  • Single quotes. 'I love you' is an error. Single quotes hold one character (a char), so text goes in double quotes.
  • Wrong capitalization. Java is case-sensitive, so system.out.println and string don’t work.
  • Running before compiling. Running java Main before javac Main.java fails, because the compiled Main.class file doesn’t exist yet.

The complete guide to c++ vs java

Difference Between C++ and Java
Read the guide

Keep exploring

Guides from other topics

All topics →

List vs Array in C++

Compare C++ arrays, std::array, std::vector and std::list by memory layout, element access and insertion cost, with code and a simple rule for choosing.

List vs Array 5 min read

std::optional in C++

std::optional in C++17 holds either a value or nothing. Learn how to check it, read it safely with value_or and return it from a function, with examples.

std::optional 4 min read