Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null in a boolean?

 
keerthidhar dongre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String args[])
{
Boolean a[]=new Boolean[4];
int I=1;
System.out.println(a[I]);
}
Why does this print null? when the only values a boolean can hold are true and false? Also why does this compile since the array elements are not initialized?
Thanks in advance,
Keerthi
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boolean is a wrapper class and boolean is primitive which can hold only true or false. What you have is an array of 4 Boolean type object references.
Arrays are always initialized, even if they are local to the method.
HTH,
- Manish
 
keerthidhar dongre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Manish.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic