Solved

print to output window

Posted on 2001-06-17
8
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6200546
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
ID: 6200672
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
ID: 6201693
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6204383
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
 

Author Comment

by:ken021600
ID: 6205359
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
ID: 6205669
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
ID: 6205700
cheers mate!
ken
0
 
LVL 92

Expert Comment

by:objects
ID: 6205735
Only a B :-)

Thanks for the points.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

691 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