1

My friend asked a question

Why Java doesn't support function like clrscr in C?

The answer given by SO's users were

John3136 : since Java is cross platform and can be run without a console, the meaning of clrscr has to change depending on how the app is run and on what platform.

mvp : Java was designed to be write/compile once, run anywhere. And this function does not quite fit into this agenda.

My question is that isn't the cross platform discrepancies handled by JVM? How would a function like Thread.sleep() and System.out.println() work then? They also depend on the underlying platform like the threads are implemented [may be] differently in Linux and in Windows?

My main concern is that if function like System.out.println() works in different platform then it is possible to implement clrscr for different platforms as well. I am a little skeptical on John's and mvp's answer

Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • Java's console streaming is considered to be notoriously slow. Console apps are not really the focus of java, so probably the designers (as well as most of the users) just didn't think it was worth the effort to write that, since it would require a lot of nitpicky code... Clearing a screen is different depending on not only the system, but also what console you're running as well. It's a lot of work to get that working. – scott_fakename Jul 14 '13 at 05:41
  • @cHao : I know I have referenced the same question .read the post – Bhavik Shah Jul 14 '13 at 06:52
  • You have obviously never had to battle with the hundreds of different kinds of terminals, each with their own escape codes, that have been manufactured over the decades, and which are all incidentally obsolete. clrscr() is only possibly because the Windows DOS box obeys the ANSI control codes, or because of the existence of the Unix 'curses' library, which NB requires user configuration to get right. – user207421 Jul 14 '13 at 07:00
  • @scott_fakename Considered notoriously slow by whom? Surely it's the console itself that is slow? There's no reason I can see why the Java part would be the rate-determining step. – user207421 Jul 14 '13 at 07:01
  • @EJP probably right. This is stuff that people say when I'm talking to them face to face -- I have no concrete evidence to back that up. Java does take a long time to start for any program, however, and so is not typically well suited to console apps (which frequently are expected to be to start and fast to end). You make a fair point though. – scott_fakename Jul 14 '13 at 07:17
  • @BhavikShah: "I don't like the answers, lemme ask again" does not make this any less a duplicate. – cHao Jul 14 '13 at 15:25

3 Answers3

1

System.out.println uses the standard output stream, which can be a console or something else (say you IDE's output window for example).

The console itself can be accessed with System.console() which, as pointed out by the documentation, can return null. In that situation it is not clear what clrscr would be supposed to do.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • So when System.console returns null what does System.out.println() does? The same can be done for clrscr. – Bhavik Shah Jul 14 '13 at 05:50
  • 1
    System.out is a printstream -- and a printstream can wrap a file stream, for instance. So if you programmatically reassigned system.out to a file so that you could log it, then System.out.println() would write to a file, but what then would clrscr do? Erase you log? It just wouldn't make sense to have it as a feature. – scott_fakename Jul 14 '13 at 06:08
1

The problem is that a reliable implementation of clrscr requires the library method to know what kind of device it is connected to ... so that it can do the appropriate thing to clear the screen. A library might be able to know how to do this on Windows, but it can't be done on all platforms.

The general philosophy has been that if some functionality cannot be implemented across all supported platforms, then standard Java classes won't support it.

However, this doesn't mean that you can't implement your own library to do clrscr yourself ... after having figured out how to do it on all platforms that matter to you. Or you could find a suitable 3rd-party library. You risk making your application non-portable, but that's your concern.

The other explanation is that the Java SE libraries don't provide any support for old-school terminals / terminal emulators. (The equivalent of the old "termcap" libraries.) Why? Because there is little call for it in these days of computers and phones with graphic screens. (I can't remember the last time I used a real 24x80 terminal ... but it was probably more than 25 years ago!)


I am a little skeptical on John's and mvp's answer:

It is hard to address your disbelief. Maybe the only thing that will convince you is for you to try to implement clrscr in Java yourself across a wide range of platforms and output devices ...

How would a function like Thread.sleep() and System.out.println() work then?

All operating systems (or at least all that are capable of supporting Java) provide native API's / libraries with the required functionality.

  • In the case of Thread.sleep, the JVM implementation typically uses the POSIX sleep(...) function.

  • In the case of System.out.println, out is a PrintWriter and the JVM creates a PrintWriter that is connected to the standard output device. The println call most likely results in a call to the POSIX write function.

This is NOT the case for clrscr. This functionality is not supported by the C standard libraries.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for explaining it..I'll accept your answer and upvote assylias's answer since he was talking the same thing..I don't want to be unfair :) – Bhavik Shah Jul 14 '13 at 06:51
0

As mentioned in the question that you referenced, C does not support clrscr as a standard feature. Since the console is being cleared, it implies the existance of a O.S. system call that allows to do this. Assuming not all O.S.'s implement this system call, it would simply not be possible to implement it in Java or in fact in any other language as a standard feature.

Tarik
  • 10,810
  • 2
  • 26
  • 40