Java/C2/First-Java-Program/English
Title of script: Getting started with java: first program.
Author: Prathamesh Salunke
Keywords: video tutorial, Hello World, first program
|
|
---|---|
Slide 1
Opening slide |
Welcome to the Spoken Tutorial on getting started with the First java program. |
Slide 2
Learning Objectives |
In this tutorial we will learn
* To create a simple java program. * To compile the program. * To run the program. * About the naming conventions followed. |
Slide 3
System Requirements |
Here we are using
|
Slide 4
Prerequisite |
To follow this tutorial JDK 1.6 must be installed on your system.
If not, for relevant tutorial please visit our website as shown. |
Switch to Terminal and then to the Text Editor | Let us now write our first java program.
For that you need a Terminal and a Text Editor.
|
Point to the gedit | I am using gedit as my Text Editor |
type:
class HelloWorld
} [ENTER]
|
In the text editor, we will first create the class HelloWorld.
So type class HelloWorld HelloWorld is the name of the class.
Press Enter and close curly bracket.
|
Highlight {} | The code inside this block will belong to the class HelloWorld |
Click on the Save icon. | Now save the file by clicking on the Save icon.
It is a good practice to save the file frequently. |
Save as Dialog box | Save As Dialog box appears.
Browse the location where you want to save the file. |
Select Home directory
Create folder Demo. |
Here, in the home directory we will create a folder.
Let us name it as Demo Then inside this folder we will save the file. |
Type:
HelloWorld.java
|
In the Name text-box, type the name of the class.
In java, the name of the class and the file name should be same. Recall that the class we have created is HelloWorld.
|
Highlight .java | Dot java extension is given to a java file. |
Click on Save | Then click on Save button.
So the file is now saved. |
class HelloWorld
{ public static void main() { } } Highlight Main
|
Inside the class, we write the main method.
public static void main within parentheses String arg Square brackets
Press Enter and close curly bracket.
|
Highlight {} | The code inside these curly brackets will belong to the main method. |
Type:
public static void main(String arg[]) { System.out.println(); }
|
We will now write a code to display a line on the Terminal.
|
Click on Save icon | Let us Save the file by clicking on Save icon. |
Switch to the Terminal | Let us go back to the Terminal. |
ls
|
Make sure that you are in the directory where your HelloWorld.java file is present.
|
javac HelloWorld.java | Now, run the command javac Space HelloWorld dot java.
|
ls | Alright now the file is compiled as we see no error.
We can see the HelloWorld.class file created. This file can run anywhere. That is, on any Operating System. We do not need java compiler as well. Hence, java is rightly described as “write once, run anywhere.” |
Switch back to the Terminal | Go back to the Terminal. |
Java HelloWorld | After successful compilation, run the program using the command,
java (no c) space HelloWorld (no dot java) and hit Enter. |
Output | We see the output My first java program!
So we have written our first java program. |
Go back to text editor | Let us go back to the Text Editor. |
Delete ; | Now, remove the semi-colon which is at the end of the statement. |
Click on Save | Click on the Save icon. |
Go to Terminal | Let us go back to the Terminal. |
javac HelloWorld.java | Run the command javac Space HelloWorld dot java. |
HelloWorld.java:5: ';' expected
System.out.println("My first java program") ^ 1 error |
The compiler gives an error.
It says, a semi colon is expected on the fifth line.
|
Switch back to the Text Editor | Let us go back to the Text Editor.
In Java, all statements are terminated with semicolons. |
Type ; | So go to the fifth line.
At the end of the statement add a semi-colon. |
Click on Save | Click on the Save icon. |
Switch back to the Terminal | Let us go back to the Terminal. |
javac HelloWorld.java | Run the command javac Space HelloWorld dot java. |
java HelloWorld | The file is successfully compiled as we see no errors.
Now, run the program using the command java space HelloWorld and hit Enter.
|
Output | We see the output My first java program!
This is how you can handle errors in java. As the series progresses, we will learn more about the errors. |
Switch to Slides
Slide 4
|
We now see what are the naming conventions in java.
So, H of hello, W of World are in uppercase.
|
Slide 5
|
* The method name should be in mixed case.
|
Slide 6 | * Example: showString(), main(), goToHelp().The variable name should not begin with digits. Here s of show is in lowercase and S of string is in uppercase.
|
Slide 7
Summary |
So in this tutorial, we have learnt to write, compile and run a simple java program.
We also saw the naming conventions followed in java. |
Slide 8
Assignment |
For self assessment write a simple java program which prints Java file name and class name should be same.
|
Slide 9
About Slide |
|
Slide 10
About Slide
|
The Spoken Tutorial Project Team
|
Slide 11
Acknowledgment |
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Slide 12
Contributor Slide |
We have come to the end of this tutorial.
Thanks for joining. This is Prathamesh Salunke signing off. Jai Hind. |