Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate two strings in Java?

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 . "!");     } } 
like image 433
test Avatar asked Sep 20 '10 17:09

test


People also ask

How do you concatenate 2 strings?

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.

How do I concatenate two characters to a string in Java?

String concatenationstr = "" + c; is the worst way to convert char to string because internally it's done by new StringBuilder(). append(""). append(c).


1 Answers

You can concatenate Strings using the + operator:

System.out.println("Your number is " + theNumber + "!"); 

theNumber is implicitly converted to the String "42".

like image 152
Peter Lang Avatar answered Sep 24 '22 06:09

Peter Lang