CodeGym /Courses /Java Syntax /Concatenation

Concatenation

Java Syntax
Level 3 , Lesson 5
Available

"I'd like to tell you how to merge strings. The process of merging or joining strings is often referred to using the short word 'concatenation'. Cat lovers will find it easy to remember: con-Cat-en-Nation. I'm joking."

"The rules for merging strings are simple. If we 'add' (+) a string and something else, then the 'something else' is implicitly converted to a string via the toString() method."

"Were you talking to me just now?"

"Okay, I'll explain it in an easier way. If we add a string, a number and a cat, then both the number and the cat will be transformed into strings. Here are some examples:"

Code Equivalent code
Cat cat = new Cat();
String text = "The cat is " + cat;
Cat cat = new Cat();
String s = cat.toString();
String text = "The cat is " + s;
int a = 5;
String text = "a is " + a;
int a = 5;
String s = Integer.toString(a);
String text = "a is " + s;
int a = 5;
String text = a + "a is ";
int a = 5;
String s = Integer.toString(a);
String text = s + "a is ";
Cat cat = new Cat();
int a = 5;
String text = "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String text = "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = a + "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String s3 = Integer.toString(a);
String text = s3 + "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = cat + a + "The cat is " + cat + a;
The program won't compile!
The addition operations are executed from left to right, so we get:
String text = (((cat + a) + "The cat is ") + cat) + a;
If we add a cat to a number, there is no automatic string conversion.
// But you can do this:
Cat cat = new Cat();
int a = 5;
String text = cat + (a + "The cat is ") + cat + a;

// This is the same as:
Cat cat = new Cat();
int a = 5;
String text = ((cat + (a + "The cat is ")) + cat)+a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = cat.toString();
String s3 = Integer.toString(a);
String s4 = Integer.toString(a);
String text = s1 + s3 + "The cat is " + s2 + s4;

"The time has come to do a few tasks from Diego."

3
Task
New Java Syntax, level 3, lesson 5
Locked
What's the cat's name?
Help the cat get a name using the setName method.
3
Task
New Java Syntax, level 3, lesson 5
Locked
Cat register
Write code in the addNewCat method to increase the number of cats by 1 each time it is called. The variable catCount corresponds to the number of cats.
3
Task
New Java Syntax, level 3, lesson 5
Locked
Setting the number of cats
Write the setCatCount method. The method must set the number of cats (catCount).
3
Task
New Java Syntax, level 3, lesson 5
Locked
Name register
Finish writing the code of the setName method so that it sets the value of private String fullName to the value of the local String variable fullName.
3
Task
New Java Syntax, level 3, lesson 5
Locked
Count the number of cats
Write a code that correctly counts the number of created cats (count) and correctly displays the count on the screen.
Comments (55)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Antonio5859 Level 3, Mexico
12 June 2023
(Sixty hours multiplied by sixty will give you the minutes and multiplying it again by sixty will give you the seconds.)👩‍💻 This: What is the result of 60 min times 60 sec? Therefore, you multiply This by the parameter they give you.
6 August 2021
everithing´s fine...
17 January 2021
I noticed that the time conversion task only accepted "60 * 60" as the conversation to meet the 2nd criteria. I used "360" in my original code and it was not accepting it in IntelliJ. Just a note.
Drzazgen Level 8, Warsaw, United Kingdom
1 February 2021
Try with "3600"
17 February 2021
🤦‍♂️
Michael Scanlan Level 4, Kalispell, United States
31 December 2020
In the examples the tostring method is used to create several variables from the same variable or object, why do this instead of using the same one multiple times?
Brandon Foust Level 4, Austin, United States
16 December 2020
public static void main(String[] args) { writeToConsole("Hello, World!"); } public static void writeToConsole(String s) { System.out.println("printing: " + s); What is the exact point of this code? Why not just write System.out.println("printing: Hello, World!"); and just save a few lines of code?
John Clune Level 8, London, Canada
17 December 2020
To get you used to using methods for anything that you might need to repeat. Think of a messenger service that puts the users name before the text they are sending.
Michael Scanlan Level 4, Kalispell, United States
31 December 2020
Thanks for explaining my dude, I've been wondering about this as well
Yuxi Zhou Level 9, New York, United States
3 November 2020

Cat cat = new Cat();
int a = 5;
String text = a + "The cat is " + cat + a;
Hey guys, a somewhat tricky question - in this example, how does it come equivalent with

String text = s3 + "The cat is " + s1 + s2; 
? how does s3 comes first? I know there should be s2 and s3 because Integer.toString(a) is not stored so everytime we meet a, we should convert implicitly again and again.
Antonio5859 Level 3, Mexico
12 June 2023
I think it is like this because it is easier to work by duplicating the previous line of code, that is, taking the previous exercise and they only added s3 at the beginning and the result would be the same. Because today no one should make explicit conversions making it difficult for other programmers to understand. In other words, we won't have to worry about people meticulously wanting to examine which variable comes first haha. That is precisely why we use implicit conversion. That is the point, which variable goes first? that is irrelevant if you think about it you hinder true learning still the answer is the same. That is precisely why that was eliminated today so as not to discuss too many irrelevant things that waste the programmer's time and affect his performance. If the work can be automated by a computer, obsessive people should not do it because at the end of the day the computer is never wrong in repetitive work. The only point is to learn how to implicit convert not spend hours thinking what the computer can think. Repetitive processes that do not require much intellect but do require brute force. If you want to make a repetitive process, there is already someone who does it more efficiently than you: the computer. They just copied and pasted these lines: String text = "The cat is " + s1 + s2; -------------------------------------------------- ----- String text = s3 + "The cat is " + s1 + s2; Adding s3 saves you work and time.
Kelly Pepper Level 7, Orlando, United States
10 October 2020
1000 liters per 1 cubic meter; 3600 seconds per hour;
Mariam Level 9, Columbus, United States
5 September 2020
1 cubic meter has 1000 liters.
Switch/Cypher Level 25, Bexleyheath, United Kingdom
5 September 2020
Eurgh, so godamn important to know, and so nothing to do with the coding!!!!!!
Carlitos Martinez Level 4, Villa Mercedes, Argentina
31 August 2020
No se pueden resolver las tareas.
Fer Esquivel Level 3, Coyoacan, Mexico
19 June 2020
We can't verify the tasks. :(