CodeGym/Java Course/Java Syntax/Screen output once again

Screen output once again

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)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
qaz_gangnam
Level 16 , Korea, Republic of
3 May 2023, 09:57
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, 11:00
Noto que realmente estoy avanzando, estos métodos funcionan. Mil Gracias!!!
Lunita
Level 4 , Dominican Republic
4 January 2023, 02:23
Now I'm completing all my tasks on IntelliJ. I'm excited!
ValPereira
Level 7 , Minas Gerais, Brazil
1 November 2022, 18:48
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, 08:34
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
schipplock Backend Developer at n/a
22 June 2021, 19:55
This is so much better than Jetbrains Academy.
MD Alice
Level 3 , Petah Tikva, Israel
19 June 2021, 16:38
Really cool version of learning process! Thank you, developers of this course!
Fabricio
Level 3
29 April 2021, 17:05
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, 03:34
Why there isnt any task? I cant continue like this, this was a cool site.
John Squirrels Website Admin at CodeGym
29 April 2021, 15:14
Por favor, veate el mensaje privado.
Lorenzo
Level 18 , Poland
17 January 2021, 16:28
not working anything!
Olha
Level 40 , Fremont
Expert
18 January 2021, 10:38
Please contact the support team at support@codegym.cc for further assistance.