JspWriter write versus print - java

I'm developing some custom JSP tags. In my SimpleTag.doTag() I grab the JspContext and call getOut() to get the JspWriter. When writing to JspWriter, what's the different between write(String) and print(String)? Should I be calling one instead of the other?

The print() method can buffer, the write() method is inherited from the Writer class and cannot - so you may get better performance from the JspWriter's print() method.
In addition, the print() method is overloaded to take many different types of objects as an argument, whereas the write method deals in Strings and chars only.
See the JspWriter javadocs for more details.

from the javadoc:
The 'write' function was inherited from java.io.writer .
The 'print' function: prints "null" if the argument was null. Otherwise, the string's characters are written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Related

why need of "println(char[] x)" when there is already "println(Object x)" - java

I was reading about println function and I came across that there is println(char[ ] x) as well as println(Object x)
https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char[])
My question is that: As arrays in java are object so what is the need to specifically overload println() with char[] whereas rest arrays like int[] etc. uses the println(Object x) overloaded function.
println(Object x)
if you use it to print a char array (the char array is an object), it won't print the content but the objectClass#hashcode style. You can test it yourself to see the exact output.
Because they are implemented differently.
println(Object)
will (after checking for null, etc), call the parameter's toString() method and display the result of it.
The toString() method of an array is not useful: it will give you the array type and the hashcode of the array. So the overloaded form gives a more useful implementation in the case of a char[] parameter.
Note that, with most object types, the toString() method can be overridden (so overloading the println(...) method for every possible type is not necessary (or possible...). However, the toString() method cannot be overridden for arrays, so there is benefit to overloading println in this case.
Because it prints the char array as a string and otherwise prints in object form, and seeing the contents may be more convinient. You can try casting it to object first and see the difference.
Because print/ln(char[]) handles the actual printing of characters, toString() of the array object itself still provides the usual type+hash output, regardless of being an array of characters
char c[]={'a','b','c'};
Object o=c;
System.out.println(c);
System.out.println(c.toString());
System.out.println(o); // *
System.out.println(o.toString());
The * thing is interesting (and this is why I post at all, since the rest is already there in other answers) because it demonstrates that Java has single dispatch: the actual method to be invoked is decided in compilation time, based on the declared type of the argument(s). So it does not matter that o is a character array in runtime, in compilation time it seems to be an Object, and thus print/ln(Object) is going to be invoked for it.

Long commands in Java

I am learning to code in Java
I know what namespaces, classes and methods are
with that knowledge I understand code such as the following
CharSequence v = new BackwardString("whale");
v.toString();
However sometimes you see examples of code which are longer than this
an example being
dictionary.subSet("a","ab").size();
In the ubove example dictionary is a class and subSet() is a method.
However size() is also a method but methods cannot contain other methods, so where does size() come from and why does it work?
Another common example which i have used without giving any thought to until now is
System.out.printLn();
in this case would System be a namespace, out be a class and printLn() be a method?
dictionary.subSet("a","ab").size();
It's a chaining of method calls. dictionary.subSet("a","ab") returns a String object, on which you call the size method.
System.out.println()
System is a class (java.lang.System), out is a static variable of that class whose type is PrintStream, and println is a method of PrintStream.
dictionary.subSet("a","ab").size();
The subSet method returns a String object, you are then calling .size() on this String. It is shorthand for doing the following
String a = dictionary.subSet("a","ab")
int size = a.size();
System.out.println()
System.out returns a PrintStream method, and you are invoking the println() method of that object.
This is called Method Chanining
System.out.printLn();=
System is a class
PrintStream is a class again // ref is out
println() is a function
In this example:
dictionary.subSet("a","ab").size();
method subSet returns an object with method size which gets invoked after subSet returns.
Similar thing happens with another snippet:
System.out.printLn();
Class System contains a static field out which has a method println
This is a common practice in Java programming to pipeline method calls. Sometimes an object can return itself allowing you can call multiple methods in one line.
The . selection is done as follows:
(dictionary.subSet("a","ab")).size();
Set s = dictionary.subSet("a","ab");
s.size();
The method subSet delivers (likely) a Set,
and Set has a method size.
This is called "chaining".
To get a feeling of it:
BigDecimal n = BigDecimal.valueOf("123456780.12");
n = n.multiply(n).add(n).divide(BigDecimal.TWO).subtract(n);
BigDecimal does large numbers with precise fixed point arithmetic.
It cannot use operators, and the above is a normal style.
Selectors (that might be chained:
. member
[ index ]
( function arguments )

In Java is there a name for the object the method is called on?

When I explain code, I often have to write "the object that the method is called on". E.g.
The contains() method simply checks if the input substring is present in the string you call the contains() on.
In Objective-c I'd write
The contains() method simply checks if the input substring is present in receiver (string).
Which makes sense as Objective-c uses message passing and a message has a sender and a receiver.
The lack of this expression for Java terminology makes descriptions very complicated when the object in question cannot be named explicitly. Does there exist a standard name for the object that the method is called on?
VERDICT: receiver seems to express the relationship the best.
That's called the "receiver". See Terminology.
In java it is often referred to as "this" object.
For instance:
The contains() method simply checks if the input substring is present in this string.
It is consistent with the this keyword meaning in Java.
In the Java SE javadocs, you'll see "this", "this instance", "this object", or "this <class>" used quite a bit.
Object.equals(Object)
Indicates whether some other object is "equal to" this one.
Collection.iterator()
Returns an iterator over the elements in this collection.
Another decent example maybe Observer.update(Observable, Object)
This method is called whenever the observed object is changed. An
application calls an Observable object's notifyObservers method to
have all the object's observers notified of the change.
Note that the receiver is simply referred to by its classname.

Java - Printing a class

If I have an array of objects which have a toString method and I print the array using a for loop (e.g.: simply array[i] to reach the objects and carry out System.out.println(array[i])) will the toString method be invoked automatically? It seems to be but I just want to check this is what is going on.
Yes, it will.
The advantage, in fact, of doing this over implicitly calling .toString() is that nulls are handled without throwing an exception. If array[i] is null, then System.out.println(array[i]) will print null where System.out.println(array[i].toString()) will throw a NullPointerException.
This is because the System.out.println(object) method calls System.out.print(object) which calls String.valueOf(object) which in turn calls object.toString().
Yes, it certainly will.
Here are some API descriptions of how println(Object) and print(Object) methods work.
println(Object)
print(Object)

Methods:println() and write() in java

Hello,
I want to know the difference between println() and write() methods used in java servlet.
out.println("Hello");
out.write("Hello");
How the above code will be stored??
Why can we use both the methods for the writing the same text as above..
The out variable in your case is most likely refers to a PrintWriter, so the answer to your question lies in the API documentation of that class.
Just compare the description of write...
public void write(String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
... with the description of println ...
public void println(String x)
Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().
... and print ...
public void print(String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
All in all I'd say that the print methods work on a higher level of abstraction and is the one I prefer to work with when writing servlets.
The only plausible difference with PrintWriter.println has to with flushing which is invoked in case of using println but not print("\n") in all other respects it would behave same as print()
Plus the PrintWriter class also doent throw any Exception.
1.out.write("") method is for java.io.Writer it is used to write the any file like text,csv.
2.out.println("Hello") this one is servlet method and it is use for write data on browser.

Categories

Resources