Q1. Class, Object, Method
Q2. Key points to remember
Q3. public static void main(String args[])
Q4. Running java program
Q5. why java/main() is designed to not return any value?
Class, Object, Method
- class:-blueprint that describes the behaviour/state of objects that it supports syntax: class {...} example: class MyClass{...}
- object:- it is a real-time entity such as dog has colour, breed, a number of legs etc. object is an instance of a class.
- method:- it's a behaviour or it's a block of code designed to perform a particular task. One class can contain multiple methods.
Key points to remember
- case sensitive:- Java is case sensitive i.e. mail and Mail are different.
- Java class name's first letter should be capital. the program can run if the class name is in small letters, but this contention was created to make code more readable. For example class EmployeePolicies()
- method name should start with a lower case letter. for example employee and()
- name of the program should match the class name.
- public static void main(String args[]):- Java processing starts from the main(). It's not mandatory to have the main method in java program, instead you can use static block. The static block gets executed even before the main method() i.e. when class is loaded before the main(). However, JDK7,8 and so one main method is mandatory.
public static void main(String args[])
class HelloPrint
{
public static void main(String args[])
{
System.out.println("Hello");
}//main method end
}//class HelloPrint end
point where the program starts its execution or entry point
public:-
-it is one of the access specifiers
-specifies who can access the method and from where.
-public makes it globally available
-it is made public so that JVM can invoke it from outside the class as it itself is present outside too.
-making the main method private throws an error which is:
Error: Main method not found in class, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Applicationstatic:-
-used so that compiler can call it without the creation of an object or before the creation of an object of the class.
-removing static will affect the JVM such that it won't be able to spot the main() because there is no object of that class is present.
Error: The main method is not static in-class test, please define the main method as:
public static void main(String[] args)void:-
-specifies that method does not return anything.
-that is because as soon as the main() terminates, the program terminates too. Hence it is senseless to return anything from main() and JVM can't do anything with the return value.
-possibilities:
a. Error: Main method not found in class, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
b. changing the data type lets say to int, will throw an error on the line of main() method's end i.e. "}"
c. return 0 and reset void to int will throw an error:
Error: Main method must return a value of type void in class GeeksforGeeks, please
define the main method as:
public static void main(String[] args)main():-
-it is an identifier that informs JVM of the starting point. It's not a keyword though.
-removing or renaming the main() will throw an error:
Error: Main method not found in class, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.ApplicationString[] args:-
-it can also be written as String args[] or String... args
-used to store command-line arguments
-it is an array of type java.lang.String class.
-here String has 'S' capital because all predefined classes name starts with capital letters.
-array name can be anything as here it is args[].
-Here String array is args which can be user-defined too.System:-
-predefined final class defined in java.lang packageout:-
-instance of PrintStream type
-it is public, static member/variable of the System class-
println():-
-defined in java.io package
-1. used to print an argument passed to it- add a new line to the output
Running java program
step 1: compile
-javac .java
-it will create a class file with a class name which contain the main method
step 2: execute
-java
-here class name is the one which contains main method OR output of the above step
why java/main() is designed to not return any value?
-let's compare C++ and java regarding returning a value.
- C++ interact directly with the OS. And the value return from C++ is an exit status which defined whether the closing is successful or contains any error i.e. reason of termination. For example, 0 is a successful termination and 1 is a Miscellaneous error.
- Why it is mandatory to return exit status in C++? -well, in C++ child processes sends exit status to their parent processes to let the parent process frees up the resources allocated to it(child process). -since java programs run as the main thread in JVM, there is no direct interaction between Java program and OS. Thus there is no direct resource allocation present. Resulting there is no one to receive an exit status. That is why java is designed to not return an int or exit status. NOTE-JVM is a part of OS, thus it can e terminated using certain exit status, for example,
- System.exit(int status)
- java.lang.Runtime.exit(int status)
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.