Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:05:55+00:00 2026-05-25T13:05:55+00:00

public class test { public static void main(String[] args) { int MAX = 5;

  • 0
public class test {
    public static void main(String[] args) {
        int MAX = 5;
        boolean bit[] = new boolean[MAX];
        float[] value = new float[2*3];
        int[] number = {10, 9, 8, 7, 6};
        System.out.println(bit[0]); // prints “false”
        System.out.println(value[3]);   // prints “0.0”
        System.out.println(number[1]);  // prints “9”
        }
    }

I’m testing out the above code, how come Java would initialize the values for you? I thought it should throw compilation error if I don’t initialize my variables. Also, what should I do to the line float[] value = new float[2*3]; if I want to initialize them all to 0.0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:05:56+00:00Added an answer on May 25, 2026 at 1:05 pm

    Static and instance variables, and elements of arrays are initialized to default values (0, false, '\0', null etc). Local variables aren’t initialized by default.

    In your code, only the array elements aren’t explicitly initialized – the compiler would have a hard time working out whether every array element you tried to use was initialized. What would you expect to happen if the array came from a parameter, for example?

    From the Java Language Specification, section 15.10.1:

    Then, if a single DimExpr appears, a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).

    (That’s the situation you’re in – DimExpr is your 3 * 2 expression here.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) { } } class Outer
public class Test { Integer i; int j; public static void main ( String
I have the following code: public class Test { public static void Main() {
Here is a little test program: #include <iostream> class Test { public: static void
E,g class Test { public: void setVal(const std::string& str) { this.isVal = str; //This
Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
Give the following (straight-forward) code: public class pr1 { public static void f1(){ long
I have a class which looks something like this: public class Test { private
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.