views:

233

answers:

9

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 . "!");
    }
}
+1  A: 

"+" instead of "."

Zenzen
OK, thank you! :)
Dan
+10  A: 

You can concatenate Strings using the + operator:

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

theNumber is implicitly converted to the String "42".

Peter Lang
`"!");` [ ](http://.)
KennyTM
@KennyTM: Thanks, corrected :)
Peter Lang
Aha! Another mistake I didn't see - sweeet.
Dan
+4  A: 

The concatenation operator in java is +, not .

Read this (including all subsections) before you start. Of try to stop thinking the php way ;)

To broaden your view on using strings in Java - the + operator for strings is actually transformed (by the compiler) into something similar to:

new StringBuilder().append("firstString").append("secondString").toString()
Bozho
Aha, right right - Silly mistake.
Dan
@Erick Robertson you are wrong. Reread the javadoc of `java.lang.String`. Using `+` is inefficient inside a loop. Otherwise the compiler uses `StringBuilder`
Bozho
@Erick: you're confusing `+` with `+=`.
BalusC
@Bozho: Wow, this is new to me. It wasn't always like this. I will stop explicitly using `StringBuilder` now when I can do the same thing with a sequence of `+`'s. I will still use it when concatenating strings across multiple lines, however, like when strings are being conditionally appended and such. Thank you!
Erick Robertson
+2  A: 

Use + for string concatenation.

"Your number is " + theNumber + "!"
Greg
Ahah, thanks! :D
Dan
A: 

You must be a PHP programmer.

Use a + sign.

"Your number is " + theNumber + "!"

Ed Manet
Ahah yes - i get them mixed up a bit.
Dan
A: 

In Java, the concatenation symbol is "+", not ".".

Uncommented
thanks!! :) haha.
Dan
+1  A: 

This should work

public class StackOverflowTest
{  
    public static void main(String args[])
    {
        int theNumber = 42;
        System.out.println("Your number is " + theNumber + "!");
    }
}
Bragboy
Correcto-mundo good sir!
Dan
A: 

"+" not "."

But be careful with String concatenation. Here's a link introducing some thoughts from IBM DeveloperWorks.

JohnMetta
A: 

There are two basic answers to this question: 1. [simple and inefficient] Use the + operator (string concatination).

"your number is" + theNumber + "!" (as noted elsewhere) 2. [less simple and less inefficient]: Use StringBuilder (or StringBuffer).

StringBuilder value;
value.append("your number is");
value.append(theNumber);
value.append("!");

value.toString();

I recommend against stacking operations like this:
new StringBuilder().append("I").append("like to write").append("confusing code");
Spaceisavaluablecommodity,asthissentancedemonstrates.

dwb
JavaC converts "+" to use StringBuilders please see http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java
ArtB