Solved

print to output window

Posted on 2001-06-17
8
172 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 32
servlet filter example 37 58
JavaFX TableView not displaying correctly 3 16
Is there a simpler dropbox system? 10 18
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…
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 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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 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