"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 |
---|---|
|
RainInSpain |
|
RainIn Spain |
|
Rain In Spain |
"To keep bits of text separate, we need to add a space. For example:"
Code | Result |
---|---|
|
56 |
|
5 6 |
|
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 |
---|---|
|
These three examples are equivalent. |
|
|
|
"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."