Java – (floating point value, integer value, long value) how to give unexpected results?

import java.util.*;
import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Float f=new Float(3.1);
                Integer i=new Integer(1);
                long l=2;
                System.out.println("Result is "+l+f+i);
    }
}

Output: the result is 23.11

I saw the above code somewhere I tried to find the reason behind this unexpected output, but failed Please provide some links or references or explanations

Additional information: system out. Println (L f I "is the result"); 6.1 is the result This means that the order of strings and numbers affects the output

Solution

Add a focus here for other answers:

Whenever a string connection is made, the toString () method is called for each element in the connection So, the element to connect is,

"Result is ",l,f,and i

For primitives, Auto@R_288_2419 @Ing first converts them into wrapper classes, and each calls the toString () method, which is what happens

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>