Thursday, January 17, 2019

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

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



Related Posts:

  • Anagram program in java package seleniumrepo; import java.util.Arrays; /*  * Check whether two strings are anagram of each other  * Ex: LISTEN <=> SILENT, TRIANGLE <=> INTEGRAL, HEART <=> EARTH  */ public cl… Read More
  • Prime number a. prime or not Prime number: A prime number is a whole number greater than 1 whose only factors are 1 and itself. Ans: --- public class PrimeTest { public static void main(String[] args) { int n, i, k=0… Read More
  • Lambda Expressions in Java Lambda Expressions: It is a anonymous(nameless) function. It does not has method name, method return type nor modifiers            General Method:            … Read More
  • Functional Interface in java Functional Interface: is an interface it contains only one abstract method and declares with annotation @FunctionalInterface E.g:                         @Func… Read More
  • Extract numeric value from a String                    package seleniumrepo;                           public class ExtractNumerics{  … Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts