将双精度值添加到 Java 中的字符串会发生什么?
带有字符串的“+”运算符用作连接运算符。
无论您何时使用“+”运算符将字符串值添加到双精度值,都会连接这两个值,从而生成一个字符串对象。
事实上,将双精度值添加到字符串是将双精度值转换为字符串的最简单方法。
示例
import java.util.Scanner; public class StringsExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter a double value: "); double d = sc.nextDouble(); System.out.println("Enter a String value: "); String str = sc.next(); //Adding double and String String result = str+d; System.out.println("Result of the addition "+result); } } }
输出
Enter a double value: 245.5474 Enter a String value: test Result of the addition test245.5474
广告