• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintWriter format and printf methods

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain the reason that these methods returns itself?

There must be some benefit, but for the life of me, I can't see what it is.

I've looked around a bit, but haven't even found any examples where printf or format are called and the return value is used.

Thanks.

Kevin
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that it makes it easy to chain calls. Maybe for printf() that's not really something you'd like to do, but for example with the append() method it could be handy:

Although I wouldn't recommend writing code like that.
 
Kevin Crays
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So are you saying that with these methods, there's no reason to chain the calls and thus there's really no reason to have them return a PrintWriter?

Thanks again,

Kevin
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chaining is a style that some people like and some don't. It's popular in functional languages. One place where I like to use it is when I need to create a tempory object, set a few of its properties and extract a result:
 
Kevin Crays
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff. I'm familiar with chaining and I think your SelectStatement example is a great example of where it makes the code easier to read, but I just don't see how it's useful with a printf or format statement. I'm not even sure it's that useful when appending, since that could almost certainly be more cleanly implemented as a single format or printf statement.

For example,


is basically the same as writing


I realize I could also use it to flush, close or whatever, but it seems like you'd rarely want to close a file, unconditionally, right after writing to the file.

I guess I'm just trying to understand when the ability to chain format or printf would be useful.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I'd don't see the big advantage to having *some* methods allow chaining, especially why other PrintWriter methods don't. For example, you can write:

but not

Because all the print methods have void return type. (One could argue that it's time to use printf, but not everyone likes using formating when they don't have to...)

Did you notice the cool thing happening in PrintWriter? It implements Appendable:

And when it implements these methods, it uses Java 1.5's covariant return types to change the return type of these methods to PrintWriter. (I can't think of another place in the API that they use CRT ...) The API for Appendable also states:

The Appendable interface must be implemented by any class whose instances are intended to receive formatted output from a Formatter.

A-ha...
 
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic