0

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);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
kthxbye
  • 11
  • 3

6 Answers6

5

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

xingbin
  • 27,410
  • 9
  • 53
  • 103
3

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    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). – Mena May 29 '19 at 12:26
  • 1
    @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. – Eran May 29 '19 at 12:28
  • @Mena I see only one. How do you count? – kumesana May 29 '19 at 12:28
  • @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. – Mena May 29 '19 at 12:31
  • Those are literals, not really created when running this code... I suppose it's a matter of semantics. – kumesana May 29 '19 at 12:54
1

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());
}
DanielM
  • 3,598
  • 5
  • 37
  • 53
0

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
Shravan40
  • 8,922
  • 6
  • 28
  • 48
0

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");  
   }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

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