Has the standard Java output function already accepted multiple parameters?

Question:

My teacher taught the class to use System.out.println("Mensagem") like this for the class (the type of total_alunos doesn't matter):

System.out.println("Existem ", total_alunos, " alunos na classe.");

After some errors popping up on the computers, I said that it was necessary to concatenate the string through the + , which worked, but she said that the comma should also work. At some point, was it possible to execute this function in the way presented above? If yes, in which version of Java?

Answer:

Maybe your teacher is confusing println() with printf() , because something similar to what you want can be done like this:

public class Teste {
    public static void main(String[] args) {
        int totalAlunos = 10;
        System.out.printf("Existem %d %s", totalAlunos, " alunos na classe.");
    }
}

Result:

There are 10 students in the class.

According to the documentation printf() accepts a variable number of parameters and its signature is as follows:

public PrintStream printf(String format, Object... args)

PS: Pay attention to the %d and %s inside the first pair of double quotes that are the format specifiers of the other arguments, unlike as it is in the code of your question that doesn't have them.

println() does not accept several parameters and there is no way to pass several arguments to it other than concatenating them and transforming them into just one, however, there is no way to concatenate with a comma nor has there ever been.

Scroll to Top