Why does an empty type declaration of type show a compilation error, but not for a null declaration?

public static void main(String[] args) { String s1 = null; String s2; s1.trim(); //No compile error. But run time error s2.trim(); //compile error. } 

In the above code there is no assignment for s1 after initialization, the compiler knows s1 is null . Then why not show a compilation error for s1.trim() as s2 ?

+4
source share
9 answers

Since s2 not initialized and s1 is null initialized

For Que 1: Do trim() Operation on null actually, so it will throw NPE ( NullPointerException ), as it should.

For Que 2: See §4.12.5 JLS for a very detailed explanation:

A local variable must be explicitly set before using it, by initialization or assignment, so that it can be verified by the compiler using rules for a specific purpose.

+4
source
 String s2; 

This is a local variable. Local variables are not assigned default values ​​that you must provide before use, otherwise the compiler will complain. The reason you cannot compile the code.

 String s1 = null; 

You are initializing this local variable. Thus, there is no compilation problem, but you cannot perform a trigger operation on something null. This way you get NPE, which is a run-time exception that should not be cached by default (although you can).

+2
source

Since s1 is initialized, the compiler is happy, although it may give a warning about a possible NullPointerException

0
source

1) Null pointer exception .. since there is no object, but it works with zero; 2) Local variables cannot be used without initialization

0
source

Local variables must be explicitly initialized for use. Setting s1=null is initialization, albeit relatively useless. Since s2 is on the stack and not initialized, it cannot be used .

s1 gets an exception at runtime, but s2 gets a compile-time error.

0
source

When creating a String object, we must initialize some value.

  String s1 = null; 

It contains a null value.

  String s2; 

But the value of the s2 variable is null, but not assigned.

0
source

This is because the java compiler is looking for an assignment operator (=) when compiling the code. When the compiler starts the "=" sign in case of s1, it considers s1 to be initialized regardless of the initialization value. While in the case of s2, the compiler does not find an assignment operator, therefore, in the lexical phase, it makes an entry in the error table, indicating that "the variable s2 may not have been initialized"

0
source

The Java compiler checks to see if the local variable is initialized or not. If not, the compiler throws an error. This is necessary because, unlike an instance variable, java will not initialize a local variable with its default value. After the variable is initialized, even if it is initialized to zero, the compiler will not complain, because the compiler assumes that the variable will be reinitialized with non-zero values ​​somewhere during the program. The compiler cannot in any way check which value was reinitialized during the program, since the value can be any at run time. But if the value is still null, it will call the instance method, the JVM throws a NullPointerException because it is known only at run time. Has the meaning?

0
source

Take this example for a specific purpose, taken from JLS:

 { int k; int n = 5; if (n > 2) k = 3; System.out.println(k); /* k is not "definitely assigned" before this statement */ } 

We can say that the compiler knows that execution will certainly reach inside the if block, as a result of which the value k will be assigned, so it should compile fine, but it is not.

I think this is a decision made by the java authors, how reasonable they want to be a compiler, given the acceptable compile-time values ​​and / or other factors.

0
source

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


All Articles