I’m moving into Java from a C++ background and I’m currently getting to grips with the notion of not overloading operators, as is the Java way. I’m currently trying to figure out how to handle a situation where, if I were writing in C++, I would overload operator<< for my class and call it a day.
How should this situation be handled in Java? Is there a specific procedure with specific syntax, or it is as simple as writing something like
public void PrintMyStuff( final MyClass myClass ){
System.out.print( /*MyClass member1*/ );
System.out.print( /*MyClass member2*/ );
}
I think you’ve answered your own question. To put a little more “ritual” and “the right way to do things” into things – the convention is for this “printing method” to be called toString(), and it should return a String – that String is then printed explicitly.
(…though I won’t tattle on you if you just print in the method like you want to… especially if it’s only for your own purposes…)
Here is the salient link about this convention: http://www.javapractices.com/topic/TopicAction.do?Id=55