Saturday, May 24, 2008

Why Java does not have Clear Screen functionality?


Why Java does not have Clear Screen functionality?


Clear Screen functionality has not been implemented in Java as it's simply not possible without sacrificing Platform Independence. So, how to handle a situation asking this? Well... Java has not been designed to develop applications requiring a direct console based interaction. You may think of using Swing or AWT based GUI instead. It's not impossible to have such a functionality in Java, but certainly not portable and hence not preferrable. We do have few platform dependent workarounds to achieve it in Java and the best among the lot still remains the same - using JNI.


In Java, we don't have the concept of a typical console as we have in C/C++. Instead all I/O in Java is stream-based. I don't think Clear Screen can be implemented for a stream as the steam may point to a file, a printer (it will be a really challenging scenario, isn't it?), or may be to a console on certain platforms. Since, you never know what this stream would be pointing to at run time, you can't assume it to be a console in all the cases and hence can not have that functionality. Clear Screen will obviously make sense only for clearing the console and not for a file (logs etc.).


System.out.println() is far more easier to implement in all environments maintaining platform independence as irrespective of what the stream points to at runtime, it can always be written on. This holds true for C/C++ as well. And probably this is the reason why 'printf()' works fine in all environments in case of C/C++ (Now in Java also ... Java 5.0 onwards) and not 'clrscr()'.


C++ also has the concept of Streams similar to that in Java and I think Clear Screen function is not a part of any class based on streams. It's a separate method altogether and it has a specific implementation only for those environments which have the concept of Console. For different platforms we may need to look for different vendor specific extension to have that functionality even in case of C/C++. For instance, Turbo C/C++ has the function 'clrscr()' in the vendor specific header file 'conio.h'. We don't have that header file available for Linux platform.


Similar explanation can be given for other platform dependent tasks such as 'accepting input without ENTER being pressed - a functionality getch() provides in C/C++', 'applying font color, background color, etc. on the console', and so on. Such tasks have not been implemented consistently across all platforms even in C/C++, so forget about Java. The goals of Java will probably never allow such tasks to be implemented not at least by using standard library functions. You got to use platform-dependent vendor specific packages to achive them unless of course, all the environments start following the same standards for console and other related stuff.


I thank Lavnish (one of our regular visitors) for raising this point in response to the post on 'Limitations of Java Language'. He runs a blog and writes frequently on Java. You can find his post on the Clear Screen functionality here. I hope our visitors will continue chipping in with their valuable comments and making this place better and better for us.


Update from Java 6.0


Java 6.0 has gone C/C++ way to facilitate better interaction with character-based console devices by using standard library functions. It seems that it wasn't possible for them to have such features for traditional Java Streams and hence they created a new Java Class named java.io.Console. This Class is mainly for reading from and writing to the Console and not for clearing it (as of now).


The Sun Java Doc of this class clearly says that the class contains methods to access the character-based console device, if any, associated with the current JVM. We can obviously see a compromise with Platform Independence here as the code written using this Class can never be guaranteed to behave uniformly across all platforms and hence not portable. System.console() method simply returns 'null' on the platforms which doesn't have an associated Console with the underlying JVM.


On the platforms where it finds a Console, it probably uses JNI internally to get the task done. We always have such solutions provided by third party vendors and the only good thing about the addition of this new class is probably that it's now a part of the standard library and it might be having little more efficient code than the code supplied by the third party vendors.


BTW, the center of our discussion 'Clear Screen functionality' is still not there in this class. Let's see if at all they provide that also in the newer releases of Java.


The bottom line remains the same - we would probably never have any such functionality without compromising Platform Independence and in most of the cases the APIs would probably be using JNI only. A true Java solution to this problem still remains a topic of research.




Share/Save/Bookmark


1 comment:

Anonymous said...

finally I found a comprehensive answer to this famous que. wonderful post.