0

I would like to map System.out.println on another function, such as:

public static void puts(Object o) { System.out.printl(o); }

This goes against some Java best-practice, or can have any drawbacks I did not think about?

b3by
  • 114
  • 4
  • 15
  • 1
    Is this just because `System.out.println` is so damn wordy, or do you have another reason? – user2357112 May 12 '14 at 23:30
  • Other than boxing, I see no issues. – Sotirios Delimanolis May 12 '14 at 23:30
  • Is there any reason why you would like to create such method? If you want to reduce `System.out.println` you remove `System` by static import of `System.out`. – Pshemo May 12 '14 at 23:31
  • If declared within a class, it can be used in conjunction with something like `private static final String _CLASS_NAME = MyClass.class.getName()`, for logging purpose (id est, the `puts` method could do `System.out.println(_CLASS_NAME + ": " + o)`). – b3by May 12 '14 at 23:35
  • Got it. It is useless, but still, it does not seem to be wrong (even if these two are quite similar). – b3by May 12 '14 at 23:41
  • 1
    I think if you're referring directly to `System.out` in so many places that it's become cumbersome, *that* is what's against Java best practice. Classes/methods should generally have a output stream given to them, not just grab a static reference to one. – Chris Martin May 12 '14 at 23:49

1 Answers1

1

You can do that if you want. When you call the method, it will work the same as System.out.println, in most cases.

The problem is if you try this:

puts(new char[] {'h', 'e', 'l', 'l', 'o'});

If you do that with System.out.println, you will get hello. But if you do it with puts, you get something like this:

[C@812f71

If you don't print char[]s, you will be just fine.

tbodt
  • 16,609
  • 6
  • 58
  • 83