Grails Cookbook - A collection of tutorials and examples

Groovy println Examples

The Groovy println method is similar to Java's System.out.println method. This is usually used to display a single line to the screen or console. Here are some Examples.

Syntax

Here is the syntax of Groovy println method.

public void println()

This prints a new line to the current out variable which is usually a PrintWriter or the standard out.

Here is another syntax

public void println(Object value)

This prints the value and a new line to the current out variable. This is the most commonly used method to display something to the screen or console

Groovy println Examples

Here is a simple Groovy println example:

class GroovyPrintExample {
   static void main(String[] args) {
      println "Hello World"
      println "Hello World"
   }
}

Notice that each invocation to println displays the value to the console and then adds a new line at the end. Hence this would display the texts in separate lines as shown below:

Hello World
Hello World

Here is another example of using println without a parameter.
class GroovyPrintExample {
   static void main(String[] args) {
      println "Hello World"
      println()
      println "Hello World"
   }
}

Notice that println() just displays a new line as shown in the output below:

Hello World
 
Hello World


Other Groovy Tutorials