• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Please explain. Boolean[] b1 = new Boolean[10];

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if run the following code?
1: Boolean[] b1 = new Boolean[10];
2:
3: boolean[] b2 = new boolean[10];
4:
5: System.out.println("The value of b1[1] = " +b1[1]);
6: System.out.println("The value of b2[1] = " +b2[1]);
A) Prints The value of b1[1] = false
The value of b2[1] = false
B) Prints The value of b1[1] = null
The value of b2[1] = null
C) Prints The value of b1[1] = null
The value of b2[1] = false
D) Prints The value of b1[1] = false
The value of b2[1] = null
The answer is C .
please explain why answer is C . I think this is wrong.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is right.
you have two arrays, one able to hold references to Boolean objects and one holding boolean primitive values.
If you don't initialize each element of the array they are initialized by default. In Object arrays, each element is initialized to null and in primitive arrays, each element is initialized to some value, in the case of boolean array that value is false.
So when you query the value of the second element of each array that you haven't initialized explicitely, they give you their default value which are null for b1[1] and false for b2[1].
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mr Iftikhar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin Crettaz
I know the thing that the default value of object is null
and default value of boolean variable is false.
But how you are differenting the the line 1 is Boolean
object array and line 3 creating the Boolean primitive
array values.

1: Boolean[] b1 = new Boolean[10];
2:
3: boolean[] b2 = new boolean[10];
please explain me.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the first line Boolean is written with an initial uppercase which corresponds to the class Boolean in package java.lang
On the second line boolean is written with an initial lowercase which corresponds to the boolean primitive type.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mr Iftikhar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin Crettaz
This was a silly but i have not proper concentration.
 
Evacuate the building! Here, take this tiny ad with you:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic