how many types of inputting method in java?can you explain with examples?
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.
how many types of inputting method in java?can you explain with examples?
what do you mean by
public static void main(string[] args)
{
}
The questions you're asking aren't really how this works. In fact, I'm going to merge them.
The answer to both is: what do you think? What have you tried? What did google tell you?
Please see the link in my signature on asking questions the smart way before you post again.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
The tutorial is a good place to look for how to do things.
For example for methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
If you don't understand my answer, don't ignore it, ask a question.
okk i try it..
I've also stared at the ugly (forgive me Java heads) snippet of code that pops up in every single code example:
Although I've started reading the tutorials over at the Oracle site, skipping all of the exercises and what-not, I'm still not confident I understand what that set of code does. I of course realize that it all will make perfect sense once I understand Java at some level. The fact that no matter what I need to code my program will always start with this... thing, is just annoying.public static void main(string[] args) {}
This is my understanding of the line of code thus far:
This is the meat and potatoes of the expression, as this defines the "main" method of a class. Apparently all "classes" have main methos, that's just how the language works.main()
The "body" of the method goes within these brackets.{}
Both of these elements are actually concatenated, as the latter names the (internal) variable containing the arguments (or parameters) that the main method takes as input. The former defines that variable as being an "array" comprising of "string type" values. I'm guessing any number of values, even if I also read that data structures always need to be pre-defined. Dunno what's gives.string[] args
This keyword means that the main method will not "return" any value once called. Apparently main methods never do, albeit other methods of the same class might.void
These two are basically needed in order not make the main method be "private" or "dynamic". Because that would be bad and thus not compute. This is where I lack any deeper understanding.public static
Now, how wrong am I?
Last edited by Baldyr; June 30th, 2014 at 12:38 PM.
First of all, it would be good if you started your own thread instead of hijacking the thread of other people. Thats really not the nice way.
But I will try to help you out as good as I can.
Nope.
A main method is not needed at all, you can have no main method in your entire program.
But usually its still in use because it defines an entry-point into your application.
When you application is started it will usually start at a main method.
Thats correct.
The body of every method goes within such brackets.
This is the parameter list for the method. A parameter list is a list of touples divided by commata.
Each touple consists of a type and a name for a parameter. In this case the method only has a single parameter, its name is args and its type is an array of String objects.
You can have as many parameters for a method as you like, even 0. But you have to define a type and a name for each one of them.
By the way, parameter is what you define in the header of your method. But when you actually pass a value in as a parameter its called an argument.
Correct.
Main methods never return anything because they are the entry-point into your program. When the main method is over your program will usually be exited. (unless you create other threads that is)
The first one is visibility, there are 4 types of visibility in java:
public - the method can be assessed from anywhere and anybody
private - the method can only be assessed from within the class itself (or inner / annonymous classes).
protected - the method can be assessed from within this class and all sub-classes.
package private - the method can be assessed from anywhere within the same package. To declare a method package private just dont write anything in front of it.
The static means that this method does not belong to an object but to an entire class. The main method must be static because before your program is started there will not be any objects.
For further information you should really read some tutorials or the java specification.
Ada Lovelace (June 30th, 2014), Baldyr (June 30th, 2014)
I did not realize that was in fact what I was doing. Since no one else was about to answer the original poster I though I would give it a try. I succeeded in making you, good sir, clarify things for us newbies. So, thank you and sorry for the inconvenience.
Well, that's interesting. I wonder why tutorials start with making newbies write that main method anyways, like it really was the one and only way to do it. I'm guessing this is what also made the original poster scratch his or her head. At least we know this much now!
So... you really don't need to put in that definition for the args tuple then, if you don't have any parameters? Seems like waste of code, but perhaps it's usually included for some clarity?
Ah, I thought that parameters are just what arguments are called with methods (in contrast with mere functions). I stand corrected, then.
Since I struggle to grasp this thing - could you give an (unusual) example of where you would want a main method to return some value? Could a program, for example, return a true/false boolean if it managed to complete whatever task it set out to do?
Since this redundancy of code bothers me, wouldn't it be more convenient if "public" was the "default" type of method, and not "package private"? But I'm guessing it would be even more unconvenient to put those bits into every class method then...
But if there can be no exceptions to this, why not make the "static" state of a main method implied? Sure, it would be good form to put it there for clarity, but the code would still compute the same, since it's a main method and all...
As I said, I'm currently reading the tutorials and aim to read some more. This is just where I'm at right now. Thank you for your time and effort in helping a newbie out.
For me learning a language isn't merely to accept the way things work, but to understand why they work that way and not some other way. If I know what things are, what they do and why I'm putting them into my code - then I can feel like I can think the language, at some level. (That feeling when you realize that you were dreaming in code...)
Take a bow Cornix That explaination was better than books and lectures. Seriously, really good dude
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
Often it's because there was an arbitrary decision made by the programmer. Somewhere back in history a programmer decided that the method to start the execution of a program should be named main.why they work that way
Currently the java program looks for a public, static, void method named main that takes a String array as the place to start the execution of a class when you enter: java TheClassName
static makes sense because there isn't an instance of the class yet.
The String array is a way to pass args to the class when it starts:
java TheClassName the args here
would set the String array passed to main() to {"the", "args", here"}
If you don't understand my answer, don't ignore it, ask a question.
Probably because otherwise you would hardly be able to test your code.
There are ways to start a java program without a main method, but its not the usual way. The main method is the default entry-point and I guess that is the reason why newbies are teached to start with a main method.
If you want to use your main method as the entry-point you need to. The arguments that are passed to the method will be the command line input. All java programs are started from a command line, sometimes the command line is hidden by an executable or something else, but its still there. The args array will contain each command line option as an element.
If you leave this parameter out the main method can no longer be used as an entry-point because the command line options could not be passed anymore.
There are no "functions" in java. You write methods, these take parameters but get arguments passed to them.
You can write any kind of method you like and name it main, that is not forbidden. But then you cant use it as an entry-point to your program anymore. Once your main method returns something it is not a "real" main method anymore but instead just a regular method that just happens to be called "main".
You can also not return anything since java programs are always command line programs. You can write to the output stream though; this is usually done via System.out.println(...).
This is left over from the very early start of java. The java gurus would probably want to change it too, but that would brake backwards compatibility which is holy to java. So once a decision was made it will never be undone.
Because you can write a main method which is not static, but then it will not be an entry-point for your program anymore but a simple method which just happens to be called "main".
I follow. Your example would require the main method to take three parameters, and only these parameters, right? So the main method wouldn't accept two or four or none parameters, then.
But if my program (-s main method) doesn't require or accept any command line options - what then? Do I still need to define a tuple of strings in the constructor? (I realize that the args name as such is arbitrary though.)
So this makes Java a OOP language only then. I was kinda wondering about this.
That sorta makes a lot of sense, because you of course mean that once the program exits back to the command prompt, there isn't any process left to pick up that return value. Check.
The only thing left to do is to accept this fact. Thank you, again.
First of all, you have no control over how many command line arguments your program gets passed on start up. The user can type in anything he/she wants.
Thats why the parameter args is an Array of Strings. This array could contain any number of elements, even 0 if no command line arguments have been inputted by the user.
The main method does always look the same way if you want to use it as the entry-point, you never change the number of parameters or their types.
Imagine a main method like this:
This method would simply write back every argument that was passed to it.class Foo { public static void main(String[] args) { for (String s : args) { System.out.println(s); } } }
Now this program could be started from the command line like this:
And the output would be:java Foo a b c d
a b c d
But if you start the program like this:
The output is just empty, because the array args contains no elements.java Foo
Last edited by Cornix; June 30th, 2014 at 03:14 PM.
Baldyr (June 30th, 2014)
BTW .java looks like a source file extension.java Foo.java
If the Foo class is not in a package, the command line would be:
java Foo
If you don't understand my answer, don't ignore it, ask a question.
No.Your example would require the main method to take three parameters,
String[] args defines args as a variable that can refer/point to ANY sized single array.
For example:On each line args is assigned a reference to a different sized array.String[] args = {"a", "B"}; args = new String[4]; args = new String[] {"a"};
If you don't understand my answer, don't ignore it, ask a question.
Baldyr (June 30th, 2014)
Oh, now I see. The length of the args tuple is set by the actual number of parameters passed, not by the main method definition. Understanding how these things work makes it easier for me to understand what it is I'm doing, instead of simply copy-pasting some main method definition template because I've come to accept that this is the only way to do it.
For anyone who might care, I pretty much read the tutorials on this site in one sitting and am finally starting to make sense of things. Particularly why Java works the way it does, which was the thing I was trying to grasp. I'd recommend the tutorial for anyone getting into Java at entry-level!
The next step would probably be to install JDK and JEdit and start coding and getting used to the syntax and such, then...