Public
class MyFirstCode {
publicstaticvoid
main(String[] args) {
System.out.println("Hello
World");
}
}
|
Save as
|
MyFirstCode.java
|
Compile (on
cmd prompt)
|
javac MyFirstCode.java
|
Run (on cmd
prompt)
|
java MyFirstCode
|
Do you know
|
It is
convention that file name should be same as name of class having main()
method. But file can be saved by other name if class is not public. For
Example,
|
class
MyFirstCode {
publicstaticvoid
main(String[] args) {
System.out.println("Hello
World");
}
}
|
Save as
|
SecondClass.java
|
Compile (on
cmd prompt)
|
javac SecondClass.java
|
Run (on cmd
prompt)
|
java MyFirstCode
|
Program description:
·
Class : This
keyword is used to declare class.
·
Public :
This is one of the access specifier which means visible to all.
· Static :
Keep in mind, wherever static keyword comes that method or variable does not
belong to any object. It has class level scope.
·
Void : Void
is return type of method. This means it returns nothing.
·
String[] args :
This is for storing command line arguments. Instead of String args[] one can
use var-args as main(String… args).
·
System.out.println() : This is for printing text on console on new line.
Do you
know : System.out.println()
|
·
System is
a class in the java.lang package.
·
out is
a static member of the System class, and is an
instance of java.io.PrintStream.
·
println() is
a method of java.io.PrintStream. This method is overloaded
to print message to output destination, which is typically a console or file.
|

No comments:
Post a Comment