public static void main(String[] args){} or public static void main(String... args){}:
Java main method is the entry point of any java program. Its syntax is always "public static void main(String[] args)". You can only change the name of String array argument.
public: This is an access modifier means this method can be accessed anywhere.
static: The main() method can be accessed directly with the class name without instantiation. When java runtime starts, there is no object of the class present initially. That’s why the main method has to be static so that JVM can load the class into memory and call the main method.
void: tells the compiler main doesn't return any value.
main: method name. It’s fixed and when we start a java program, JVM looks for the main method.
String[] args: method accepts an argument of type String array. This also called command line arguments. Argument name can be anything instead of args.
We can also specify parameter as "String... args".
If we tried to change parameter type as "Integer[]" then JVM doesn't allow you to run a java application in eclipse.
Example: Class name: TestClass
Passing Command Line Arguments in Eclipse:
It is another style of giving values to the program at runtime. Here arguments are passed at running time (not readily at compile time); the traditional way is through method calls and taking input from keyboard
Method 1: Right on class name -> Run As -> Run Configurations...
Click on Arguments Tab, enter the text in Program Arguments section as below:
The output will appear as below for the above program:
The value of args[0] is: I
The value of args[1] is: love
The value of args[2] is: my
The value of args[3] is: India
Java main method is the entry point of any java program. Its syntax is always "public static void main(String[] args)". You can only change the name of String array argument.
public: This is an access modifier means this method can be accessed anywhere.
static: The main() method can be accessed directly with the class name without instantiation. When java runtime starts, there is no object of the class present initially. That’s why the main method has to be static so that JVM can load the class into memory and call the main method.
void: tells the compiler main doesn't return any value.
main: method name. It’s fixed and when we start a java program, JVM looks for the main method.
String[] args: method accepts an argument of type String array. This also called command line arguments. Argument name can be anything instead of args.
We can also specify parameter as "String... args".
If we tried to change parameter type as "Integer[]" then JVM doesn't allow you to run a java application in eclipse.
Example: Class name: TestClass
Passing Command Line Arguments in Eclipse:
It is another style of giving values to the program at runtime. Here arguments are passed at running time (not readily at compile time); the traditional way is through method calls and taking input from keyboard
Method 1: Right on class name -> Run As -> Run Configurations...
Click on Arguments Tab, enter the text in Program Arguments section as below:
The output will appear as below for the above program:
The value of args[0] is: I
The value of args[1] is: love
The value of args[2] is: my
The value of args[3] is: India
Method 2: Create an another class TestClass2 and call main of TestClass1 in TestClass2 by passing a String type array as argument value. See the below code:
Run TestClass2, Output get as below:
The value of args[0] is: Apple
The value of args[1] is: Banana
The value of args[2] is: Orange
The value of args[3] is: Grapes
0 comments:
Post a Comment