Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: New to Java

  1. #1
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default New to Java

    Hello everyone, I'm starting to learn Java (only had 1 class till now but I already got some programs to work on) so bare with me.

    My question is :

    long i =Integer.parseInt(args[0]);

    When I have this,

    1. What does long mean ?

    2. What's the whole Integer.parseInt(args[0]) part for ?

    When I compile the program, I gotta write it like this {"5"} or else it won't work.

    I have a function apart that does the factorial of a number so that's what the program is about.

    Any help ?

    Cheers!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: New to Java

    A long is like an int but it takes eight bytes so it can store much larger numbers. That args[0] mumbo jumbo converts a String (the first command line argument args[0]) to an int. You'll learn all about it in the next lessons.

    kid regards,

    Jos
    JeremyLin likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: New to Java

    Thanks Jos.

    I've understood the long part.

    About the args[0] what will it convert to an int in this case ?
    What's the purpose of using it in this case ?

    Cheers!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: New to Java

    Quote Originally Posted by JeremyLin View Post
    About the args[0] what will it convert to an int in this case ?
    What's the purpose of using it in this case ?
    The 'args' array is an array of Strings; it contains the command line arguments, so args[0] is the first command line argument (if it exists).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: New to Java

    Quote Originally Posted by JosAH View Post
    The 'args' array is an array of Strings; it contains the command line arguments, so args[0] is the first command line argument (if it exists).

    kind regards,

    Jos
    Hmm, thanks.
    I don't think there exists any command line argument though :o

    I mean, I do compile it using {"7"} so I'm guessing that would convert "7" to 7 right ?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: New to Java

    Quote Originally Posted by JeremyLin View Post
    Hmm, thanks.
    I don't think there exists any command line argument though :o

    I mean, I do compile it using {"7"} so I'm guessing that would convert "7" to 7 right ?
    Right; if there are no command line arguments the expression args[0] would cause your program to throw an ArrayIndexOutOfBoundsException and stop. Your version simply converts the String "7" to an int (which is 7 of course).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: New to Java

    Alright.
    Thanks a lot Jos, I'll be posting some doubts I may have later :p

    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •