Java Tutorial
🔍

Print Name in Java

beginner·  Basics & I/O  ·  Fundamentals

Problem

A string literal is fixed text written directly in the source code, surrounded by double quotes.

Store a name in a variable and print it to the console.

Input
John
Output
John

Java Program

Java
public class PrintName { public static void main(String[] args) { String name = "John"; // store the name in a variable System.out.println(name); } }

Output

John

Core Logic

Storing text in a String variable and printing that variable separates the data from the print statement, a small step up from hard-coding text directly into println().

How It Works
  1. 1String name = "John"; declares a variable and stores the text "John" in it.
  2. 2System.out.println(name) prints the variable's current value, not the literal word name.
Changing the value assigned to name changes what gets printed, without touching the println() call itself.
💡

Key Point: Once a String is created in Java, its content can't change — reassigning name points it at a brand-new String object instead of modifying the original.

Key Concepts

String variableSystem.out.println()

Related Programs