Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Writer and PrintWriter

Reply
Thread Tools

Writer and PrintWriter

 
 
yzzzzz
Guest
Posts: n/a
 
      03-25-2005
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.
 
Reply With Quote
 
 
 
 
Roland
Guest
Posts: n/a
 
      03-25-2005
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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
 
 
 
yzzzzz
Guest
Posts: n/a
 
      03-25-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PrintWriter and Batch Processing Sharp Tool Java 1 07-09-2005 03:44 PM
Confused with PrintWriter, OutputStream and OutputStreamWriter. CamT Java 1 03-16-2005 08:06 PM
Problems with Cdrom and DVD writer and CD writer Combo reachinout Computer Support 1 09-20-2004 10:02 PM
Isn't DataGrid.Render(writer) supposed to automatically call RenderBeginTag(writer) and RenderEndTag(writer)? Henri ASP .Net Building Controls 1 07-13-2004 10:27 PM
CD writer and Dvd writer problem Gareth not NLL or anybody else. Computer Support 3 04-28-2004 12:41 PM



Advertisments