4
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 compiler knows s1 is null. Then why not showing compile error for s1.trim() like s2?

1
  • Maybe off topic, but if you use eclipse, you will get warning for this Commented Aug 2, 2013 at 12:36

9 Answers 9

4

Because s2 is not initialized and s1 is initialized with null

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

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

A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified by the compiler using the rules for definite assignment.

Sign up to request clarification or add additional context in comments.

Comments

2
String s2;

This is a local variable. Local variables are not given default values you must provide one before using it or else compiler will complaint. The reason why you are not able to compile the code.

String s1 = null;

You are initializing this local variable. So no compilation problem but you cannot perform trim operation on something that is null. So you are getting NPE which is a runtime exception which by default must not be cached(though you can).

1 Comment

@Keyser if it's an instance variable then default value is automatically assigned(null in case of String) and in that case you will not get compilation issues but NPE.
0

Since s1 is initialized the compiler is happy, though it may be giving warning about possible NullPointerException

Comments

0

1) Null pointer exception .. since there is no object but operating on null; 2) Local variables cant be used without initialization

Comments

0

Local variables must be explicitly initialized to be used. Setting s1=null is an initialization, albeit a relatively useless one. As s2 is on the stack and uninitialized it cannot be used.

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

Comments

0

At the time of Creating String Object we Should initialize some value.

   String s1 = null;

It hold null value.

   String s2;

But variable s2 null value but not assigned.

Comments

0

It is because java compiler looks for assignment operator (=) while compiling code. When compiler triggers "=" sign in case of s1, It considers as s1 initialized irrespective of initialization value. While in case of s2 , compiler does not find assignment operator so in lexical phase it make an entry in error table stating "variable s2 might not have been initialized"

Comments

0

Java compiler will check whether the local variable is initialized or not. If not, the compiler throws an error. This is required because unlike instance variable, java will not initialize the local variable with its default value. Once the variable is initialized even though it is initialized with null, the compiler won't complain because the compiler assume that, the variable will get re-initialized with non-null values somewhere during the course of the program. Compiler, no way can check what value got re-initialized during the course of the program as the value can be anything during runtime. But if the value is still null, will calling an instance method, the JVM throws NullPointerException because it is known only at Runtime. Makes sense?

Comments

0

Let us take this example on definite assignment 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 */
}

One might say the compiler knows the execution will certainly reach inside the if block, resulting in the assignment of k so it should compile fine, but it doesn't.

I think it's a decision taken by java authors how smart they want the compiler to be, keeping in mind acceptable values of compilation time and/or other factors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.