How does java create objects implicitly? As with the String class

I cannot understand how an object is created implicitly.

Example:

String s = "implicit instantiation";

Is it possible to create your own class whose objects can be created implicitly?

+4
source share
3 answers

No, a String instance is processed implicitly by the compiler. Only String and Array classes have this property.

String greeting = "Hello world!";
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };

Autoboxing allows you to implicitly create objects of primitive wrapper types, but this is also a special case handled by the compiler. You cannot create your own classes with this ability.

Boolean b = false;
Integer i = 0;
Double pi = 3.1416;
+3
source

Unfortunately, you just can't do it!

C ++ - Java-, -

Foo myFoo = 1

string:

String s = "implicit instantiation"

sintax , "" - (, ):

String s = new String("implicit instantiation")

, ...

+1

- . :

String s;  // Is not initialized and it nos constructed.

, java? new operator !

s = new String("qwe"); // New object constructed

-, . :

String s= "asdfasd;" 

String - Java, new operator, , . :

Integer i = 3; 
Double d = 3.3d;

..

0

Source: https://habr.com/ru/post/1674671/


All Articles