Link to home
Start Free TrialLog in
Avatar of ken021600
ken021600Flag for Australia

asked on

print to output window

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.

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
Avatar of ken021600

ASKER

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
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.


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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cheers mate!
ken
Only a B :-)

Thanks for the points.