In java language we use two print method. One is "print" and second is "println". But why we need an extra command "println" if there is only one difference is that it insert a new line in last but we can use a new line character with print command like we use in c/c++. Why an extra command added in java.
-
There's also `printf` in Java – OneCricketeer Sep 23 '18 at 17:46
-
1It's a very common use case. We don't need it, the class could have had just one method, but multiple methods act as helpers. – Peter Lawrey Sep 23 '18 at 17:47
-
1The question may be not very smart folulated, but niraj17's answer shows that there is something behind it and it is not just the matter of a shortcut. The OP was confused and thought that only adding `\n` would be enough. I don't agree that it is "primarily opinion-based". – Honza Zidek Sep 23 '18 at 17:53
-
@HonzaZidek yes, it's not opinion-based – Andrew Tobilko Sep 23 '18 at 17:53
-
@AndrewTobilko I agree that the question could have been formulated better, but still it deserves considering. Even a trivial question like this may show that the problem is deeper than the OP even expected. I vote for reopening. – Honza Zidek Sep 23 '18 at 17:56
-
The question is related to (but *not* a duplicate of) https://stackoverflow.com/q/207947/2886891 – Honza Zidek Sep 24 '18 at 05:02
1 Answers
I m not sure about my answer, but maybe it is because if we have to give new line by ourselves in c we give printf("\n"), here the problem is that for every operating system new line is different. For example:
in windows - "\r\n"
in unix - "\n"
in mac - "\r"
.
So in order to save the developer from remembering this println
is used.
edits
for mac's new line feed refer this discussion- https://superuser.com/questions/439440/did-mac-os-lion-switch-to-using-line-feeds-lf-n-for-line-breaks-instead-of
Note as suggested in below comment by Honza Zidek
:
1. Java also has printf() method and this one accepts a control sequence %n which is replaced by the proper end-of-line for the target OS.
2. There is a method System.lineSeparator() returning the proper end-of-line marker. See How do I get a platform-dependent new line character?

- 79
- 7
-
-
Apple used to use `\r`. Since they're BSD based now, they may have changed it. – markspace Sep 23 '18 at 17:53
-
good point, `println` resolves this problem by reading the `line.separator` property instead of hardcoding `\n` or any other line separator – Andrew Tobilko Sep 23 '18 at 18:00
-
anyway, `System.out.println("...")` is way more readable than `System.out.print("...\r\n")` – Andrew Tobilko Sep 23 '18 at 18:03
-
I agree with your answer. Just for completeness, you may add the following 2 notes: 1. Java also has `printf()` method and this one accepts a control sequence `%n` which is replaced by the proper end-of-line for the target OS. 2. There is a method `System.lineSeparator()` returning the proper end-of-line marker. See https://stackoverflow.com/q/207947/2886891. – Honza Zidek Sep 24 '18 at 05:01
-
Thanks @HonzaZidek, AndrewTobilko, markspace for your valuable comments – niraj17 Sep 24 '18 at 06:29