InterviewSolution
| 1. |
In How Many Ways String Object Can Be Created? |
|
Answer» Since strings are objects so strings can of course be CREATED using new operator. String class has more than 10 constructors to create Strings which ranges from taking NOTHING as parameter to taking char array, StringBuffer, StringBuilder, another String as ARGUMENT. Another and more preferred way to create Strings is to ASSIGN String literal directly to a String reference as you will do for any primitive type. For every string literal Java will automatically CONSTRUCTS a String object. As example - String str = “abc”; Since strings are objects so strings can of course be created using new operator. String class has more than 10 constructors to create Strings which ranges from taking nothing as parameter to taking char array, StringBuffer, StringBuilder, another String as argument. Another and more preferred way to create Strings is to assign String literal directly to a String reference as you will do for any primitive type. For every string literal Java will automatically constructs a String object. As example - String str = “abc”; |
|