Home› Java Programming Language› / Programs / Java Program to Print Your Name (My First Java Program)
🚀 Programming Example

Java Program to Print Your Name (My First Java Program)

19,851 Views
Java Java 8 or later
Beginner
Code Snippet

Write your first Java program to print your name (or any text) on the screen using System.out.println(). This beginner program introduces the basic structure of a Java program: a class, the main() method and an output statement.

Before You Begin

What you will learn

  • Understand the basic structure of a Java program
  • Learn why the file name must match the public class name
  • Understand the purpose of the public static void main(String[] args) method
  • Print text on the screen using System.out.println()
  • Write single-line (//) and multi-line (/* */) comments
  • Compile a program with javac and run it with java

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • A text editor or IDE such as Notepad, VS Code, Eclipse or IntelliJ IDEA
  • Basic knowledge of using the command prompt or terminal
  • The PATH variable set so that javac and java commands work

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named MyFirstJavaProgram.
  3. Define the main() method inside the class.
  4. Display the text "rummanansari.com" using System.out.println().
  5. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac MyFirstJavaProgram.java
java MyFirstJavaProgram

💻 Program Code

// Print Your Name
public class MyFirstJavaProgram {

    /* This is my first Java program.
     * It prints your name (or any text) as the output.
     */

    public static void main(String[] args) {
        System.out.println("rummanansari.com"); // prints rummanansari.com
    }
}

Sample Input

No input required

🖥 Program Output

                                rummanansari.com
                            

📘 Explanation

Overview

This is the simplest Java program you can write. It prints a single line of text on the screen. Every Java program you write later will follow this same basic structure: a class, a main() method and statements inside it.

Save this file as MyFirstJavaProgram.java. In Java, the file name must exactly match the name of the public class, including capital letters.

Step-by-Step Explanation

1

Single-Line Comment

Notes for humans, ignored by the compiler.

// Print Your Name

Anything after // on the same line is a comment. It explains the code but isn't executed.

2

Class Declaration

Every Java program lives inside a class.

public class MyFirstJavaProgram {
    ...
}

public means the class can be accessed from anywhere. class is the keyword used to define a class, and MyFirstJavaProgram is its name. The curly braces { } mark the start and end of the class.

3

Multi-Line Comment

Comments that span several lines.

/* This is my first Java program.
 * It prints your name (or any text) as the output.
 */

Everything between /* and */ is a comment, however many lines it covers.

4

The main() Method

The entry point of every Java program.

public static void main(String[] args)

When you run the program, the JVM (Java Virtual Machine) looks for this exact method and starts executing from here.

5

Print Statement

Shows output on the screen.

System.out.println("rummanansari.com");

System.out.println() prints the text inside the double quotes and then moves the cursor to the next line. Every statement in Java ends with a semicolon ;.

Parts of the main() Method

Keyword Meaning
public The JVM can call this method from outside the class.
static The method can run without creating an object of the class.
void The method doesn't return any value.
main The name the JVM looks for to start the program.
String[] args An array that holds command-line arguments, if any are passed.

How to Compile and Run

javac MyFirstJavaProgram.java
java MyFirstJavaProgram

javac compiles the source code into bytecode and creates MyFirstJavaProgram.class. java then runs that bytecode on the JVM. Don't add .class when running the program.

Output

rummanansari.com

Print Your Own Name

Replace the text inside the quotes with your name:

System.out.println("Rumman Ansari");

Common Mistakes

File name doesn't match the class name Saving the file as myfirstjavaprogram.java or Program.java gives a compile error. It must be MyFirstJavaProgram.java.
Missing semicolon Leaving out ; at the end of the print statement gives the error ';' expected.
Wrong capitalisation Java is case-sensitive. system.out.println or Main won't work. Use System and main.
Good practice Keep comments accurate. If you change what the program prints, update the comment too.

print() vs println()

Method Behaviour
System.out.print() Prints the text and keeps the cursor on the same line.
System.out.println() Prints the text and moves the cursor to the next line.

Key Takeaway

Every Java program needs a class and a main() method. Use System.out.println() to print output, and always save the file with the same name as the public class.

Complexity

Time: O(1)

Space: O(1)

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.