To print variables in Java, call the
System.out.println
method.// print one string
String stringVariable = "a string";
System.out.println(stringVariable);
// print one integer
int intVariable = 33;
System.out.println(intVariable);
// print one long
long longVariable = 33L;
System.out.println(longVariable);
// print one double
double doubleVariable = 33.0;
System.out.println(doubleVariable);
// print one float
float floatVariable = 33;
System.out.println(floatVariable);
How to print multiple at once?
You can also print multiple variables with System.out.format
method.
String stringVariable = "a string";
int intVariable = 33;
long longVariable = 33L;
double doubleVariable = 33.0;
float floatVariable = 33;
System.out.format("All variables: %s %d %d %f %f", stringVariable, intVariable, longVariable, doubleVariable, floatVariable);
Every variable needs to have a corresponding %
placeholder.
The letters than you put after %
depend on the type of the variable.