public static void main(String[] args)
credits to science tech easy

public static void main(String[] args)

psvm in Java

psvm in Java stands for public static void main(String[] args).

It is the most important Java method. When we start learning Java(or any other coding language),the first function(method in Java) we encounter is the main() method. It is the point from where the program starts its execution.

public

The first word ,i.e. p is public which is the access modifier of the main method. The “public” keyword means that any object can use the main method. We can access the class from anywhere. The main() has to be public as, if it is non-public, then it’s not allowed to be executed by any program.


Let’s see what happens if we don’t define main() as public.

No alt text provided for this image



static

The second word, i.e. s is static which allows main to be called without having to instantiate any object. When java runtime starts, there is no object of the class present. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. We could also write static public instead of public static, it would work the same, but public static is the standard way.


Let’s see what happens when we don’t use static in java main() method.

No alt text provided for this image


void

The third word, i.e. v is void which is the return type of the main method. The return type “void” specify that method doesn’t have a return value. This is done because once the main() terminates, java program also terminates. There is nothing that can be done for the returned object by JVM. So there is no point in returning anything.


main

The fourth word, i.e. m is main which specifies that it is the entry point of the program. It’s fixed and when we start a java program, it is the main() that the JVM looks for as the starting point of the java program.?Remember, it is an identifier, not a keyword.


String[] args

The java main() accepts a single argument of array of type?java.lang.String?class. This is also called as java command line arguments. Everything in the psvm statement is fixed except this part. You can change the name of String array argument, for example you can change?args?to?myStringArgs. String array argument can also be written as?String... args?or?String args[].


Behind the Scene

When the java program starts executing, the java.exe is called. The java.exe makes JNI(Java Native Interface) calls, and this load JVM. The java.exe parse the command line, generates a new string array, and invokes the main() method. A daemon thread is attached to the main method, and this thread gets destroyed only when the Java program stops execution.


How can C/C++ program return int , but not Java?

While coding in C/C++, we may or may not return value. The C and C++ programs which return int from main are processes of Operating System. The value returned from main is the exit code or exit status, which mainly specifies why the program was terminated. Exit code 0 means successful termination while non-zero exit code indicates an error.

However, the Java program runs as a ‘main thread’ in JVM. The Java program is not even a process of Operating System directly. There is no direct interaction between the Java program and Operating System. There is no direct allocation of resources to the Java program directly, or the Java program does not occupy any place in the process table. Then whom should it return the value to?

This is the reason why the main method of Java is designed not to return an int or exit status.

要查看或添加评论,请登录

Matam Kirankumar的更多文章

  • Multi-tenant Architecture

    Multi-tenant Architecture

    Building a multi-tenant application has become an increasingly important concept in today’s application development…

    1 条评论
  • Lets Explore ConcurrentSkipListMap

    Lets Explore ConcurrentSkipListMap

    A ConcurrentSkipListMap is a part of the Java Collections Framework and is available in the java.util.

  • Lets understand Go 1.21 new release

    Lets understand Go 1.21 new release

    The Go programming language has released its first Release Candidate (RC) for version 1.21, which is packed with new…

  • Functional index in database

    Functional index in database

    ?? Database Tip Most developers are puzzled that indexes are not used for e.g.

  • Mastering Sprint Planning: Key Steps for Successful Agile Development

    Mastering Sprint Planning: Key Steps for Successful Agile Development

    Introduction Sprint planning is a meeting that takes place at the beginning of a sprint in the Agile software…

  • REST API Naming Conventions and Best Practices

    REST API Naming Conventions and Best Practices

    Lets us know the best practices/Standards in REST API Full form of REST API is Representational State Transfer…

    5 条评论
  • Azure Cloud Shell : Permission denied (public key)

    Azure Cloud Shell : Permission denied (public key)

    generate new public key ssh-keygen writing key to file Generating public/private rsa key pair Enter file in which to…

  • Java 19 arriving!

    Java 19 arriving!

    Good news for Java Developer’s Community . Java 19 is Arriving in Sep-2022.

  • Collection vs Collections in Java.

    Collection vs Collections in Java.

    Differentiate between Collection and collections in the context of Java. Collection : In the java.

  • Constructor Injection vs Field Injection

    Constructor Injection vs Field Injection

    Repeat after me: field injection is a dumb idea Let’s discuss this stuff with a bit of (quite generic) context: we want…

    2 条评论

社区洞察

其他会员也浏览了