CodeGym /Courses /Java Syntax /Screen output once again

Screen output once again

Java Syntax
Level 3, Lesson 3
Available

"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."

Comments (120)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
qaz_gangnam Level 16, Korea, Republic of
3 May 2023
I tried to run the task on IntelliJ but it didn't work, it said "Abnormal build process termination", I could verify it though, but it didn't run
David Level 6, Spain
16 March 2023
Noto que realmente estoy avanzando, estos métodos funcionan. Mil Gracias!!!
Lunita Level 4, Dominican Republic
4 January 2023
Now I'm completing all my tasks on IntelliJ. I'm excited!
ValPereira Level 7, Minas Gerais, Brazil
1 November 2022
Apoio a proposta do usuario Anjo Nível 3 que pede que as tarefas seja em pdf disponivel para dowload. Minha gratidão.
Angel Level 3, Spain
11 July 2022
I propose to have a downloadable pdf of each lesson that is studied, in which among the necessary to solve the proposed exercises. Since in the malloría is given by known aspects that have not been studied for a novice. Thanks for the app
22 June 2021
This is so much better than Jetbrains Academy.
MD Alice Level 3, Petah Tikva, Israel
19 June 2021
Really cool version of learning process! Thank you, developers of this course!
Fabricio Level 3
29 April 2021
I also have the same issues as other fellow students and I only see lots of complaints from users and not a solution. Not a good sign for this app :/
Juan Gimenez Level 3, Buenos Aires
27 April 2021
Why there isnt any task? I cant continue like this, this was a cool site.
John Squirrels Level 2, San Francisco, Poland
29 April 2021
Por favor, veate el mensaje privado.
Lorenzo Level 18, Poland
17 January 2021
not working anything!
Olha Level 40, Fremont Expert
18 January 2021
Please contact the support team at support@codegym.cc for further assistance.