Java中是否存在运算符重载? [英] Does operator overloading exist in Java?

查看:83
本文介绍了Java中是否存在运算符重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想了解Java中的一件事.让我们考虑以下Java代码段.

I just want to know one simple thing in Java. Let's consider the following segment of code In Java.

int x=10;
String temp="x = "+x;
System.out.println(temp);

在Java中完全有效,并产生输出x = 10作为字符串.

is perfectly valid in Java and produces the output, x = 10 as a string.

尽管变量x的类型为 int ,但它会自动转换为String类型(包装器类型). Java如何做到这一点?尽管运算符重载已从Java中删除,但它在Java中是否像上面的C ++和C#中那样存在某种程度的重载? 此处使用特定概念将x转换为字符串.我想知道的唯一一件事

Although, the variable x is of type int, it is automatically converted to a String type (a wrapper type). How does Java do this? Does operator overloading somewhat or somewhere exist in Java like the one above as with C++ and C#?, though it was removed from Java. Which specific concept is used here to convert x into a string. The only thing I want to know

还有一个问题,在Java中,布尔数据类型(不是布尔值,包装类)不能转换为任何其他类型(据我所知).为什么会这样,在某些非常特殊的情况下,将其转换为字符串或其他一些类型可能会有用.

and also, one more question, in Java a boolean data type (not Boolean, a wrapper class), can not be converted to any other type (as per I'm knowing). Why is it so, in some very specific situations , it may useful to convert it to a string or some other types.

推荐答案

编译器在内部将该词组("x =" + x)转换为StringBuilder并使用.append(int)将整数添加"到字符串.

The compiler converts that phrase ("x = "+x) into a StringBuilder internally and uses .append(int) to "add" the integer to the string.

要超越实用的"Java如何做到这一点",我将听从Stephen的建议,并提供理论知识.从概念上讲,串联中的每个值都首先转换为String,然后进行串联.空值被串联为单词"null".

To go beyond the practical "how does Java do this", I'll take the advice of Stephen and give the theoretical as well. Conceptually, each value in the concatenation is first converted to a String and then concatenated. Nulls are concatenated as the word "null".

来自 Java语言规范:

15.18.1.1字符串转换

任何类型都可以通过字符串转换转换为String类型.一个值 首先将原始类型T的x转换为参考值,就像 将其作为适当的类实例创建的参数 表达式:

Any type may be converted to type String by string conversion. A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression:

如果T为布尔值,则使用新的布尔值(x).如果T为char,则使用new 字符(x).如果T为byte,short或int,则使用新的Integer(x).如果 T长,然后使用新的Long(x).如果T为float,则使用新的Float(x). 如果T为double,则使用新的Double(x).然后,该参考值是 通过字符串转换转换为String类型.现在仅供参考 值需要考虑.如果引用为空,则为 转换为字符串"null"(四个ASCII字符n,u,l,l). 否则,转换的执行就好像是通过调用 没有参数的引用对象的toString方法;但是如果 调用toString方法的结果为null,然后字符串"null" 代替.

If T is boolean, then use new Boolean(x). If T is char, then use new Character(x). If T is byte, short, or int, then use new Integer(x). If T is long, then use new Long(x). If T is float, then use new Float(x). If T is double, then use new Double(x). This reference value is then converted to type String by string conversion. Now only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

toString方法由原始类Object定义;许多 类会覆盖它,特别是布尔值,字符,整数,长整型,浮点型, Double和String.

The toString method is defined by the primordial class Object; many classes override it, notably Boolean, Character, Integer, Long, Float, Double, and String.

15.18.1.2字符串连接的优化

实现可以选择执行转换和串联 一步避免创建然后丢弃中间体 字符串对象.增加重复字符串的性能 串联后,Java编译器可以使用StringBuffer类或 减少中间String对象数量的类似技术 通过对表达式求值而创建的.对于原始类型, 一个实现也可以优化包装器的创建 通过直接从原始类型转换为字符串来实现对象.

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression. For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

优化版本实际上不会首先进行完全包装的String转换.

The optimized version will not actually do a full wrapped String conversion first.

这很好地说明了编译器使用的优化版本,尽管没有转换原语,但您可以在其中看到编译器在后台将事物更改为StringBuilder:

This is a good illustration of an optimized version used by the compiler, albeit without the conversion of a primitive, where you can see the compiler changing things into a StringBuilder in the background:

http://caprazzi.net/posts/java-bytecode -string-concatenation-and-stringbuilder/

此Java代码:

public static void main(String[] args) {
    String cip = "cip";
    String ciop = "ciop";
    String plus = cip + ciop;
    String build = new StringBuilder(cip).append(ciop).toString();
}

生成此代码-查看两种串联样式如何导致相同的字节码:

 L0
    LINENUMBER 23 L0
    LDC "cip"
    ASTORE 1
   L1
    LINENUMBER 24 L1
    LDC "ciop"
    ASTORE 2
// cip + ciop
   L2
    LINENUMBER 25 L2

    NEW java/lang/StringBuilder
    DUP
    ALOAD 1
    INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
    INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
    ALOAD 2
    INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;

    ASTORE 3
    // new StringBuilder(cip).append(ciop).toString()
   L3
    LINENUMBER 26 L3

    NEW java/lang/StringBuilder
    DUP
    ALOAD 1
    INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
    ALOAD 2
    INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;

    ASTORE 4
   L4
    LINENUMBER 27 L4
    RETURN

编译器已将"cip + ciop"转换为"new StringBuilder(cip).append(ciop).toString()".换句话说,"+"实际上是更冗长的StringBuilder习惯用法的简写.

这篇关于Java中是否存在运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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