Concept
How does string concatenation work in Java?
ComputerScienceOne / String Methods
"Java has a concatenation operator built into the language. The familiar plus sign, +, can be used to combine one or more strings by appending them to each other. Concatenation results in a new string. String firstName = \"Tom\"; String lastName = \"Waits\"; String formattedName = lastName + \", \" + firstName; //formattedName now contains \"Waits, Tom\". Concatenation also works with numeric types and even objects. int x = 10; double y = 3.14; String s = \"Hello, x is \" + x + \" and y = \" + y; //s contains \"Hello, x is 10 and y = 3.14\". When used with objects, the concatenation operator internally invokes the object's toString() method. The plus operator as a concatenation operator is syntactic sugar. When the code is compiled, it is replaced with equivalent code that uses a series of the StringBuilder class's append() method. StringBuilder is a mutable version of the String class."
Related Ideas
- How can StringBuilder be used to format a name?ComputerScienceOne · Arrays of Strings
- How can the toString() method represent a Java object?ComputerScienceOne · Common Methods
- How does Java handle passing primitive and object types to methods?ComputerScienceOne · Passing By Reference
- How does Java make working with strings easier?ComputerScienceOne · Basics
- How are String variables declared and reassigned in Java?ComputerScienceOne · Basics
- How do you concatenate strings in PHP?ComputerScienceOne · String Functions
- How can strings be concatenated in PHP?ComputerScienceOne · String Concatenation
- How is operator precedence determined in Java?ComputerScienceOne · Order of Precedence