What is the difference between uninitialized object variable and object variable initialized to null in Java

I have the following two object variables

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Date a;
Date b=null;
</code>
<code>Date a; Date b=null; </code>
Date a;
Date b=null;

Definitely both ‘a’ and ‘b’ are not referring to any objects.

Now if I invoke following statement

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>System.out.println(a.toString());
</code>
<code>System.out.println(a.toString()); </code>
System.out.println(a.toString());

There will be a compile time error, whereas if I invoke the following statement

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>System.out.println(b.toString());
</code>
<code>System.out.println(b.toString()); </code>
System.out.println(b.toString());

There will be no compile time error but there will be a runtime error. What is the reason for this and what value will be actually stored in ‘b’ to represent a null value?

5

Thats because the state of local variables is controlled within its scope

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> // method/loop/if/try-catch etc...
{
Date d; // if it's not intialised in this scope then its not intialised anywhere
}
</code>
<code> // method/loop/if/try-catch etc... { Date d; // if it's not intialised in this scope then its not intialised anywhere } </code>
 // method/loop/if/try-catch etc...
 {
   Date d; // if it's not intialised in this scope then its not intialised  anywhere
 }

Which is not the case for fields

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Foo{
Date d; // it could be intialised anywhere, so its out of control and java will set to null for you
}
</code>
<code>class Foo{ Date d; // it could be intialised anywhere, so its out of control and java will set to null for you } </code>
class Foo{
 Date d; // it could be intialised anywhere, so its out of control and java will set to null for you
}

Now, why its fine to set a variable to null and use it immediately? maybe that is a historical mistake that sometimes leads to horrible mistakes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> {
Date d = null;
try{
}catch{ // hide it here
}
return d;
}
</code>
<code> { Date d = null; try{ }catch{ // hide it here } return d; } </code>
 {
  Date d = null;
  try{
  }catch{ // hide it here 
  }
  return d;
 } 

Now what’s the semantic difference?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Date d;
</code>
<code>Date d; </code>
Date d;

just declares variable that can hold a reference that points to an object of type Date, however

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Date d= null;
</code>
<code>Date d= null; </code>
Date d= null; 

does exactly the same but the reference is pointing to null this time, null is like any reference, it takes a space of a native pointer, that is 4 byte on 32-bit machines and 8 bytes on 64-bit machines

8

There is no difference for class fields. They are null by default for objects, 0 for numeric values and false for booleans.

For variables declared in methods – Java requires them to be initialized. Not initializing them causes a compile time error when they are accessed.

What’s the reason? Class fields can be modified by any method. In any order the method is invoked. All non-private fields may be modified by other classes and/or classes extending that class. Hence, there is no point in notifying about an uninitialized variable, since it may be assigned in many, many places.

Variables inside methods, however, are local and can be modified only inside the method itself. Hence it is both possible and rational to point possible mistakes. And the compiler tries to do it. If it knows the field is not initialized, it will show an error, because that’s never what you want. If it is not sure – it will give a warning, so that you can make sure.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static class Test {
Date a; // ok
Date b = null; // ok
public void test() {
Date c;
Date d = null;
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(c.toString()); // error
System.out.println(d.toString()); // warning
}
}
</code>
<code>public static class Test { Date a; // ok Date b = null; // ok public void test() { Date c; Date d = null; System.out.println(a.toString()); System.out.println(b.toString()); System.out.println(c.toString()); // error System.out.println(d.toString()); // warning } } </code>
public static class Test {
    Date a; // ok 
    Date b = null; // ok

    public void test() {
        Date c;
        Date d = null;

        System.out.println(a.toString());
        System.out.println(b.toString());
        System.out.println(c.toString()); // error
        System.out.println(d.toString()); // warning
    }
}

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật