为什么Java的System.out.print方法有多种形式?

huangapple go评论73阅读模式
英文:

Why java System.out.print method have various forms?

问题

public void print(long l) {
    write(String.valueOf(l));
}

/**
 * 打印浮点数。由<code>{@link
 * java.lang.String#valueOf(float)}</code>生成的字符串根据平台的默认字符编码转换为字节,
 * 并以与<code>{@link #write(int)}</code>方法完全相同的方式写入这些字节。
 *
 * @param f 要打印的<code>float</code>
 * @see java.lang.Float#toString(float)
 */
public void print(float f) {
    write(String.valueOf(f));
}

/**
 * 打印双精度浮点数。由<code>{@link java.lang.String#valueOf(double)}</code>生成的字符串根据
 * 平台的默认字符编码转换为字节,然后以与<code>{@link #write(int)}</code>方法完全相同的方式写入这些字节。
 *
 * @param d 要打印的<code>double</code>
 * @see java.lang.Double#toString(double)
 */
public void print(double d) {
    write(String.valueOf(d));
}

/**
 * 打印字符数组。将字符转换为字节,根据平台的默认字符编码,以与<code>{@link #write(int)}</code>方法
 * 完全相同的方式写入这些字节。
 *
 * @param s 要打印的字符数组
 *
 * @throws NullPointerException 如果<code>s</code>为<code>null</code>
 */
public void print(char s[]) {
    write(s);
}

请注意,这只是代码的翻译部分,不包括您提供的问题或其他内容。

英文:
public void print(long l) {
    write(String.valueOf(l));
}

/**
 * Prints a floating-point number.  The string produced by <code>{@link
 * java.lang.String#valueOf(float)}</code> is translated into bytes
 * according to the platform's default character encoding, and these bytes
 * are written in exactly the manner of the
 * <code>{@link #write(int)}</code> method.
 *
 * @param      f   The <code>float</code> to be printed
 * @see        java.lang.Float#toString(float)
 */
public void print(float f) {
    write(String.valueOf(f));
}

/**
 * Prints a double-precision floating-point number.  The string produced by
 * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
 * bytes according to the platform's default character encoding, and these
 * bytes are written in exactly the manner of the <code>{@link
 * #write(int)}</code> method.
 *
 * @param      d   The <code>double</code> to be printed
 * @see        java.lang.Double#toString(double)
 */
public void print(double d) {
    write(String.valueOf(d));
}

/**
 * Prints an array of characters.  The characters are converted into bytes
 * according to the platform's default character encoding, and these bytes
 * are written in exactly the manner of the
 * <code>{@link #write(int)}</code> method.
 *
 * @param      s   The array of chars to be printed
 *
 * @throws  NullPointerException  If <code>s</code> is <code>null</code>
 */
public void print(char s[]) {
    write(s);
}

Source code image

I opened java.io.PrintStream class file. And i found there are various print methods. Each method 99% same without only parameter type. Many programming guide books suggest that you can use generic(or template) in this situation. So i'm wondering why java.io.Printstream do not use generic.

答案1

得分: 1

如果您要使用以下方法:

public <T> void print(T value);

并且您想要打印一个 int 值,那么它将被用作:

public void print(Integer value);

因此,int 值将被装箱,这意味着性能下降和不必要的内存使用。

这就是为什么通常会为所有原始数据类型提供方法重载的原因。


另外,正如Taschi指出的,这个类只是比Java中的泛型更旧。

英文:

If you would use a method with

public &lt;T&gt; void print(T value);

and you would want to print an int value then if would be used as

public void print(Integer value);

so the int value would be boxed what means lost in performance and unnecessary memory usage.

That is the reason that you will often find method overloads for all primitiv data-types


Also as Taschi pointed out, the class is simply older than generics in Java.

huangapple
  • 本文由 发表于 2020年8月3日 15:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63225529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定