Prev Main Next

Functions That Act Like Statements

You use a statement to perform an action. Some Java functions are designed to be used as statements. So you just ask them to do something, not to produce an answer.

An example of a function that acts like a statement is System.out.println. (This is a method called println that is part of something called out that belongs to a class called System.) To use System.out.println, you write a parameter expression in parentheses, exactly as you would as if it produced a result. But you just write it as a statement. For example,

  System.out.println("What is your name? ");
runs System.out.println with parameter "What is your name? ".

System.out.println takes just one parameter, of type String. The actual argument that you use can be any expression of type String. For example, you can use the string concatenation operator, +, to build up a string.

  int n = 7;
  System.out.println("I have " + n + " cats.");
runs the System.out.println function with parameter "I have 7 cats.".


Prev Main Next