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 13 of 13

Thread: I dont understand this code ?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I dont understand this code ?

    why this code only displays four zeros ?????


    public class VargjetUshtrimi2 {

    public static void main (String a []) {
    int r[] = new int[11];
    for (int i = 1 ;i < 10; i++)
    {System.out.println( r[i] );}


    }
    }


  2. #2
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: I dont understand this code ?

    String args[], not a[]. You are not initializing an array within the parameters.

    Moreover, your array r is uninitialized. Initialize it with numbers before printing it.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont understand this code ?

    The code works good and its not problem with (String a[]) but i dont know why when i run it only displays four zeros like this
    0
    0
    0
    0

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I dont understand this code ?

    Please post your code correctly per this link.

  5. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: I dont understand this code ?

    Because Your array stores "garbage value" for now. You haven't initialized it with numbers, you have just declared it. So it prints 0 as default.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I dont understand this code ?

    @Abhilash
    String args[], not a[].
    There are no requirements for the name of the String array. a is as good as args.
    I prefer the array notation like this: String[] args
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont understand this code ?

    Thanks Abhilisha

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I dont understand this code ?

    why this code only displays four zeros ?????
    I also don't understand why that code only shows 4 zeros. It should show 9 zeros!!!
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: I dont understand this code ?

    Quote Originally Posted by Abhilash View Post
    Because Your array stores "garbage value" for now. You haven't initialized it with numbers, you have just declared it. So it prints 0 as default.
    In java an array is always automatically initialized when you construct it. You never have garbage data.

  10. #10
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: I dont understand this code ?

    Actually, by garbage value I means the array does no store anything for now (i.e. it hasn't been initialized in the program). Although java initializes it automatically, but this method shouldn't be used.

  11. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: I dont understand this code ?

    Quote Originally Posted by Abhilash View Post
    Actually, by garbage value I means the array does no store anything for now (i.e. it hasn't been initialized in the program).
    Yes of course it does, it is initialized with zeroes. It always is, thats what the java specification says.
    This is a question about teminology. "Garbage Data" does not exist in java. And all variables are always initialized, the compiler makes sure they are.

  12. #12
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: I dont understand this code ?

    I don't get the question because it does display 9 zeroes. I copied it and ran it. It works!

  13. #13
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: I dont understand this code ?

    Quote Originally Posted by lil_misfitss View Post
    I don't get the question because it does display 9 zeroes. I copied it and ran it. It works!
    Well, good for you then.

Similar Threads

  1. Replies: 0
    Last Post: April 7th, 2014, 01:39 PM
  2. i dont understand whats wrong with my code, please fix it.
    By gvnvhri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 26th, 2013, 04:32 AM
  3. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  4. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM