1

The code correctly prints out "x[2][2] is false", my problem is understand why this is happening. (It's correct, I just need someone to "computer speak" this logic to me- I'm taking my final in 4 hours and will never bother anyone again :) )
Thank you so much for your assistance!

public static void main(String[] args) {
    boolean[][] x = new boolean[3][];
    x[0] = new boolean[1];x[1] = new boolean[2];
    x[2] = new boolean[3];

    System.out.println("x[2][2] is " + x[2][2]);
}
2
  • In the future, don't wait until hours before the final to discover you don't know something key to the language. You'll be happier for it, trust me. Commented Dec 12, 2011 at 17:19
  • Why are these questions randomly voted up? Commented Dec 13, 2011 at 1:48

6 Answers 6

3

When you create an array, each entry gets a default value. For boolean, the default value is false. (For numeric primitives, the default value is zero. For reference types, the default value is null.)

So when you create the top-level array, boolean[][] x = new boolean[3][];, x is a 3-element array of boolean arrays, with each element set to the default value of null. (A single array of primitive types is a reference type.) The program then initializes each element of x with a new array of boolean primitives, each filled with false elements. Note that the arrays have different lengths; this is not a problem in Java. As it happens, x[2][2] actually exists (unlike, say, x[1][2]), so the call to println succeeds.

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

3 Comments

Here's the section on default values in the Java Language Spec.
^^ This has been the biggest help- I really appreciate your (constructive) input!!
@FlorianFirlus, welcome to Stack Overflow - I'm glad it was helpful to you. Don't forget to vote up the correct answer (and any answers you found useful) and click the checkmark next to the answer that best solved your problem.
2

When you create a new boolean[n] all the boolean values default to false (which is also the default for boolean)

Comments

1

When you declare a boolean array, it automatically defaults to having all it's values 'false'.

Comments

0

Array elements are initialized to their defaults which is false in case of boolean primitives. Thus new boolean[3] will result in {false, false, false};

Comments

0

It is because variables of all primitive types have default value. You cannot use regular variable without initialization but when you create array each element is initialized automatically. int, long, short to 0, float and double to 0.0, boolean to false.

You do not initialize elements of your 2 dimensional array. So all its elements are false by default.

1 Comment

What is this "regular variable" of which you speak?
0

The default value of booleans in Java is false no matter how many and how much you allocate it.

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.