As usual the first program in java is also the program which shows a greeting message to the user. Here is the first program in java
class FirstProgram
{
public static void main(String args[])
{
System.out.println ("HELLO PRABEESH");
}
}
Output:
Everything in java is concern with objects and classes. Here also a class is present with name FirstProgram
class FirstProgram
{
}
A class name must be a valid java identifier. It is a good programming practice to start every java class with a capital letter and if more than one word present in the class name then separates them with capital letters. You can see here also the class name contains two words First And Program. So here i indicates the class name as FirstProgram.
Syntax for declaring a class in java is given bellow.
Access_specifier class Class_name
{
class_variables
member functions
}
Like other languages, java also needs a function called the main() method or function for starting the execution of the code. Here is the syntax of main() method contain in a java program.
public static void main(String args[])
{
Statements;
}
class FirstProgram
{
public static void main(String args[])
{
System.out.println ("HELLO PRABEESH");
}
}
Output:
Explaining the First Program:
Everything in java is concern with objects and classes. Here also a class is present with name FirstProgramclass FirstProgram
{
}
A class name must be a valid java identifier. It is a good programming practice to start every java class with a capital letter and if more than one word present in the class name then separates them with capital letters. You can see here also the class name contains two words First And Program. So here i indicates the class name as FirstProgram.
Syntax for declaring a class in java is given bellow.
Access_specifier class Class_name
{
class_variables
member functions
}
Like other languages, java also needs a function called the main() method or function for starting the execution of the code. Here is the syntax of main() method contain in a java program.
public static void main(String args[])
{
Statements;
}
Here is the detailed explanation of the main method in a java program.
public: main is public because it is needed to access the main function out side of the class.
static: All we know that for accessing any of the class variables or function we need an object of that class. main is also a member function of the class. So JVM needs a class object to invoke the main method, for that the java compiler creates a default object of that class at the time of compilation. Only the static functions are possible to access without an explicit object of a class. That'a why the main function is static one. JVM invokes the main method by using the default object of the class.
void : The keyword void indicates that the return type of the function is void.
String args[] : This string array is called command line arguments in java. The command line arguments are used for supply input values to a java program before the execution of the program.
It is note that every java program must be saved with a file with extension .java
CLICK HERE TO WATCH THE VIDEO TUTORIAL OF THIS TOPIC
Previous Index Next
CLICK HERE TO WATCH THE VIDEO TUTORIAL OF THIS TOPIC
Previous Index Next
Comments
Post a Comment