java中两个String类型求和
在Java中,可以使用 + 运算符将两个字符串连接起来,例如:
String str1 = "Hello";
String str2 = "World";
String result = str1 + str2;
System.out.println(result); // 输出:HelloWorld
需要注意的是,当使用 + 运算符将字符串与其他类型的数据进行连接时,会将其他类型的数据自动转换为字符串类型。例如:
int num1 = 10;
double num2 = 3.14;
String result = "The value of num1 is " + num1 + ", and the value of num2 is " + num2;
System.out.println(result); // 输出:The value of num1 is 10, and the value of num2 is 3.14
在上面的例子中,num1 和 num2 都是数字类型,但是在使用 + 运算符连接字符串时,它们会自动转换为字符串类型
原文地址: https://www.cveoy.top/t/topic/fzgo 著作权归作者所有。请勿转载和采集!