Ramesh C
Ramesh C

Reputation: 83

How to add the two or more numbers in to string in java?

  1. We can't add the numbers in to string data type with out the string value,it will not compile
String val = 5+5;
  1. We can add two or more numbers before appending the string.
String val = 10 + 10 + " Hello "
System.out.println(val);

Output

20 Hello
  1. Adding two numbers,append the value to the number but After appending the string value we can't add two or more numbers
public class Display {    
    public static void main(String[] args) {
        String val = 10 + 10 + " Hello " + 20 + 10;
        System.out.println(val);
    }    
}

Output :

20 Hello 2010

can anyone explain this?

Upvotes: 1

Views: 12948

Answers (9)

A Person
A Person

Reputation: 451

The problem is that the value you are passing to the string in concatenating. You need to specify where to look specifically.

The best approach will be to leave the addition values in brackets.

String val = (10 + 10) + " Hello" + (20 + 10);

or

String val = 10 + 10 + " Hello" + (20 + 10);

I would suggest using brackets where possible to avoid getting these small errors when the compiler compiles the code.

Upvotes: 0

ThaDome23
ThaDome23

Reputation: 90

The problem is the associativity of operand in java

The "+" operand in java have an associativity from left to right , so this means that the expression 10+10 is the first to evalutate and because these are integers (10,10) the result is integer (20).
Then it evalutate 20 + " hello ", because they are an integer and a string the java compiler transform the 20 into a string. Now string + string expression produces a string "20 hello ".

Now is the problem

at this point,because "20 hello " is a string, the expression "20 hello" + 20 transform 20 into a string and the result is "20 hello 20".When is added the last term (10) it is converted into a string and it is concatenated to the string "20 hello 20" making "20 hello 2010".

to avoid this inconvenient you can place round brackets in the expression,
20 + " hello "+ ( 20 + 10 )

Upvotes: 0

Bheem  Singh
Bheem Singh

Reputation: 707

you can try like this

public class Display {    
    public static void main(String[] args) {
        String val = 10 + 10 + " Hello " + (20 + 10);
        System.out.println(val);
    }    
}

output

20 Hello 30

because after string all next element will be concatenation

Upvotes: 0

Denis Rozhko
Denis Rozhko

Reputation: 70

These simple questions are confusing me. It's simple math with priority. when we try to concatenate number with number it will be number if one of provided is string then it will be transformed to string.

String val = 10 + 10 + " Hello " + 20 + 10;

   number  =>   string  => string => string   -> result String

((((number + number) + string) + number) + number)

Upvotes: 0

Bramanta
Bramanta

Reputation: 461

  1. You can't do it like that because 5+5 will return an integer, therefore you can't assign it into string data type

  2. If you append 10+10 then append it with string, it'll return a string, so you can assign it to string

  3. The first 10+10 still count as integer then you append it with string => it'll return to string

then you add it with integer 20 => return as string that string will return string if you add it with more and more integer

Upvotes: 0

Stultuske
Stultuske

Reputation: 9437

It's String concatenation. As soon as there is a String added, all the next elements will be added to the String, not as an int.

String a = 10 + 5 + " result";

Does this:

10 + 5 + " result";
15 + " result";
"15 result";

String a = 10 + " result " + 5 + 2;

becomes:

"10 result " + 5 + 2;
"10 result 5" + 2;
"10 result 52"; 

Make sure the ints are added before concatenation:

String a = 10 + " result " + (5 + 2);

EDIT: As for the

String val = 5 + 5;

this will cause problems because both elements are ints. What you are doing here is not concatenating, but adding two ints, which results in another int.

Upvotes: 5

Tschallacka
Tschallacka

Reputation: 28742

Order of operations matters, also knowing how such a statement is evaluated behind the screen. Check out the below rough description of how your code is evaluated.

multiplying and division before addition and substraction.

in this code:

String val = 10 + 10 + " Hello " + 20 + 10;

it's evaluated like:

Check operations for multiplications and division
Nothing found
Check for additions.
Integer found.
Addition found
Integer found
Add integer to integer. 
Remember integer.
Addition found
String found.
Create string builder.
Add remembered integer to string builder
Add string to stringbuilder
Addition found
integer found
add integer to string builder
Addition found
integer found
add integer to string builder
return string from string builder.

If you'd have an addition, that would be executed first:

https://ideone.com/aPlaPv

String val = 10 + 10 + " Hello " + 20 * 10;

It's evaluated like:

Check operations for multiplication and division
Multiplication found
Integer found
Multiplication found
Integer found
Remember multiplication
no more multiplications found
Return to start
Check for additions.
Integer found.
Addition found
Integer found
Add integer to integer. 
Remember integer.
Addition found
String found.
Create string builder.
Add remembered integer to string builder
Add string to stringbuilder
add remembered multiplication result to string builder
return string from string builder

Upvotes: 0

Chetan pothkar
Chetan pothkar

Reputation: 48

If you put parenthesis before appending to string it will work.

 public static void main(String[] args) {
    String val = 10 + 10 + " Hello " +( 20 + 10);
    System.out.println(val);
}  

Upvotes: 0

Abrikot
Abrikot

Reputation: 1005

The explanation I see is the following: the "plus" operator is defined as follows:

  • int + int -> int
  • int + String -> String
  • String + int -> String

You then have the following line: ((((int + int) + String) + int) + int) Resolving the operation one after the other, you end with the following operations to resolve:

  • int(10) + int(10) -> int(20)
  • int(20) + String( Hello ) -> String (20 Hello)
  • String (20 Hello) + int(20) -> String (20 Hello 20)
  • String (20 Hello 20) + int(10) -> String (20 Hello 2010)

Upvotes: 0

Related Questions