java - What is the purpose of void? -
public class test { public static void main(string[] args) { int x = 5; int y = 10; multiply(x,y); } public static void multiply(int x, int y) { int z = x*y; system.out.println(z); } }
i new programming , confused on few things.
why correct use void? thought void used in order specify nothing returned but, multiply method returns z.
do programs require have "public static void main(string[] args)"? purpose of main method , parameters "string[] args" mean? program work if main method removed?
thank you!
first, multiply method not return anything; prints product, not return value.
public static void multiply(int x, int y) { int z = x*y; system.out.println(z); //may return, side-effect of function. } //there no return inside block
secondly, public static void main
provides entry point program. without it, cannot run program. refer java documentation more information on usage of public static void main
.
the string[] args
here means captures command line arguments , stores array of strings (refer same link posted above, in same section). array called args
inside main
method (or whatever else call it. oracle cites argv
alternate name)
system.out.print
tells program print console, while return
result of method. example, if added print
on method debug (a common practice), printing things while program runs, not affect program returns, or result of program.
imagine math problem - every step of way "print"ing work out onto paper, result - "answer" - return.
Comments
Post a Comment