Reputation: 5306
Here you are: http://developer.android.com/reference/java/io/PrintStream.html#print%28float%29
Just one function could serve all the purposes:
public void print (Object o) {
if (o == null) {
// print "null"
} else {
// print o.toString();
}
}
More elaborations. For example, internal_print(String str)
is a function that write to the print stream. Then the only one function needed would be:
public void print (Object o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
For other float
, int
, char
, long
, etc. overloadings, i can imagine they are just like:
public void print (float o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
public void print (int o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
public void print (char o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
public void print (long o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
...
Or even just calling the killer function print (Object o)
.
Could you please explain. Many thanks!!
Upvotes: 0
Views: 186
Reputation: 111389
Historical reasons: the PrintStream
class exists since Java 1.0, long before autoboxing was added (in 1.5).
Autoboxing is what would enable you to pass a primite type to print(Object o)
. Without it PrintStream
had to implement an overload for each primitive type separately.
By the way, the implementations of print(..)
for primitive types is more like:
public void print (int o) {
print(String.valueOf(o));
}
... and the implementation of String.valueOf(..)
basically delegates to methods in wrapper classes, such as Integer.toString(int)
and Long.toString(long)
. You can read the source code at docjar.
Upvotes: 5