faris
faris

Reputation: 702

Can new objects be instantiated without the = new Obj ()?

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.

Upvotes: 2

Views: 182

Answers (3)

user2864740
user2864740

Reputation: 61875

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.

Upvotes: 2

Dogukan Zengin
Dogukan Zengin

Reputation: 574

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

Upvotes: 3

Caders117
Caders117

Reputation: 317

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.

Upvotes: 1

Related Questions