Solved

print to output window

Posted on 2001-06-17
8
169 Views
Last Modified: 2010-04-16
Hi experts,
I have a question about "printing to output window". if i want to print some stuff to the output window, one way is:
System.out.println("hello");

an alternative way is:
FileWriter fw = new FileWriter(FileDescriptor.out);
PrintWriter pw = new PrintWriter(fw);
pw.println("hello");

any difference between these two approaches? Thank you for your help.

ken
0
Comment
Question by:ken021600
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
System.out is a PrintStream, as opposed to a PrintWriter so there are differences. Check the javadoc for more details.
Your example uses a println in one case, and a print in the other so this will also cause a difference.

0
 
LVL 3

Expert Comment

by:Sendoh
Comment Utility
hi !
think the other differences is ur program will take up more resources as the System.out is already define, no point define a new priwriter just for this purposes(printing hello to screen) :P

haha
0
 

Author Comment

by:ken021600
Comment Utility
Thanks a lot for your help.

anyways,i have to point out:
1)"println" and "print" are not the issue, just a typo.

2)I've already checked out the java API. I posted this question here coz i didn't get anything from reading the API, not coz i'm lazy...so what on earth are the differences between PrintStream and PrintWriter?

3)printing "hello" to the standard output is just a simple way to ask this question. it makes life easier without losing the point...

thanks,
ken
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
They handle new line and flushing differently:

PrintStream uses '\n' as a new line character.
PrintWriter uses the system defined new line character.

PrintStream autoflushs whenever a new line character appears in input.
PrintWriter only autoflushs if a println() is called.


0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ken021600
Comment Utility
aha you are Aussie! (i live in Sydney anyway)i didn't know that till just now...i had a look at your profile.

Thanks for your help. but why did those Java developers bother to develop two ways to do it?

thanks,
ken
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
I'm in Sydney too :)
Not exactly sure on the reason for two ways, but could be so that for each OutputStream class, there is a corresponding Writer class.

0
 

Author Comment

by:ken021600
Comment Utility
cheers mate!
ken
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Only a B :-)

Thanks for the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now