SwadhyAI Notes

All Java notes

Java output

›Short versionOpen if you only have a minute.
  • println writes its argument and then a newline. print writes the argument and stays on the line.
  • printf fills placeholders and does not add a newline unless the pattern contains %n.
  • A format that does not match its argument compiles, then throws when that line runs.
  • Inside quotes, \n, \t, \", and \\ are single characters. A stray backslash is a compile error.
  • Once either side of + is a string, later + joins text. 2 + 3 + " points" and "Score: " + 2 + 3 are different results.

The console is the text window attached to the program. Almost every early program talks to it through System.out, and the choice that matters is whether the next write starts a new line.

java
System.out.println("Hello World");
System.out.println("Welcome to Java");

What the console shows

Hello World
Welcome to Java

is the usual call. It writes the text, then moves down. Two calls therefore stack. writes and stays put, so the next call continues on the same line.

java
System.out.print("Hello ");
System.out.print("World");

That pair prints Hello World with no line break between the calls and none after the last one. Add a println with an empty argument when you want to finish the line and nothing more.

A pattern, then the values

is for the case where the shape of the line matters: a name beside an age, or a price held to two decimal places. The pattern is the first argument. Each placeholder consumes the next argument.

java
String name = "Alice";
int age = 25;
System.out.printf("Name: %s, Age: %d", name, age);

The console shows Name: Alice, Age: 25 and does not move to the next line. %s expects text, %d expects an integer, and %.2f expects a floating-point number printed with two digits after the decimal. %n is the newline you put in the pattern when you want one.

java
double price = 19.99;
int quantity = 3;
System.out.printf("Price: $%.2f%n", price);
System.out.printf("Quantity: %d%n", quantity);
System.out.printf("Total: $%.2f%n", price * quantity);

What the console shows

Price: $19.99
Quantity: 3
Total: $59.97

Characters the quotes cannot hold plainly

A is how a string contains a character that would otherwise end the string or that you cannot see. The source shows two characters. The string stores one.

java
System.out.println("Hello\nWorld");
System.out.println("Name:\tJohn");
System.out.println("She said \"Hi\"");
System.out.println("C:\\Users");

The first call prints Hello, then World on the next line, because \n is a newline inside one string. The second puts a tab between the label and the name. The third includes the quote marks around Hi. The fourth prints a single backslash, so the console shows C:\Users. In the source that backslash is written twice.

System.out.println(Hello) is a different failure. Without quotes, Hello is a name, and no variable by that name exists, so the file does not compile. "C:\\new" does compile: \n is a newline, so the string is C:, then a line break, then ew. "C:\\Users" does not compile, because \U is not an escape.

Text and numbers on one line

with + is the other way to mix text and values. The operator looks like addition. It stops being addition as soon as one side is a string, and it decides that one + at a time from the left.

java
String name = "Bob";
int age = 30;
double height = 5.9;

System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Height: " + height + " feet");
System.out.println(name + " is " + age + " years old");

What the console shows

Name: Bob
Age: 30 years
Height: 5.9 feet
Bob is 30 years old

The height line prints 5.9 because that value is a double written next to text. Put the arithmetic where you mean it. "Score: " + 2 + 3 prints Score: 23. "Score: " + (2 + 3) prints Score: 5, because the parentheses add before either number meets a string.

What does System.out.println(2 + 3 + " points") print, and what does System.out.println("points: " + 2 + 3) print?

The first prints 5 points. Both sides of the first + are ints, so they add, and only then does the sum meet the string. The second prints points: 23. The left side is already a string, so each later number is converted and joined.

Quotes, names, and the semicolon that ends a print are the same rules as on Java syntax. The values you store before you print them are the subject of Java variables.