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.
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