0

I have two questions in Java:

  1. I know that uninitialized object is automatically assigned null but this code doesn't work -

    A a;        //A is some valid user-defined object  
    System.out.println(a != null);
    

    This gives error : error: variable a might not have been initialized.
    This indicates that a is not null. So, how do I differentiate between null and these kind of vaiables?

  2. I know that constructor doesn't return any value not even void and return returns void, so how can the following code work perfectly?

    A() {
        //...Some code
        return;        //Works perfectly even if it returns void from inside construtor
    }
    
1
  • 1
    History: In C/C++ all variables would be unitialized. With java all fields were automatically zeroed to ease object creation. However for local variables there is no automatic default, as that would be cause of errors with unintended zeroes / forgotten cases: int a; if (...) { a = 42; } else { ... } Commented Jan 2, 2018 at 12:12

3 Answers 3

1

From next time consider asking only one question at a time.

I know that uninitialized object is automatically assigned null but this code doesn't work -

That applies for instance members. Not for local variables. IF your code is inside a method you should consider assigning.

I know that constructor doesn't return any value not even void and return returns void, so how can the following code work perfectly?

Just a plain return; terminates the execution there. It won't return anything and that is valid, just like the break; statement.

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

1 Comment

@suresh, I get that return terminates the execution there but it returns void. How is it dealt with? Does the compiler simply ignore it? or there's some internal mechanism.
0

Answer to your first question:

Since this 'a' is a local variable they are in undefined states if not explicitly defined.Only instance variables are assigned null as the default value. This is why you get the above mentioned compilation error.

Please refer to oracle documentation here for more information.

The compiler will assign a reasonable default value for fields of the above types; 
for local variables, a default value is never assigned

Answer to the second question:

In constructor you can only write return to stop execution. You can't write return with some value. It basically implies that the function will finish processing the current routine and will return to the calling routine.

Comments

0

In the first question, only uninitialized global variables are assigned 'null' not local variables.

In the second question, the return is working as it works in any method that is 'return to the caller'. To understand better look at this code.

class practice {
    practice(){
        for(int i=0 ; i<10 ; i++){
            System.out.println(i);
            if(i==5)return;
        }
    }
    public static void main(String[] args){
        new practice();
    }
} 

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.