Is there an operator overload in Java?

I just want to know one simple thing in Java. Consider the following code segment in Java.

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

works fine in Java and prints the result, x = 10 as a string.


Although the variable x is of type int , it is automatically converted to a type of String (shell type). How does Java do this? Is the operator overloading somewhat or somewhere in Java, as above, as with C ++ and C # ?, although it has been removed from Java. What specific concept is used here to convert x to string. The only thing I want to know


and also one more question: in Java, a type of logical data (not a Boolean, a wrapper class) cannot be converted to any other type (as I know). Why this is so, in some very specific situations it may be useful to convert it to a string or some other types.

+4
source share
3 answers

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

To go beyond the practical “how Java does it,” I will take Stephen’s advice and give a theoretical assessment. It is clear that each value in concatenation is first converted to String and then concatenated. Zeros are combined as the word "null".

From the Java language specification :

15.18.1.1 String Conversion

Any type can be converted to a String type by converting a string. The value x of the primitive type T is first converted to a control value, as if as an argument to create the corresponding instance of the class expression:

If T is Boolean, then use the new Boolean (x). If T char, then use the new Character (x). If T bytes, short or int, then use the new Integer (x). If T is long, then use the new Long (x). If T is a float, use the new Float (x). If T is double, use the new Double (x). This control value is then converted to a String type by converting the string. Now only reference values ​​should 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 calling the toString of the reference object with no arguments; but if the result of calling the toString method is null, then the string "null" is used instead.

The toString method is defined by the primitive class Object; many classes override it, in particular boolean, character, integer, long, floating, Double and String.

15.18.1.2 Optimizing String Concatenation

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

The optimized version does not actually make a complete complete String conversion.

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

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

This java code:

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

Generates this - let's see how two styles of concatenation lead to the same bytecode:

  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 

The compiler turned "cip + ciop" into "the new StringBuilder (cip) .append (ciop) .toString ()". In other words, “+” is actually a shorthand for the more detailed StringBuilder idiom.

+9
source

Java supports limited operator overloading ... for built-in operators with certain combinations of (primitive) operand types. The main cases:

  • Arithmetic operators have overloads for different numeric primitive types. The + operator is additionally overloaded to concatenate strings.

  • "&", "|", "^" and "!" operators are overloaded for (not short-circuited) logical and bitwise integral operations.

However, Java does not support any form of programmable statements or operator overloading.


The answers that say overloading is “compiler magic” or that “compiler does this” are missing a point. The Java language specification says that it is part of the language, what it means and (to a large extent) determines how it should be implemented.


As another answer suggests, automatic boxing / unpacking and other things (strictly speaking) are conversions, not overloads. For example:

 int j = ... Integer i = ... j = j + i; 

This uses the overload (int, int) the '+' operator and uses automatic unpacking to convert the second operand to int.

 byte b = ... j = j + b; 

This uses the overload (int, int) the '+' operator and uses a cast to convert the second operand to int.

Note that JLS defines rules that determine which operator overload is used based on (initial) operand types. Thus, in the second example, JLS indicates that overload (int, int) "+", and not overload (byte, byte) .

+1
source

All this is the magic of the compiler. You cannot perform operator overloading in Java.

crosses his fingers and hopes it is not terribly wrong and gets me 20 downvotes

0
source

Source: https://habr.com/ru/post/1379568/


All Articles