main() method in java complete explanation- Part 2

Published: 01 May 2024
on channel: Learn Java
184
3

You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.

Q8) In java applications, if we declare main() method without the static keyword than what will be the consequences?
Example:-
public void main(String[] args)
{
System.out.println();
}
Compilation:-
===========
There will be no compile time errors becuase the compiler will treat the given method as an ordinary method and it will look only for the syntax error. It would not check whether if the programmer has defined the main method or not.
Execution:-
=========
During the execution time, there will be an error. Becuase at the time of execution, the main thread will be created by the JVM and the main thread will try to get access to the main method with the specified prototype. However, the specified prototype is missing. So that it will throw an error as follows.
Error: Main method is not static in class Test, please define the main method as:
public static void main(String[] args)

Q9) What is the requirement to provide "void" as return type in the main() method?
Example :-
========
class Test
{
public static void main(String[] args)
{
int a=100;//
if(a==100)//true
{
System.out.println("Forceful exit");//Forceful Exit
return;//The main will get terminated here//The thread is getting deactived //here
}
System.out.println("main method()");
}
}
Please be informed that in Java, there is a convention like to start the application logic at the starting point of the main method and to end the application logic at the ending point of the main method. Because, the JVM always creates the main thread which is responsible to execute all the instructions inside the main method, and after executing the instructions at the termination point, the main thread gets deactivated and the JVM will get shut down. So , it is not required to get any return value and that's why the return type is always void. It will not expect any value at the termination stage.

Q10) In java applications, if we provide main() without "void" as the return type what would be the consequences?
Example:-
=========
class Test
{
public static int main(String[] args)
{
System.out.println("main method()");
return 100;
}
}
Compilation :-
===========
There will be no compile time errors becuase the compiler will treat the given method as an ordinary method and it will look only for the syntax error. It would not check whether if the programmer has defined the main method or not.
Execution:-
==========
During the execution time, there will be an error. Because at the time of execution, the main thread will be created by the JVM and the main thread will try to get access to the main method with the specified prototype. However, the specified prototype is missing. So that it will throw an error as follows.
Error: Main method must return a value of type void in class Test, please
define the main method as:
public static void main(String[] args)

Q11) Why the name of the main() method is main?
The name must be meaning full as well as powerful so the name of the main() method is defined as main by the java people.

Q12) what is the requirement to provide parameters to the main() method?
public static void main(String[] args)
{
System.out.println();
}
Input data in the java application is categorized into 3 types.
1. static input
2. Dynamic input
3. command line input
1. static input:-
=================
If the programmer provides the data at the time of writing the program, it is called as static input.
class Test
{
public static void main(String[] args)
{
int a=100;//static data
int b=200;//static data
System.out.println(a+b);
}
}
2. Dynamic input
================
If the programmer provides the data at the time of executing program or during the run time, it is called as dynamic input.

import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number ");
int a=sc.nextInt();
System.out.println("Enter the Second number ");
int b=sc.nextInt();
System.out.println("The addition of 2 numbers are "+(a+b));
}
}

3. command line input:-
===================
Please be informed that if we provide input data along with the "java" Command in the command prompt than that input will be treated as "Command Line Input".
class Test
{
public static void main(String[] args)
{
System.out.println(args.length);
for(int i=0;i is lesser than args.length;i++)
{
System.out.println(args[i]);//0 1 2 3 4
}
}
}

//D:\Java3 java Test 100 200 300


On this page of the site you can watch the video online main() method in java complete explanation- Part 2 with a duration of hours minute second in good quality, which was uploaded by the user Learn Java 01 May 2024, share the link with friends and acquaintances, this video has already been watched 184 times on youtube and it was liked by 3 viewers. Enjoy your viewing!