In Java, how can we concatenate integers using an object of type string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In Java, how can we concatenate integers using an object of type string?

int a = 1; int b = 2; int c = 3; String ddd = new String(""); // do not know what to put here System.out.println(ddd); Desired result: concatenating the digits into 123 (of type string) ddd has been declared as an object of type String and is initialized to an empty string, but I'd like to print the concatenated digits. What statements should I add in order to print 123? I'm stuck because I know that we cannot cast an int to a string.

21st Sep 2021, 6:03 AM
Solus
Solus - avatar
3 Answers
+ 1
Convert it to string by using Integer.toString()
21st Sep 2021, 6:11 AM
Atul [Inactive]
+ 1
@Atul [Inactive] Actually I figured it out: "" + a+b+c haha Can't believe I had forgotten basic concatenation rules ^_^
21st Sep 2021, 6:24 AM
Solus
Solus - avatar
+ 1
Simply concatenate an empty string with the three integers. The + operator behaves as concatenation operator when one of its operands were a String. int a = 1, b = 2, c = 3; String ddd = "" + a + b + c; System.out.println( ddd );
21st Sep 2021, 6:24 AM
Ipang