What happens when you add a double value to a String in java?


The “+” operator with a String acts as a concatenation operator.

Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object.

In-fact adding a double value to String is the easiest way to convert a double value to Strings.

Example

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);
      }
   }
}

Output

Enter a double value:
245.5474
Enter a String value:
test
Result of the addition test245.5474

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements