Title: String is immutable?
Tags: java;string
Question: Just have a problem with this, how do we rewrite the above code so that only a single String object is created?
String is immutable, isnt studentDetails already one single String object
public void displayString(Student[] students)
String studentDetails = "";
for (Student stu : students)
studentDetails += stu.getFirstName();
studentDetails += " ";
studentDetails += stu.getLastName();
studentDetails += " ";
studentDetails += stu.getAge();
studentDetails += "\n";
System.out.println("Student Details are: ");
System.out.println(studentDetails);
Comment: side bar:  consider allowing `Student` to describe itself by moving `displayString()` to `Student`, or perhaps just override the `Student#toString()` method.

Comment: Maybe you could use String.format() inside the loop.

Comment: Use `StringBuffer` or `StringBuilder`

Comment: @DanielM [The StringBuilder class should generally be used in preference to `[StringBuffer]`](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html).

Comment: yes, String objects are immutable


Here is another answer: Yes , The string object in java is immutable.
The any change in the string will create the new string object.
Whereas, the stringBuilder and StringBuffer are mutable

Here is another answer: yes, strings are immutable, just use a StringBuilder, or a stringBuffer if thread safe is required....
StringBuilder studentDetails = "";
for (Student stu : students)
studentDetails.append(stu.getFirstName());
studentDetails.append(" ");
studentDetails.append(stu.getLastName());
studentDetails.append(" ");
studentDetails.append(stu.getAge());
studentDetails.append("\n");

Here is another answer: String is immutable - Use StringBuilder or StringBuffer (thread-safe) instead
public void displayString(Student[] students)
StringBuilder studentDetails = new StringBuilder();
for (Student stu : students)
studentDetails.append(stu.getFirstName());
studentDetails.append(" ");
studentDetails.append(stu.getLastName());
studentDetails.append(" ");
studentDetails.append(stu.getAge());
studentDetails.append("\n");
System.out.println("Student Details are: ");
System.out.println(studentDetails.toString());
Comment for this answer: synchronization is useless here. Prefer StringBuilder.

Here is another answer: String object is immutable, while the variable studentDetails is not.
When you call  studentDetails += stu.getFirstName(), you created a new String object and assign it to variable studentDetails

Here is another answer: Use StringBuilder:
public void displayString(Student[] students)
StringBuilder studentDetails = new StringBuilder();
for (Student stu : students)
studentDetails.append(stu.getFirstName());
studentDetails.append(' ');
studentDetails.append(stu.getLastName());
studentDetails.append(' ');
studentDetails.append(stu.getAge());
studentDetails.append('\n');
System.out.println("Student Details are: ");
System.out.println(studentDetails.toString());
String is immutable, isnt studentDetails already one single String object
No, since each time you append two Strings with +=, a new String is created.
Comment for this answer: The principle is sound but that will create 4 `String` objects, not one. (Edit: not sure if OP's specification to create a single `String` object might be a bit of XY though).
Comment for this answer: @kumesana there are two in the latest version of the answer (see Eran's comment above). In the previous version the char literals were `String` literals. So you had one for the whitespace, one for the newline, one for the "Student details..." and one for the `toString` invocation.
Comment for this answer: @Mena yes, I noticed that and replaced the single character Strings with chars. Still, there would be a second `String` ("Student Details are: "). I think the only way to avoid that is to append the characters of that constant String one at a time to the start of the StringBuilder, but that would be ridiculous.
Comment for this answer: @Mena I see only one. How do you count?
Comment for this answer: Those are literals, not really created when running this code... I suppose it's a matter of semantics.

Here is another answer: Yes, String object is immutable, that is why one need to work with StringBuilder or StringBuffer. Below are steps shown to work with StringBuilder
Create variable of type StringBuilder
Use append() method to concnat String
Call toString() to convert it into String

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:05/29/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><string>', 'Title': 'String is immutable?', 'ViewCount': 134, 'id': 56360553, 'user_AboutMe': '', 'user_AccountId': 15729299.0, 'user_DisplayName': 'kthxbye', 'user_Reputation': 11.0, 'user_Views': 2.0, 'user_id': 11350011.0}}