CodeGym /Courses /New Java Syntax /Converting to String

Converting to String

New Java Syntax
Level 12 , Lesson 5
Available

"And now it's time for a small but interesting topic: conversions to the String type."

"In Java, any data type can be converted to a String."

"That sounds cool."

"It's better than cool. Almost every type can be implicitly converted to a String. This is easy to see when we add two variables, where one is a String and the other is something else. The non-String variable will be converted to a String."

"Check out a couple of examples:"

Command What really happens
int x = 5;
String text = "X=" + x;
int x = 5;
String s = "X=" + Integer.toString(x);
Cat cat = new Cat("Oscar");
String text = "My cat is " + cat;
Cat cat = new Cat("Oscar");
String text = "My cat is" + cat.toString();
Object o = null;
String text = "Object is " + o;
Object o = null;
String text = "Object is " + "null";
String text = 5 + '\u0000' + "Log";
int i2 = 5 + (int) '\u0000';
String text = Integer.toString(i2) + "Log";
String text = "Object is " + (float) 2 / 3;
float f2 = ((float) 2) / 3;
String text = "Object is " + Float.toString(f2);

Conclusion: If we add a String and 'any other type', the second type will be converted to a String.

"Pay attention to line four in the table. All operations are executed from left to right. That's why adding 5 + '\u0000'" is the same as adding integers."

"So, if I write something like String s = 1+2+3+4+5+"m", I'll get s = "15m" ?"

"Yeah. The numbers will first be added, and then the sum will be converted to a string."

Comments (9)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
24 March 2023
Clear and concise as always
Jonaskinny Level 25, Redondo Beach, United States
15 February 2022
Static Block (from prior lesson) can be used to execute anything by just dumping it in the class file you have open, so static { System.out.println(1+2+3+4+5+"m"); <-- all int's until m string, then it became a string System.out.println("m"+1+2+3+4+5); <-- once a string, each x to the right gets x.toString() }
Agent Smith Level 38
22 August 2020
Don't miss another article from this lesson - How to convert int to String in Java Heavy text formatting makes it easily missable. :)
Onur Bal Level 27, Istanbul, Turkey
13 September 2020
Thanks for these comments of yours.
Austeja Level 10, Kaunas, Lithuania
21 May 2020
Is this really "adding" per se? I thought it was called "concatenating" since we are not really adding anything there. Hm ;/
Andrei Level 41
17 November 2020
The example ""So, if I write something like String s = 1+2+3+4+5+"m", I'll get s = "15m" ?" contains both addition and concatenation.
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
7 August 2019
ok, good link to article at the end of this topic
Syed Tayyab ul Mazhar Level 12, Karachi, China
17 June 2019
This would print m123

System.out.println("m" + 1 + 2 + 3); 

But you can still use parentheses to add the numbers first :

System.out.println("m" + (1 + 2 + 3));  // This would print m6
Muhammad Vahhaaj Level 19, Rawalpindi, Pakistan
3 July 2019
Ofcourse because it changes operators precedence