0

Had a particular problem with a homework assignment trying to print an array. I have no specific code...not looking for a specific answer. It was printing hash code using the print method and consensus is that the .toString() method solves the problem. I couldnt get this to work but found the second answer here:

https://stackoverflow.com/a/19845901/7262393

My question is (if it can be answered generally), why wouldnt I just use a .get() method and loop through all the elements as suggested in this answer? Is there any particular reason one is used over the other? I assume the .toString method is a loop that goes and converts each element to a string and then prints them.

Thanks

Community
  • 1
  • 1
Dan
  • 758
  • 6
  • 20
  • Over an array ? Here is the result of `toString()` on a `String[]` with `foo` and `bar` `[Ljava.lang.String;@3e25a5`. So where do you see a loop ? – AxelH Dec 07 '16 at 14:46
  • `toString()` doesn't loop over an array. The default simply prints the hashcode. But usually when people override the `toString()` in some class, they make it print the object's properties/attributes/fields/global vars. – denvercoder9 Dec 07 '16 at 14:49
  • Sorry. Not prints them. Converts each...maybe im thinking wrong. Overriding it goes through and converts each. Its a method right? So its code must be somewhere. – Dan Dec 07 '16 at 16:05

3 Answers3

0

When using a .get() method to concatenate multiple fields to a String you are going to sacrifice some performance, because for each + operation a new String needs to be created (Java Strings are immutable).

When using a .toString() method you could use something like:

String.format("Person {name:%s, street:%s}",name,street);

which would have much better performance.

S. Dragomir
  • 318
  • 2
  • 11
0

I suppose, Apache Commons ReflectionToStringBuilder will help you. Please, check this document for details.

It can be used in next way:

ReflectionToStringBuilder.toString(<object you want to print to string>);
nndru
  • 2,057
  • 21
  • 16
0

Well which would you rather do every time you want to see the contents of a container object? Loop over it and print each contained item out or call System.out.println(obj.toString());?

In general it's better to add toString() methods for your classes because that's likely something you are going to want to do a lot when debugging, and it will save you duplicating your effort many times!

daphtdazz
  • 7,754
  • 34
  • 54
  • Thanks! At the moment im handcuffed by the rules of the assignment. "Dont change your class file" Therefore....looping over it. I really appreciate all the responses. Thank you. – Dan Dec 07 '16 at 15:42
  • Can you subclass the class and add the `toString()` method, then just use the subclass? (And don't forget to upvote the answers you think are useful! Thanks is nice, but it isn't upvoting :-).) – daphtdazz Dec 07 '16 at 16:01
  • You got it. I was just looking for a reason. Want to learn why, not just how. I never remeber anythi g without a reason. – Dan Dec 07 '16 at 16:02
  • oops, sorry, forgot that! @Dan dude you need to get some more rep :-P – daphtdazz Dec 07 '16 at 17:17