System.out.println("M"+number+1);
String concatenation in java works as follows:
if the first operand is of type String and you use the + operator, it will concatenate the next operand, and the result will be a string.
try
System.out.println("M"+(number+1));
In this case, when parathesis () has the highest priority, things inside the brackets will be evaluated first. then the resulting int value will be combined with the string literal obtained in the string "M2"
source share