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

Thread: New to Java

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default New to Java

    Hi, Im new to java, coming from C++, and i had a question about the first line of code.
     public static void main(String[] args)
    in the code i understand public static void main, but i dont understand what is in the brackets. What are they for, and would i need to change them for a code that didnt just print out "hello world"?
    Thanks


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to Java

    Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)

    Read carefully the part about 'signature'. The JVM needs to know whereabouts in your application to start executing code, so the standard is that it loads the class you tell it to and invokes main(String[]), passing command-line arguments as a String array.

    If you're up to a coding challenge, change HelloWorldApp in the link above to say "Hello [whatever]" where "[whatever]" is a command-line argument. That is, if you start HelloPottsiex5App like this:
    java HelloPottsiex5App coders
    It should print the message:
    Hello coders!

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to Java

    Hey, Im still a little confused. When you say a command-line arguement, is that the same as parameters? And if it is, then i still dont understand how the parameters of function main can be the String[]... What did you mean it passes command-line arguements as a String array?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to Java

    Do you ever run java applications from the command line by typing "java MyApplication"? If you don't, this is going to be a tortuous explanation.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to Java

    Oh you mean using the command promt/terminal? I dont regularly run them in there, but i think i understand what you mean now. So if i were to run it in the command prompt or terminal and i were to use "java MyApplication" and then add "[whatever]" on to that then i could have the input of a variable be the [whatever]?
    I remember something like that from batchfiles...

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to Java

    then i could have the input of a variable be the [whatever]?
    Yes, like that, but it only works in the special case of the main(String[] argumentsArray) method. When the JVM starts, it takes whatever is on your commandline after the name of the class to load and turns those strings into a String[], which it then passes to the main(String[]) method.

    Say for example you type at the command prompt:
    java MyAddingApp 2 2
    What the main method sees in its String[] argument (you can give it any name you like, args is only a Java programmer habit) is the same as if some other code had invoked your main method like this:
    MyAddingApp.main(new String[]{"2", "2"});
    and you can access the command arguments like this:
    System.out.println(args[0] + " + " + args[1] + " = something");
    which would print
    2 + 2 = something
    You can add the "2"s like this to make a longer string because they're strings. To make them into integers (int, probably), you'd have to explicitly convert them first.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to Java

    Awesome

    So for your challenge it would be this

    public class CommandLineTest
    {
        public static void main (String[] args)
        {
            System.out.println ("Hello, " + args[0]);
        }
    }
    ?

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: New to Java

    ?
    I'm assuming - since that looks like valid Java - you know the answer already

    That's right.

  9. The Following User Says Thank You to Sean4u For This Useful Post:

    pottsiex5 (September 20th, 2011)