java - How can a string accept an integer? -
can variable string accept integer value well. or can concat integer string ?
example:
public class teststring1 { public static void main(string[] args) { string str = "420"; str += 42; system.out.print(str); } }
i expecting compilation error on here because string getting concatenated integer.
jls documentation on string concatination operator(+
)-
15.18.1 string concatenation operator +
if 1 operand expression of type string, string conversion performed on other operand produce string @ run time. result reference string object (newly created, unless expression compile-time constant expression (§15.28))that concatenation of 2 operand strings. characters of left-hand operand precede characters of right-hand operand in newly created string. if operand of type string null, string "null" used instead of operand
that why string + int not produce error. , prints 42042
Comments
Post a Comment