System.out.print()如何工作? [英] How does System.out.print() work?

查看:85
本文介绍了System.out.print()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Java很长一段时间了,我想知道函数 System.out.print()是如何工作的。

I have worked with Java for a quite a long time, and I was wondering how the function System.out.print() works.

这是我的疑问:

作为一个功能,它在 io 包中的某处有声明。但是Java开发人员是如何做到这一点的,因为这个函数可以接受任意数量的参数和任何参数类型,无论它们如何排列?例如:

Being a function, it has a declaration somewhere in the io package. But how did Java developers do that, since this function can take in any number of arguments and any argument types no matter how they are arranged? e.g:

System.out.print("Hello World");
System.out.print("My name is" + foo);
System.out.print("Sum of " + a + "and " + b + "is " + c);
System.out.print("Total USD is " + usd);

无论变量的数据类型是什么 a,b,c,usd ,foo 或如何传递, System.out.print()从不抛出错误。

No matter what is the datatype of variables a, b, c, usd, foo or how they are passed, System.out.print() never throws an error.

对我来说,我从未参与任何需求如此的项目。但是,如果我得到这样的要求,我真的不知道如何解决它。

For me, I have never worked on any project where the requirement was like this. Provided, if I get a requirement like this, I really don't know how to solve it.

有人可以向我解释它是如何完成的吗?

Can anyone explain to me how it's done?

推荐答案

System.out 只是 PrintStream的一个实例。您可以查看其 JavaDoc 。它的可变性基于 方法重载 (具有相同名称的多种方法) ,但使用不同的参数)。

System.out is just an instance of PrintStream. You can check its JavaDoc. Its variability is based on method overloading (multiple methods with the same name, but with different parameters).

此打印流将其输出发送到所谓的 标准输出

This print stream is sending its output to so called standard output.

在您的问题中,您提到了一种名为 可变功能 的技术(或 varargs )。不幸的是, PrintStream #print 不支持,因此您必须将此错误与其他内容混淆。但是,在Java中实现它们非常容易。 请查看文档。

In your question you mention a technique called variadic functions (or varargs). Unfortunately that is not supported by PrintStream#print, so you must be mistaking this with something else. However it is very easy to implement these in Java. Just check the documentation.

如果你很好奇Java如何连接非字符串变量foo+ 1 + true + myObj ,它主要负责Java编译器。

And if you are curious how Java knows how to concatenate non-string variables "foo" + 1 + true + myObj, it is mainly responsibility of a Java compiler.

当串联中没有涉及变量时,编译器只是连接字符串。当涉及变量时,连接被转换为 StringBuilder#append chain。结果字节代码中没有连接指令;即, + 运算符(在谈论字符串连接时)在编译期间被解析。

When there is no variable involved in the concatenation, the compiler simply concatenates the string. When there is a variable involved, the concatenation is translated into StringBuilder#append chain. There is no concatenation instruction in the resulting byte code; i.e. the + operator (when talking about string concatenation) is resolved during the compilation.

Java中的所有类型都可以通过 Integer 类中的方法转换为字符串( int boolean 通过布尔类中的方法,对象通过自己的 #toString ,...)。如果你感兴趣,可以查看StringBuilder的源代码。

All types in Java can be converted to string (int via methods in Integer class, boolean via methods in Boolean class, objects via their own #toString, ...). You can check StringBuilder's source code if you are interested.

更新:我很好奇自己并检查(使用 javap )我的例子 System.out.println(foo+ 1 + true + myObj)编译成。结果:

UPDATE: I was curious myself and checked (using javap) what my example System.out.println("foo" + 1 + true + myObj) compiles into. The result:

System.out.println(new StringBuilder("foo1true").append(myObj).toString());

这篇关于System.out.print()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆