-1

How do I add Ints or Floats together in Java(or BlueJ)? I mean not that:

x=2;
y=3;
System.out.println(y+x);

and then comes 5 out, I mean rather like this:

x=2;
y=3;
c=y+x;
System.out.println(c);

And here should come out 32. Not adding like in Math, rather like combining these things in one new variable. Thanks a lot in advance!

Jesus42
  • 1
  • 1
  • use this - c=""+y+x – Rishikesh Dhokare Jun 12 '18 at 14:15
  • 2
    There are at least two and probably more ways to interpret this question. What, *specifically*, do you expect in `c`? A string? An `int` with the value thirty-two? An `int` in a superposition of the values 3 and 2? (Not going to happen on consumer hardware and an even vaguely standard JVM.) An `int` array with 3 and 2 in it? – T.J. Crowder Jun 12 '18 at 14:17
  • What you want is not addition of mathematical `int` values but rather interpreting the values as `String`s and then using *string concatenation*. So the steps are: transform your values to `String`s and then use *string concatenation*. After that transform back to an `int` value. – Zabuzard Jun 12 '18 at 14:21

1 Answers1

1

Concat your number with a String:

String c = "" + x + y;
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51