Print Variables
In short: Making the current value of a variable visible on the console or another output channel — one of the first techniques learned when programming, and still one of the simplest debugging tools today.
In more detail: When printing, values are usually automatically converted into a text representation (e.g. a number into its digit sequence) — for custom objects, this conversion often has to be defined yourself, otherwise the output only shows a not very helpful internal memory address. Several values are often combined via string concatenation into a single, readable output line.
In Depth
int age = 30;
System.out.println("Age: " + age); // string concatenation
System.out.printf("Age: %d%n", age); // format string, often more readableFor several combined values, pure string concatenation (+) quickly becomes cluttered — many languages therefore offer format strings/template literals as a more readable alternative, where the text and the inserted values can be more clearly separated from each other:
name = "Alex"
age = 30
print(f"{name} is {age} years old") # f-string - values marked directly in the textAn important point for custom, self-defined objects: without an explicit specification, the default output of an object usually only shows a not very helpful technical representation (e.g. class name + internal memory address) instead of its actual content. Most languages therefore allow defining a custom text representation for custom types (in Java, e.g., by overriding toString()), which is then automatically used every time that object is printed.
Printing variables is one of the simplest and oldest debugging techniques of all (“print debugging”): briefly printing the current value of a variable at suspicious spots in the code, to check whether the program is actually doing what you expect — simple, but surprisingly effective, especially combined with more targeted tools like a debugger.
See also: String Concatenation, Debugging