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

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

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

问题

  1. public void print(long l) {
  2. write(String.valueOf(l));
  3. }
  4. /**
  5. * 打印浮点数。由<code>{@link
  6. * java.lang.String#valueOf(float)}</code>生成的字符串根据平台的默认字符编码转换为字节,
  7. * 并以与<code>{@link #write(int)}</code>方法完全相同的方式写入这些字节。
  8. *
  9. * @param f 要打印的<code>float</code>
  10. * @see java.lang.Float#toString(float)
  11. */
  12. public void print(float f) {
  13. write(String.valueOf(f));
  14. }
  15. /**
  16. * 打印双精度浮点数。由<code>{@link java.lang.String#valueOf(double)}</code>生成的字符串根据
  17. * 平台的默认字符编码转换为字节,然后以与<code>{@link #write(int)}</code>方法完全相同的方式写入这些字节。
  18. *
  19. * @param d 要打印的<code>double</code>
  20. * @see java.lang.Double#toString(double)
  21. */
  22. public void print(double d) {
  23. write(String.valueOf(d));
  24. }
  25. /**
  26. * 打印字符数组。将字符转换为字节,根据平台的默认字符编码,以与<code>{@link #write(int)}</code>方法
  27. * 完全相同的方式写入这些字节。
  28. *
  29. * @param s 要打印的字符数组
  30. *
  31. * @throws NullPointerException 如果<code>s</code>为<code>null</code>
  32. */
  33. public void print(char s[]) {
  34. write(s);
  35. }

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

英文:
  1. public void print(long l) {
  2. write(String.valueOf(l));
  3. }
  4. /**
  5. * Prints a floating-point number. The string produced by <code>{@link
  6. * java.lang.String#valueOf(float)}</code> is translated into bytes
  7. * according to the platform's default character encoding, and these bytes
  8. * are written in exactly the manner of the
  9. * <code>{@link #write(int)}</code> method.
  10. *
  11. * @param f The <code>float</code> to be printed
  12. * @see java.lang.Float#toString(float)
  13. */
  14. public void print(float f) {
  15. write(String.valueOf(f));
  16. }
  17. /**
  18. * Prints a double-precision floating-point number. The string produced by
  19. * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  20. * bytes according to the platform's default character encoding, and these
  21. * bytes are written in exactly the manner of the <code>{@link
  22. * #write(int)}</code> method.
  23. *
  24. * @param d The <code>double</code> to be printed
  25. * @see java.lang.Double#toString(double)
  26. */
  27. public void print(double d) {
  28. write(String.valueOf(d));
  29. }
  30. /**
  31. * Prints an array of characters. The characters are converted into bytes
  32. * according to the platform's default character encoding, and these bytes
  33. * are written in exactly the manner of the
  34. * <code>{@link #write(int)}</code> method.
  35. *
  36. * @param s The array of chars to be printed
  37. *
  38. * @throws NullPointerException If <code>s</code> is <code>null</code>
  39. */
  40. public void print(char s[]) {
  41. write(s);
  42. }

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

如果您要使用以下方法:

  1. public <T> void print(T value);

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

  1. public void print(Integer value);

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

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


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

英文:

If you would use a method with

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

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

  1. 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:

确定