Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

August 2, 2010

PrintWriter : print() vs write()

Today while reading Servlets, I came across the class PrintWriter.


I found that there we have two methods (both with various overloaded versions) : write() and print()
and I saw that print() internally calls write() only. So I was wondering that why there are different methods although they are doing the same thing. So, this seemed to be kind of redundancy to me.


Then I realized that print() is actually used as a utility method there, to which u can pass any literal value say whether it is boolean/char/int/float/double (to its overloaded versoins), but in write u have a limitaion of using only int /char array or string.So print() is just encapsulating the implementation of write() method by providing number of overlaoded method by which u can call the write().



No comments: