將雙精度值新增到 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

更新時間:2019-07-30

2 千次以上檢視

開啟您的 職業

完成課程以獲得認證

開始學習
廣告