Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Plz.. help me out to understand the java concepts regarding arrays

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Plz.. help me out to understand the java concepts regarding arrays

    when i write the code:

    int a;
    System.out.println(a);

    compiler flags an error that a might not have been initialized.

    but when i write this

    int [] a= new int[10];
    System.out.println(java.util.Arrays.toString(a));

    compiler doesnot flag any error
    and gives a output
    [0,0,0,0,0,0,0,0,0,0]

    I want to understand from where the integers stored in the array are getting initial value 0 without any expilicit initialization.
    Plz.. help me out i am a beginner in java .


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Plz.. help me out to understand the java concepts regarding arrays

    That's just the default value populated in an array- 0 for ints, null for Objects. It's similar to how class variables (not method variables) get a default value, even if you don't explicitly give them one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Plz.. help me out to understand the java concepts regarding arrays

    Quote Originally Posted by KevinWorkman View Post
    That's just the default value populated in an array- 0 for ints, null for Objects. It's similar to how class variables (not method variables) get a default value, even if you don't explicitly give them one.
    thanks a lot kevin for ur help.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Plz.. help me out to understand the java concepts regarding arrays

    Quote Originally Posted by Arindam Sarkar View Post
    thanks a lot kevin for ur help.
    No problem. Keep in mind that had you done this instead:

    int [] a; //no declaration!
    System.out.println(java.util.Arrays.toString(a));

    ...you would have received a similar error.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Please help! Java ARRAYS
    By Score11 in forum Object Oriented Programming
    Replies: 11
    Last Post: January 15th, 2012, 05:45 AM
  2. Replies: 15
    Last Post: November 4th, 2011, 03:52 AM
  3. Replies: 4
    Last Post: February 28th, 2011, 09:49 AM
  4. New To Java (Help with arrays)
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2010, 05:48 AM
  5. Replies: 5
    Last Post: June 10th, 2010, 10:19 AM