0

In Java programming, despite String being a reference variable and not a primitive, a constructor is not required, e.g

String s = "stringValue";

Is the same as

String s = new String("stringValue");

I've seen it mentioned before, but never explained. How is String treated as a primitive, and is this functionality able to built into other classes?

Edit: Looking for the actual code that makes String different, and how it can be used - not like in the specific example supplied.

nuggetbram
  • 345
  • 1
  • 12
  • ```String str = "abc";``` is equivalent to: ```char data[] = {'a', 'b', 'c'}; String str = new String(data);``` – Alexandr Jun 15 '15 at 11:15
  • "Is the same as `String s = new String("stringValue");`" nope, it is similar to`String s = new String(new char[]{'s','t','r','i','n','g','V','a','l','u','e'});`. If what you said was true then we would still need to replace inner `"stringValue"` with `new String("stringValue")` which doesn't actually solve anything. Anyway crating `"stringValue"` literal is not that simple, since it also involves string pool interning/reusing. – Pshemo Jun 15 '15 at 11:15

0 Answers0