Some super n00bie Java Q's

PigCorpse

[H]ard|Gawd
Joined
Sep 4, 2005
Messages
1,115
Hi!

I'm beginning AP Computer Science at my school and we're doing Java.

I've run into some things that I would like to ask:

1. 'public static void main (String[] args)' In the '(String[] args)' part, will there ever be anything else that can be there? I searched and all of the tutorials I've read have (String[] args), nothing else. What is the purpose of that whole part? What does the 'args' mean?

What I think it means is that after typing 'java foo <inputs>', that the inputs will be assigned to args[0] and [1] and so on. Does that mean that I can have an unlimited number of inputs? If I type 'java foo' with 100 inputs, will it automatically create 'args[0]' to 'args[99]'?

What will happen if I type '(String[3] args)'? Or if I change 'args' to 'nums'? Will it make all the arguments 'nums[0]' and so on?

And the fact that it's 'String[]' means that the inputs will be strings?

Thanks a lot!
 
Although these are good questions you are trying to move too far ahead if you just started in my opinion. Public static void main is a method. It is the place where you will call other methods from. As you take your classes over time you will reach the points where you will learn what these things mean but for where you are at now they aren't what you should be concentrating on. String[] name is java's way of creating an array of type string. An array is a data structure for storing contents. name[0] would refer to the string stored in the first section, name[1] would refer to the next and so on. You should not bother trying to call or deal with args[] at this point. string[] args stores each parameter from the command line. If you don't send a paramter through the command line any call on args will give you an arrayIndexOutOfBoundsException. No doubt you will come up with many questions in your studies. Learn to use the java documentation and using search engines.
 
PigCorpse said:
1. 'public static void main (String[] args)' In the '(String[] args)' part, will there ever be anything else that can be there? I searched and all of the tutorials I've read have (String[] args), nothing else. What is the purpose of that whole part? What does the 'args' mean?

This is one of the entry points the JVM (Java Virtual Machine) looks for when it is told to run a class as an executable program. Think of it like the entrance to the house - it's gotta look a certain way for the JVM to recognize it. It will always follow the same method signature - public static void main(String[]). You can probably name the parameter anything you'd like - why don't you try it yourself?

PigCorpse said:
What I think it means is that after typing 'java foo <inputs>', that the inputs will be assigned to args[0] and [1] and so on. Does that mean that I can have an unlimited number of inputs? If I type 'java foo' with 100 inputs, will it automatically create 'args[0]' to 'args[99]'?

Yes, you are correct. It will automatically populate them all for you.

PigCorpse said:
What will happen if I type '(String[3] args)'? Or if I change 'args' to 'nums'? Will it make all the arguments 'nums[0]' and so on?

Yes. Go ahead and try it!

PigCorpse said:
And the fact that it's 'String[]' means that the inputs will be strings?

That is correct. You can then iterate over that array and do nifty things to them, like convert them to integers if you need to, or use an argument as a filename to open, etc, etc. I am sure you will be getting into all of that in your class.

PigCorpse said:
Thanks a lot!

You're welcome. Don't get too far ahead of yourself. It is good to be inquisitive however don't go off on too many tangents or you will miss the core competencies your class is trying to teach you - to "think" like a computer scientist.
 
Great questions, but...

seriously, if you have a question on "how does this work" or "will it do this?",just give it a shot. Google is a much more effective (and typically much faster responding) then posting something here. You will soon learn that Google is your best friend. Just try typing an error message you get at the console one time into google, you'll be amazed how quickly you find the answer to your problem.

Just some advice from someone who was struggling to understand Java just like you a few years back... hope it helps! Good luck!
 
Thanks a lot to all of you! Your advice has really helped me out a lot.

And yes, I am going way ahead of myself. In school, it's really enough for us to just know that something will work and how to use it. Outside of what will be tested on the AP exam, we're not really taught anything. I tend to wonder about all the other possibilities, and thanks to all of you, they've become a lot clearer.
 
Icemastr said:
Although these are good questions you are trying to move too far ahead if you just started in my opinion.
Sorry to pick at a post, but these are the first questions I asked (and so did a lot of my colleagues). Don't discourage anyone from asking this sort of question - learning by rote won't help anyone, and to start your programming life with a question is possibly the best way :)
 
Back
Top