0
Why do I write "String" instead of "int" after "public static void" even if I just add some integer in a code?
Public class confused { Public static void (String[] args) { /*This "String" here*/ int x = 5; int y = 6; system.out.println(x + y); } }
4 ответов
+ 3
for eg:
public static void doAdd(int a, int b){
int c= a+b;
//or do int c=3+5;
}
In above code^^^ a and b are called arguments ..when we call the method we pass the value of a & b.
Similarly, Java code starts running from
main(string[] args)
here string[] args... indicates that argument or input of method is "array of strings " (int[] will indicate array of int) and variable name of argument is args...
So now, If (only if not compulsion) you can use that variable like
String fullName = args[0]+ args[1];
It doesn't matter if you don't use it... to use or not use is totally up to you
I hope it's clear ...
+ 2
ADDED TO @VEdward's answer.Here is a clear example, Consider a method named add, used to add 2 integers,
public static void add(int a , int b){
System.out.print(a+b);}
Hence when you call this method using,
add(5,7);
output is 12 ,printed on console.THIS is only possible if I use integer parameters (or arguments) NOTE:- 5and 7 are parameters.
if I add doubles,
add(3.4,5.5);
this would show error, as there is no method named add that takes double parametes.
+ 1
Forgot "main". String is for parameter, you can run your programme with.(eg java myjar.jar -bla bla)It is just the way, how software works and Java is a language, that forces you to write your code more strict formatted as it would be the case with js/c/c++.
0
Thanks. This helped a lot :)