Why is a unified object in Java not set to null?

I have a very basic problem in which I could not find a solution, even if it should exist.

I have the following code:

public class Foo { public static void main(String[] args) { String foo; if (foo != null) { System.out.println("foo"); } else { System.out.println("foo2"); } } } 

It gives me

'' may not have been initialized ''

. Why should I assign null explicitly, and not all variables are initialized to zero by default?

+4
source share
5 answers

This prevents the local variable from being accidentally used before it is definitely assigned. This is good - it helps prevent mistakes. It is a shame that this cannot be done for fields (instance and static variables) - but apart from the final variables, the compiler does not know when you are going to read them, and when you are going to assign them - this cannot indicate the order in which you are going to call methods. Within one method, it has much more flow control information, so it can provide you with additional verification.

If you want the local variable to be null, why don't you just specify null? This not only holds back the compiler, but also makes it clear what your intentions are.

For more information on a specific purpose, see Chapter 16, Java Language Specifications .

Here is an example of how it can prevent errors:

 Iterable<String> collectionOfNames = ...; // Some method call String lastNameInCollection; for (String name : collectionOfNames) { lastNameInCollection = name; } System.out.println("The last name was: " + lastNameInCollection); 

What happens if the collection is empty? Maybe we want to print "Last Name was: null" - but maybe we need to do something else. The fact that the above will not compile (since lastNameIncollection may not be assigned) makes the programmer think about where the collection of names is empty.

+15
source

If you leave the variable uninitialized, sometimes it’s good to let the compiler verify that it has been assigned in all possible code branches before using it. For example, if you have a complex condition that assigns a variable and should never be null :

 String variable; if (conditionA) { if (conditionB) variable = "B"; else variable = "A"; } else { switch (conditionC) { case 1: variable = "C1"; break; case 2: variable = "C2"; break; default: variable = "CD"; break; } } System.out.println(variable.length()); 

If you forget to assign a variable in one of the branches, the compiler will complain. Since you know that you never assign null , you can safely use a variable without checking it for null . If you initialize the variable with null in the definition, and you forget to set the variable to a value that the compiler cannot check, and you can get a NullPointerException :

 String variable = null; if (conditionA) variable = "A"; // NullPointerException if conditionA is false, not check by compiler System.out.println(variable.length()); 

A variable can also be final, in which case it can only be assigned once. If you initialize it with a default value, it would already be assigned and cannot be reassigned:

 final int variable; if (condition) variable = 1; else variable = 2; 
+2
source

Simple answer:
Local member variables are not automatically initialized to null. But class member variables.

This code will work

 public class Foo { String foo; public static void main(String[] args) { if (foo != null) { System.out.println("foo"); } else { System.out.println("foo2"); } } } 

Or this one

 public class Foo { public static void main(String[] args) { String foo = null; if (foo != null) { System.out.println("foo"); } else { System.out.println("foo2"); } } } 

If you want a complete in-depth understanding of this topic, consider Jon Skeet's answer and check out its link.

+1
source

It may also be completely unnecessary to initialize it if it is initialized definitely later:

  String foo; if (specialUseCase) { foo = "this"; } else { foo = "that"; } 
+1
source

Since local variables are not automatically initialized, you must explicitly initialize them. Only member variables are initialized to zero or the default value, even if you do not explicitly initialize. If you save String foo; outside the main () method, you do not need to initialize.

-1
source

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


All Articles