Java字符串文字会自动解释为String对象,但是如何? [英] Java string literal automatically interpreted as String object, but how?

查看:86
本文介绍了Java字符串文字会自动解释为String对象,但是如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于没有重载运算符,并且不默认支持低级字符串缓冲区(不包括高级StringBuffer),因此Java自动将字符串文字解释为String对象的机制是什么?这是语言还是虚拟机上下文?

What are the mechanics behind Java automatically interpreting string literals as String objects, since there are no overloaded operators and there is no default support for low-level string buffers (not including the high level StringBuffer)? Is this a language or virtual machine context?

推荐答案

两者都有.

JVM具有用于将字符串文字加载到运行时堆栈上的字节码,并且在JVM规范中指定了这些操作码,以通过将适当构造的String对象推入堆栈中来工作,该对象的内容等于在其中指定的某些字符串文字.类文件.除了必须插入String对象外,没有其他说明,这意味着转换是特定于实现的.

The JVM has byte codes for loading string literals onto the runtime stack, and these opcodes are specified in the JVM spec to work by pushing an appropriately-constructed String object onto the stack whose contents are equal to some string literal specified in the class file. There is no specification for how this is done other than that the String object must be interned, meaning that the transformation is implementation-specific.

在语言级别,出于类型检查的目的,编译器可以将它看到的任何String文字视为完整的String对象,然后可以将文字和适当的字节码写到类文件中.

At the language level, the compiler can treat any String literal it sees as a complete String object for the purposes of type checking, and can then just write out the literal and the appropriate bytecodes into the class file.

这篇关于Java字符串文字会自动解释为String对象,但是如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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