"Long ago, computers could only display text. Programs displayed data on the screen after receiving input from the keyboard. This is called a 'console user interface' or simply the 'console'. A window interface is an alternative to the console. With this type of interface, the user interacts with the program through one or more windows. Since we're just learning how to program, we'll start by working with the console."

"All right."

"Text is displayed on the console (screen) consecutively, line by line. The text is entered using the keyboard. To avoid mistakes, the keyboard input is displayed on the screen. Sometimes it looks like the human user and the program are taking turns writing things on the screen."

"You can use the System.out.print() method to display text on the screen. This method simply displays the text, while System.out.println() displays the text and moves the cursor to the next line."

Code Result
System.out.print("Rain");
System.out.print("In");
System.out.print("Spain");
RainInSpain
System.out.print("Rain");
System.out.println("In");
System.out.print("Spain");
RainIn
Spain
System.out.println("Rain");
System.out.println("In");
System.out.println("Spain");
Rain
In
Spain

"To keep bits of text separate, we need to add a space. For example:"

Code Result
int a = 5, b = 6;
System.out.print(a);
System.out.print(b);
56
int a = 5, b = 6;
System.out.print(" " + a + " " + b);
5 6
int a = 5, b = 6;
System.out.print(" The sum is " + (a + b));
The sum is 11

"Got it"

"This lets you display anything on the screen: all Java objects can be transformed into a string. All Java classes derive from the Object class, which has the toString() method. This method is called when you want to transform an object into a string."

Code Description
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat);
These three examples are equivalent.
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat.toString());
Cat cat = new Cat("Oscar");
String catText = cat.toString();
System.out.println("The cat is " + catText);

"But my program displayed 'The cat is com.codegym.lesson3.Cat@1fb8ee3'. What in the world is that supposed to mean?"

"The Object class's standard toString() method returns a string consisting of the class name and the object's memory address (in hexadecimal form)."

"Uh-huh. And what good could possibly come from such a method?"

"You can write your own implementation of toString() in your class. Then that is the method that will be called."

"Really? All right."

"Here are some tasks from Diego."