Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

Why does the following work? I would expect a NullPointerException to be thrown.

String s = null;

s = s + "hello";

System.out.println(s); // prints "nullhello"

1 Answer

0 votes
by (46k points)

The second line is transformed to the following code:

s = (new StringBuilder()).append((String)null).append("hello").toString();

The append methods can handle null arguments.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 28, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...