• Initializing a boolean variable : boolean b=true;
  • In some cases we need to initialize all values of boolean array with true or false.
  • In such cases we can use Arrays.fill() method
  • Arrays.fill(array, Boolean.FALSE);
  • java initialize boolean array with true:  Arrays.fill(array, Boolean.FALSE);
  • Lets see an example java program on how to assign or initialize boolean array with false or true values.



#1: Java Example program on initializing boolean array.

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview questions
  6.  * 
  7.  * Description: Initialize boolean array values with false or true
  8.  *
  9.  */
  10. public class InitializeBoolean {

  11. public static void main(String[] args) {
  12. Boolean[] array = new Boolean[4];
  13. //initially all values will be null
  14. for (int i = 0; i < array.length; i++) {
  15. System.out.println(array[i]);
  16. }
  17. Arrays.fill(array, Boolean.FALSE);
  18. // all values will be false
  19. for (int i = 0; i < array.length; i++) {
  20. System.out.println(array[i]);
  21. }
  22. Arrays.fill(array, Boolean.TRUE);
  23. // all values will be false
  24. for (int i = 0; i < array.length; i++) {
  25. System.out.println(array[i]);
  26. }
  27. }

  28. }

Output:

  1. null
  2. null
  3. null
  4. null
  5. false
  6. false
  7. false
  8. false
  9. true
  10. true
  11. true
  12. true


java initialize boolean array with true

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu