println and print
println shows something on the screen and moves to the next line, while print stays on the same line. It is like speaking a sentence and either pressing enter or continuing on.
In this page:
Syntax
println(value)
print(value)
println and print
println prints a value followed by a newline, while print prints without adding one. Both accept any value and call .toString on it implicitly.
println is by far the more commonly used of the two, since most output benefits from ending on its own line.
Note:
Use string interpolation (
s"...") with println for the clearest, most readable way to build formatted output.Example: println and print
object Main extends App {
println("This line ends with a newline")
print("This line ")
print("does not, so ")
println("this continues on the same line")
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
printand getting all the output on one line because it does not add a newline. - Passing several values like
println("a", "b"), which prints a tuple(a,b)instead of two values. - Expecting
printlnto format numbers, such asprintln(3.10)printing3.1, when you needf"..."for fixed decimals.
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: