The modifiers public and static can be written in either order(public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. For example:
java MyApp arg1 arg2
Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:
-descending
The "Hello World!" application ignores its command-line arguments, but you should be aware of the fact that such arguments do exist.
... most programmers choose "args" or "argv"...
java MyApp arg1 arg2
Each string in the array is called a command-line argument
Rajib Ban wrote:So in case of Does args mean "arg+s", i.e., argument string or a single argument of the form of an array of elements of type String? Or is it a single
argument "args": an array of elements of type string[]?
Rajib Ban wrote:The array of elements is supposed to be a,r,g,1,' ',a,r,g,2,'\0'. So are the strings supposed to be "arg1" and "arg2"?, because to me the array {a,r,g,1,' ',a,r,g,2,'\0'} appears to be a single string!
Rajib Ban wrote:I find that learning programming in the way the college/university education system wants us to learn programming is essentially cultivating the hard way of keeping our distrust/disbelief suspended. Because while learning C I found that there are many things in C that can't be understood, but only memorised, keeping our disbelief/distrust suspended and continue with degree seeking education, unless one learns machine language, assembly language and Opt Codes! But they are taught chronologically later in computer courses!
Jesper de Jong wrote:
Rajib Ban wrote:The array of elements is supposed to be a,r,g,1,' ',a,r,g,2,'\0'. So are the strings supposed to be "arg1" and "arg2"?, because to me the array {a,r,g,1,' ',a,r,g,2,'\0'} appears to be a single string!
I don't know where you got this a,r,g,1,' ',a,r,g,2,'\0' from but it has nothing to do with Java. Don't confuse whatever you learned about C or C++ with Java. Java is a different language, and not an extension of C++ - so it is not so that whatever exists in C++ also exists and works in the same way in Java. If you start your program with: java MyApp arg1 arg2 then the args variable of the main method will contain two String objects, with the values "arg1" and "arg2".
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
This array is the mechanism through which the runtime system passes information to your application. For example:
java MyApp arg1 arg2
Each string in the array is called a command-line argument.
Please demarcate what exactly each string is, and which one is the array:Dave Tolls wrote:What don't you understand about that line?
For example:
java MyApp arg1 arg2
Each string in the array is called a command-line argument.
I think it is one of those things which vary from person to person. Look at Winston's confessions about how difficult he found it to move from procedural to object languages. You were obviously different. I think the problem is that the similarity of the syntax misleads people into thinking there is a similarity in semantics and paradigm, which there isn't.Bear Bibeault wrote:. . . I'm not sure I agree that C is detrimental to learning Java. But then, I was striving to write object-oriented C before I even knew what object orientation was, so I may be an outlier.
Stephan van Hulst wrote:When you run ...
Campbell Ritchie wrote: ... If there is a risk of confusion, it is probably better to avoid that risk ...
Rajib Ban wrote:
No, Sir, what I was asking was with respect to that specific quote:
Please demarcate in this example:
Rajib Ban wrote:
Stephan van Hulst wrote:When you run ...
No, Sir, what I was asking was with respect to that specific quote: ...
Rajib Ban wrote:
Yes, Sir! That is why I said I needed a book that will deal with exclusively Java from the first principles without allusion to C programming language.
Dave Tolls wrote:The Oracle tutorials, which I think have been linked before, don't mention C, at least not that I remember.
main method is similar to the main function in C and C++
in this example:
java MyApp arg1 arg2
Each string in the array is called a command-line argument.
Rajib Ban wrote:I wanted to be pointed out, exactly with respect to that specific quote, what exactly "Each string" is, and which one is "the array":
This thread may please be closed, as further discussions are in my next post: I found a solution to my earlier thread mentioned herein
Thanks for helping.
Regards!
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Campbell Ritchie wrote: I think the problem is that the similarity of the syntax misleads people into thinking there is a similarity in semantics and paradigm, which there isn't.
Consider Paul's rocket mass heater. |