Writer and PrintWriter

Discussion in 'Java' started by yzzzzz, Mar 25, 2005.

  1. yzzzzz

    yzzzzz Guest

    Hi,

    What is the difference between PrintWriter.write(String) and
    PrintWriter.print(String)?

    And how can PrintWriter.write not throw IOException when Writer.write
    does? Should the overriding method have the same signature?

    Thanks.
     
    yzzzzz, Mar 25, 2005
    #1
    1. Advertising

  2. yzzzzz

    Roland Guest

    On 25-3-2005 17:08, yzzzzz wrote:

    > Hi,
    >
    > What is the difference between PrintWriter.write(String) and
    > PrintWriter.print(String)?


    PrintWriter.print(String) prints the literal string "null" if the
    argument is null. Otherwise it's the same as PrintWriter.write(String)
    Source from 1.4.2
    public void print(String s) {
    if (s == null) {
    s = "null";
    }
    write(s);
    }


    >
    > And how can PrintWriter.write not throw IOException when Writer.write
    > does? Should the overriding method have the same signature?
    >
    > Thanks.


    The signature of a method does not include the thrown exceptions, only
    its name, and the number and types of its parameters. See JLS §8.4.2
    <http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38649>

    And from JLS §8.4.4
    <http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78323>:
    "A method that overrides or hides another method [...] may not be
    declared to throw more checked exceptions than the overridden or hidden
    method."
    In other words, a method overriding a method of a parent class may be
    declared with fewer checked exceptions.
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Mar 25, 2005
    #2
    1. Advertising

  3. yzzzzz

    yzzzzz Guest

    Roland wrote:
    > On 25-3-2005 17:08, yzzzzz wrote:
    >
    >> Hi,
    >>
    >> What is the difference between PrintWriter.write(String) and
    >> PrintWriter.print(String)?

    >
    > PrintWriter.print(String) prints the literal string "null" if the
    > argument is null. Otherwise it's the same as PrintWriter.write(String)
    > Source from 1.4.2
    > public void print(String s) {
    > if (s == null) {
    > s = "null";
    > }
    > write(s);
    > }


    Hm. Weird!


    >> And how can PrintWriter.write not throw IOException when Writer.write
    >> does? Should the overriding method have the same signature?
    >>
    >> Thanks.

    >
    > The signature of a method does not include the thrown exceptions, only
    > its name, and the number and types of its parameters. See JLS §8.4.2
    > <http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38649>
    >
    > And from JLS §8.4.4
    > <http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78323>:
    >
    > "A method that overrides or hides another method [...] may not be
    > declared to throw more checked exceptions than the overridden or hidden
    > method."
    > In other words, a method overriding a method of a parent class may be
    > declared with fewer checked exceptions.


    Ok. That does make sense.
     
    yzzzzz, Mar 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oz Levanon
    Replies:
    1
    Views:
    2,135
  2. CamT
    Replies:
    1
    Views:
    4,443
    Oscar kind
    Mar 16, 2005
  3. Sharp Tool

    PrintWriter and Batch Processing

    Sharp Tool, Jul 9, 2005, in forum: Java
    Replies:
    1
    Views:
    485
    John B. Matthews
    Jul 9, 2005
  4. DaVinci
    Replies:
    1
    Views:
    582
    Piotr Kobzda
    Oct 5, 2006
  5. Henri
    Replies:
    1
    Views:
    394
    Henri
    Jul 13, 2004
Loading...

Share This Page