Which of the following statements is preferred to create a string "Welcome to Java"?
时间: 2024-03-24 16:40:30 浏览: 154
The most preferred way to create a string "Welcome to Java" is:
```
String str = "Welcome to Java";
```
This creates a string literal and assigns it to the variable `str`. String literals are preferred over creating strings using the `new` operator because they are more efficient and use less memory. When a string literal is encountered, the Java compiler creates a single instance of the string in the string pool and all references to the same string literal point to this instance.
Therefore, using a string literal is the most preferred way to create a string in Java.
阅读全文