+ 2

Can someone explain me this code?

public class Program { public static void main(String[] args) { int x = 1; int y = 2; String s = "" + x + y + (x + y); System.out.println(s); } } In this code, I Don't understand the main role of the double quotation marks String s = "" + x + y + (x + y); The double quotation marks convert the numbers to string?

28th May 2017, 6:33 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
4 Respuestas
+ 3
exactly. but first the expression in the brackets will be evaluated first, then everything after the quotation marks will be concatenated to a string. btw the result of the full statement is: 123 hope that helps. :)
28th May 2017, 7:16 AM
Thanh Le
Thanh Le - avatar
+ 1
if you think my answer was of any help for you, please consider marking it as the best answer. thanks in advance. :)
28th May 2017, 8:49 AM
Thanh Le
Thanh Le - avatar
+ 1
thank you sir :) but I have another doubt, is there another way to convert int to string without using double quotation marks?
28th May 2017, 8:53 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
yes, try it with the Integer wrapper object: String numStr = Integer.toString(123);
28th May 2017, 8:58 AM
Thanh Le
Thanh Le - avatar