0
public class Apples {
    public static void main(String args[]){
        String base;
        int no1 = 72;
        int no2 = 75;

        base = (String)(no1 + no2);
        System.out.println(base);
    }
}

The Above code generates error and says that it cannot convert int to String. Why is this so ?

I am a beginner and from my knowledge I guess that type-casting should work here.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • You cannot do a _cast_ here simply because an `int` is not a `String`. They are different types and do not have a type relationship. So you have to _convert_: `String.valueOf(no1) + String.valueOf(no2)`. – Seelenvirtuose Feb 21 '16 at 08:22

1 Answers1

1

You can use valueOf() like this:

String base = String.valueOf(no1 + no2);

Alternatively, If you just want to print the number then you can directly do:

System.out.prinltn("Number: " + (no1 + no2));
user2004685
  • 9,548
  • 5
  • 37
  • 54