That program is wrong and the best explanation of why it has been done in one way or another is: because the author has made a mistake .
Do not use that program as an example of how to program.
We can see that this program is incorrect if we enter an empty string in the dialog. This demo shows it: link
The result is:
The longest word is: null
Its length is: 0
Which is obviously false. The phrase is empty so it does not have the word null.
If we start showing " "
instead of null
we have a much more correct result (demo: link ):
The longest word is:
Its length is: 0
It is therefore clear that it is not the same to initialize the variable show a null
or a " "
.
That difference will only be noticed if the string read from the dialog is empty or contains only spaces, in which case the following conditional is never executed:
if (palabra.length() - 1 > c) {
c = palabra.length() - 1;
mostrar = palabra.trim();
}
And when the variable variable is not executed, it remains with the value with which it was originally initialized. That's why you notice the difference between assigning null
or " "
. In any other case, the variable is overwritten by that conditional and you will not notice functional difference in those other cases.
Regarding the difference between null and "" in general.
It is easy to find wrong ideas about null
like:
- null means "no value"
- If you initialize the variable with null value, it indicates that it does not contain any value.
- When declaring a variable this is null because it has no value
All previous statements are incorrect. The Java Language Specification tells us what is null
:
3.10.7. The Literal Null
The null type has a value, the null reference, represented by the
literal null, which is formed by its ASCII characters.
NullLiteral: null
A null literal is always of the null type (§4.1).
About the null type the specification says :
4.1. The categories of Types and Values.
(.. part omitted because it is not relevant ..)
There is also a special type null, the type of the expression
null (§3.10.7, §15.8.1), which has no name.
Since the null type does not have a name, it is impossible to declare a variable of
type null or do a conversion (cast) to type null ..
The null reference is the only possible value of an expression of type
null.
The null reference can always be assigned or converted (cast) to any type of reference (§5.2, §5.3, §5.5).
In practice, the programmer can ignore the null type and
simply pretend that null is a special literal that can be
any reference type.
Emphasis mine. The bold part is the answer to this question. null
is a special value, is a value that can be assigned to any variable of type reference.
Therefore all the following lines are correct:
Integer i = null;
String s = (String) null;
List<String> ls = (List<String>) null;
Object o = null;
" "
however it is a string literal and when assigning it to a variable with the operator =
we are assigning a reference to an object of type String
that contains the string of that literal. That reference of type String can not be assigned to any variable, it can be assigned only to declared variables of type String
or a type that is superclass of String
. In this case only Object
is superclass of String
.
For this reason two of the following lines are incorrect:
Integer i = ""; //Error de compilación
String s = (String) ""; // Correcto
List<String> ls = (List<String>) ""; //Error de compilación
Object o = ""; // Correcto