Why in Java can I instantiate an object with new without assigning that reference to a variable whereas if I do that with a string I get an error, isn't string also supposed to be an implicitly instantiated object?
new Persona();
"Hola mundo";
new Persona().decirAlgo();
"Hola mundo".toUpperCase();
Ok, in the first part according to what I have studied it works because what I am doing is calling a constructor function and it is not necessary to store what is returned in a variable since it is an invocation instruction but my question is here, ¿ Isn't the same thing supposed to happen with the string since when using the string an object is implicitly instantiated and therefore it would be calling a constructor method?
And in the other two examples I don't understand something either, I know that the invocation to the saySomething method works because the Constructor function returns a reference to that point in the execution of the program and therefore I can use the value returned by that expression but why if now I use the string to then call the method of the String class toUpperCase this works if when I try to use only the string "Hello world" in the previous statement it gives me an error because it should not work the same as when I call the constructor function using new in person?
That is, at that point where "Hello world" is not implicitly invoking a constructor and I do not get any reference and therefore I cannot invoke the method of the reference returned by the constructor method because the constructor method is not being calling but nevertheless if I use a string together with a method of the String class this works. Could someone explain to me exactly what happens?
You're doing the same thing in your code, the compiler doesn't know what to do with the string
"Hola mundo"
because it's an instruction that doesn't make sense on its own in the same way that spitting out a number at the bakery doesn't make sense on its own either, it would have had the same effect write3;
.Let's change the story:
The baker has understood my request, but I have not done anything with it. That's what the code does:
"Hola mundo".toUpperCase();
, it would have had the same effect to write3.toString();
To explain this question we can ask ourselves another one before:
In the example you put:
It gives error, but
It doesn't.
The first case is called "Implicit declaration", and it has an important difference with the second case. In the implicit declaration it is possible that a new object is not created, since before it is checked if an exactly equal string exists in the so-called "String Constant Pool", and if so, instead of creating a new object gets a reference to the existing object.
That's why the first case fails. It doesn't make sense to get a reference to an object that isn't going to be stored anywhere. The second case of explicit declaration if, since the constructor of the object is called (although it doesn't make much sense).
The error you get when you do
"Hola mundo";
is:It's clear, isn't it? Defining a string without assigning it to a variable is useless.
In the second case you don't have any errors and the method
toUpperCase()
is executed but it's the same - you're not assigning the value that your method returns to any variable. That is - it is useless.The error that the program returns when you have an unassigned String as indicated by Evgeni is a message that the compiler sends you so that you know that you have useless code consuming memory since the String is not assigned anywhere.
In the case of
new Person()
is different because by instantiating a class you are actually calling its constructor in which you can have logic (not very good practice by the way) so it makes sense for the compiler to be more lax in this case because it doesn't can ensure that your statement is a waste of memory (although it most likely is).