2

So String isn't a primitive type but rather a class and objects of classes have to be created in the form Obj x = new Obj (); usually. However, strings can be created by saying String x = "..."; and this is the preferred way rather than String x = new String ("...");.

So I was wondering why this is allowed for strings and would it be possible to create objects of other classes in a similar fashion if the constructor only required a single parameter.

For example, if a class had a constructor that only called one integer, would saying Obj x = 2; be syntactically correct because it has the object name and parameter still included in the same way that Strings are written.

Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36
faris
  • 692
  • 4
  • 18
  • 1
    No, it's only possible with strings and boxed types (`Integer`, `Long` etc.). – shmosel Dec 06 '17 at 06:28
  • This is Java not C++. – smac89 Dec 06 '17 at 06:28
  • why strings though specifically if they're not a boxed type – faris Dec 06 '17 at 06:30
  • Possible duplicate of [What is the difference between "text" and new String("text")?](https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext) – vinS Dec 06 '17 at 06:30
  • A `".."` (string literal) evaluates to a String object instance representing the text *by definition of the language* (there is *no* requirement that it is a new instance of a String). Why require passing a String (created/existing by definition) to a constructor that creates a (new) String? The reason string literal exists is because *strings are so common* - it would be *painful* to use something like `new String(new char[] { 'h', 'e', 'l', 'l', 'o' })` everywhere. – user2864740 Dec 06 '17 at 06:32
  • See also https://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them – Mark Rotteveel Dec 06 '17 at 11:53

3 Answers3

3

String is a special final class that can not be extended. Creating a String with new keyword and literals are different;

There is a special memory area in the heap just for strings called String Pool. When you create a String literal, jvm searches the string pool and if it exists in the pool that string is addressed, if there is no match for the string you created than it is added to the pool and other strings with the same value address that object too.

If you create a String with the new keyword, it is ensured that string is a new object in the memory no matter what.

So String is a special kind of object that can not be categorized with the other objects.

Here is an article

What is String Pool

Dogukan Zengin
  • 545
  • 3
  • 11
2

So String isn't a primitive type but rather a class and objects of classes have to be created in the form Obj x = new Obj (); usually. However, strings can be created by saying String x = "..."; and this is the preferred way rather than String x = new String ("...");

That's correct, it often a mistake to use new String(String). Since Strings are such a common type of value to create that Java, like most languages, has a string literal grammar construct. When evaluated the string literal expression returns a string instance representing the text.

Calling String y = new String("...") is roughly equivalent to String x = "..."; String y = new String(x);: the string literal evaluated to a string which was then supplied to a String constructor that returned a new string instance (albeit using the same backing character array to avoid copying data) - such a waste! As such the new String(String) constructor should probably "never" be called, with the exception of wishing to do something special wrt. string interning.

So I was wondering why this is allowed for strings and would it be possible to create objects of other classes in a similar fashion if the constructor only required a single parameter.

It is a special aspect of Java grammar, and most other languages, to create string objects with minimal fuss. It would be very annoying to have to create strings out of other values: new String(new char[] {'n','o',' ','t','h','a','n','k','s'}).

Java does not have support for custom parsing and thus it is not possible to extend this behavior to custom types in Java itself. The grammar of string literals (and behavior during execution) is covered in the Java Language Specification.

The very creation of string objects from string literals is largely "JVM magic / implementation details" (creation of strings from literals do not call the new String(String) constructor as then, how is the original string instance created?). Furthermore, the string object instances returned from evaluating string literals are not guaranteed to be unique / "new" per string interning rules.

For example if a class had a constructor that only called one integer, would saying Obj x = 2; be syntactically correct because it has the object name and parameter still included in the same way that Strings are written.

This is not how Java works. A language that did work as hypothesized would not be Java 8.

In this example the integer literal is 2: when evaluated as an expression it yields the value .. (int)2. Unlike with the case of a string literal, Java then performs an Integer autoboxing operation: 2 is an int which, unlike String, is not an Object and cannot be directly assigned. This makes the original similar to Object x = new Integer(2); where the int-value has been implicitly wrapped as an Integer-value.

user2864740
  • 60,010
  • 15
  • 145
  • 220
1

It is not possible to do this in Java. It is only possible with primitive type variables and String. Since String is used so much, it is special and you do not have to write out the full new String("..."). The String x = "..." is an exception, not the usual for creating objects.

Caders117
  • 319
  • 3
  • 18
  • `new String("doesn't make sense because how would the string supplied as an argument be created without a string literal?")`. If that form was "required" to create string objects then the grammar rules would have to make `new String("..")` *not a constructor call* but a special parsing construct as the raw string literal that we expect presumably wouldn't exist.. – user2864740 Dec 06 '17 at 06:38