Because StringBuilder
is an Object and it needs to be constructed. You're getting the error because String is not a StringBuilder
.
String is a special, it's designed to be between primitive and class1. You can assign a string literal directly into a String variable, instead of calling the constructor to create a String instance.
The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language. Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces.
For performance reason, Java's String is designed to be in between a primitive and a class.
More reading: