I am trying to concatenate strings in Java. Why isn't this working?
public class StackOverflowTest { public static void main(String args[]) { int theNumber = 42; System.out.println("Your number is " . theNumber . "!"); } }
You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.
String concatenationstr = "" + c; is the worst way to convert char to string because internally it's done by new StringBuilder(). append(""). append(c).
You can concatenate Strings using the +
operator:
System.out.println("Your number is " + theNumber + "!");
theNumber
is implicitly converted to the String "42"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With