undefined

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

undefined
1
Task
Java Syntax, level 3, lesson 5
Locked
Printing strings
I foresee a recurring need to print strings in the life of a programmer! To print a string, you have to use a specific method... or write your own that has some special features. This task is different in that our method shouldn't simply display a string. Instead, it should change the string by adding the word "printing".
undefined
3
Task
Java Syntax, level 3, lesson 5
Locked
Time conversion
Hours are few, but seconds are many. Perhaps measuring time in seconds would give the illusion that we have more time than we really do? Anyway, enough philosophizing. Let's work on programming. We need to implement a method that will convert hours to seconds, and then we'll see what this gets us.
undefined
1
Task
Java Syntax, level 3, lesson 5
Locked
Fill a pool with water
Today our task is to fill a pool with water. In doing so, we mustn't drown anyone, but we also don't want to hold back: we'll fill it to the brim! The filling method will take the dimensions of the pool. We will assume that it is a parallelepiped, i.e. it has a well-defined length, width and depth. And the method will return the required amount of water. In liters.
Comments (49)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
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.
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?
Yuxi Zhou Level 8 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.
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.
Devonte A Level 18 Rubery United Kingdom
22 April 2020
1 hour = 3600 seconds :)
Daniel Malloy Level 3 Temecula United States
11 April 2020
I very much dislike Time Conversion ):
SYED ATHAR RAHMAN Level 3 Jaipur India
1 April 2020
cannot do last one
Pete Latham Level 15 Swindon United Kingdom
26 August 2019
Filling the pool - the task description does not specify what units a, b & c represent. It is up to us to assume that they're in metres but this isn't always a safe assumption.