Java main() Method - public static void
main(String[] args)
Java’s main() method is the entry point of every Java
program, where the JVM begins execution. If the main()
method is missing or its signature is incorrect, the
program will compile but won’t run.
The JVM looks specifically for a properly defined
main() method to start execution.
The [Link] launcher initializes the JVM using JNI
and invokes the main() method.
The main thread created by the JVM is always a non-
daemon thread.
Syntax of main() Method
Syntax of the main() method is always written as:
Syntax:
Example: The most common method to define
the main() method is shown in the example below.
class Geeks
{
public static void main(String[] args)
{
[Link]("I am a Geek");
}
}
Output
I am a Geek
Every word in the public static void main statement
has a meaning in
the JVM that is described below:
1. Public
It is an Access modifier, which specifies from where
and who can access the method. Making the main()
method public makes it globally available. It is made
public so that JVM can invoke it from outside the class
as it is not present in the current class.
If the main method is not public, it's access is restricted.
Example:
// Java Program to demonstrate the
// use of any other access modifier
// other than public
class Geeks
{
private static void main(String[] args)
{
[Link]("I am a Geek");
}
}
Output:
2. Static
It is a keyword that is when associated with a method,
making it a class-related method. The main() method
is static so that JVM can invoke it without instantiating
the class. This also saves the unnecessary wastage of
memory which would have been used by the object
declared only for calling the main() method by the JVM.
If you try to run Java code where main is not static, you
will get an error.
Example:
class Geeks
{
public void main(String[] args)
{
[Link]("I am a Geek");
}
}
Output:
3. Void
It is a keyword and is used to specify that a method
does not return anything. As the main() method does
not return anything, its return type is void. As soon as
the main() method terminates, the Java program
terminates too. Hence, it doesn't make any sense to
return from the main() method as JVM can't do anything
with its return value of it.
If main method is not void, we will get an error.
Example:
class Geeks
{
public static int main(String[] args)
{
[Link]("I am a Geek");
return 1;
}
}
Output:
4. main
It is the name of the Java main method. It is
the identifier that the JVM looks for as the starting
point of the Java program. It's not a keyword.
If we change the name while initiating main method, we
will get an error.
class Geeks
{
public static void newmain(String[] args)
{
[Link]("I am a Geek");
}
}
Output:
5. String[] args
It stores Java command-line arguments and is an
array of type [Link] class. Here, the name of
the String array is args but it is not fixed and the user
can use any name in place of it.
Example: Execution Process of String[]
class Geeks
{
public static void main(String[] args)
{
for (String elem : args)
[Link](elem);
}
}
1. Data Types in Java
A data type specifies the type of data that a variable can store. Java is a strongly typed
language, which means every variable must have a declared data type.
Output:
Apart from the above-mentioned signature of main, you
could use public static void main(String
args[]) or public static void main(String... args) to
call the main function in Java. The main method is
called if its formal parameter matches that of an array of
Strings.
Types of Data Types
A) Primitive Data Types
Primitive data types are built into Java and store simple
values.
Data
Size Example Description
Type
byte 1 byte byte age = 25; Small integer
short 2 bytes short num = 1000; Short integer
int 4 bytes int marks = 90; Integer
long population =
long 8 bytes Large integer
9000000L;
float 4 bytes float price = 99.5f; Decimal number
High-precision
double 8 bytes double pi = 3.14159;
decimal
char 2 bytes char grade = 'A'; Single character
1 bit
boolean boolean pass = true; True or False
(logical)