I will write down the simple java program first and will explain each and every line of this program followed by it.
First Simple Java Program
/*
This is a simple Java Program.
Call this file 'Example.java'
*/
class Example
{
// Your program begins with a call to main() method.
public static void main(String args[])
{
System.out.println("This is a simple java program");
}
}
Output of the above program is: This is a simple java program
Step by Step explanation of the above program:
The program beings with the following lines. /* This is a simple Java Program. Call this file 'Example.java' */ This is a multiline comment which starts with /* and ends with */. All the lines between /* and */ will be treated as comments and wont be executed by JVM. This syntax is mainly used if there are more than one lines of comments. |
class Example { } We can clearly understand from the code that we have created a class named Example. In Java, all the code must reside inside a class by writing the code inside the braces i.e. { and } of the class. Note - The name of the class should match the name of the file that holds this program. In this example, the name of the class is Example , so while saving this program having the class Name as Example, we've to save the program file as Example.java. If we save the program file with a different name then your program wont execute. And also observe that if the class name is Example and you are going to save the program file as example.java, the program wont run as the class name and program file name wont match in case. |
// Your program begins with a call to main() method This is a single line comment that starts with // and ends at the end of the line. JVM wont execute the single line comments. |
public static void main(String args[]) { } This is the line at which the program will begin execution. All the Java applications will begin execution by calling main() method. There can be more than one method inside a class but the program execution starts from main() method only. The main method may further call the other methods in the class for execution. Also we've to understand the usage of public, static, void and String args[] terms in this simple program.
All the code related to the main() method should be written inside the braces { and } of the main( ) method. The same applies to the other normal methods.
|
System.out.println("This is a simple Java program."); This line of code is written inside the main( ) method in the above simple program. This line prints This is a simple Java program. output on execution. We've to understand the 'println( )', 'System' and 'out' terms to understand how this line prints the This is a simple Java program text as output.
Also you may notice that the above line ends with a semicolon ; . All the statements in Java end with a semicolon. All the other lines in the above simple program which don't end with the semicolon are not statements. |
Go through my previous post#34 Write and execute the Java programs using the TextPad Tool and execute this simple java program explained here and find out whether This is a simple Java program. text is printed as output.
Please comment below to feedback or ask questions.
I will explain another simple Java program in the next post.
I will explain another simple Java program in the next post.
4 comments:
Hi Arun,
Am just started with learning java and I am using the textpad editor to build and run small java programs.
Program was compiled successfully but when i am running the program it gives the below error.
"Error: Main method not found in class Room, please define the main method
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
Below is my program:
====================
class Room
{
float length;
float width;
void getdata(float a, float b)
{
length = a;
width = b;
}
}
class RoomArea
{
public static void main (String args[])
{
float area;
Room Room1 = new Room ();
Room1.getdata(200,100);
area = Room1.length * Room1.width;
System.out.println ( "Area of the Room is " +area);
}
}
=======================
Kindly help.
Thanks in advance
Regards,
Bala
@Bala Bhojan - I have executed your Java program in TextPad editor and is working fine for me. Only thing I have done is saving your program as RoomArea.java. As you java program has two classes, I have saved the Java Program with the class name having the main method.
Thanks Arun
Arun, i have doubt in ur post - http://seleniumtwo-by-arun.blogspot.in/search/label/aabk.%2037.%20Using%20getAttribute%28%22value%22%29%20method%20for%20retrieving%20text%20from%20the%20text%20fields
If we have more than one fields how can i code it. Is i want to write the code step by step as you mentioned. Or any other ways is there. If so can you provide it.
I dont have the option to provide the comment in selenium webdriver, so that i'm giving the comment here.
Can you please help me on this?
Post a Comment