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 and * 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 which is 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 you need 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.
then Open curly bracket. Enter and close curly bracket.
|
Highlight {} | The code between this two curly brackets will belong to the class HelloWorld |
Click on the Save icon. | Now save the file by clicking on the Save icon at the top
It is a good practice to save the file frequently. |
Save as Dialog box | soSave As Dialog box appears.
Browse the location where you want to save your file. |
Select Home directory
Create folder Demo. |
Here, in the home directory i will create a folder.
Let us name it Demo and press enter 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 we created class HelloWorld.
|
Highlight .java | Dot java is the file 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 parentheses inside parentheses String arg Square brackets
Press Enter and close curly bracket.
|
Highlight {} | The code between these two 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.
semi-colon is used to terminate a line.
So Within parentheses in double quotes type My first java program exclamation mark. |
Click on Save icon | Let us Save the file by clicking on Save icon. |
Switch to the Terminal | Let us go to the Terminal. |
ls
|
Make sure that you are in the directory where you save your HelloWorld.java
So type cd Space Demo and hit Enter
|
javac HelloWorld.java | Lets compile this file so type javac Space HelloWorld dot java and hit enter
|
ls | Alright now the file is compiled as we see no error.
We can see that 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.”
|
Java HelloWorld | After successful compilation, run the program using the command,
java (no c) space HelloWorld (no dot java extension) and hit Enter. |
Output | You seewill get the output My first java program!
So we have written our first java program.
|
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 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 Editor.
In Java, all statements are terminated with semicolons. |
Type ; | So go to the fifth line and add a semicolon.
|
Click on Save | Click on the Save icon. It is necessary tosave the file before compiling |
Switch back to the Terminal | Let us go back to the Terminal. |
javac HelloWorld.java | Compile the file using 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 HelloWorld and .
|
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 and W of World are in uppercase.
|
Slide 5
|
* The method name should be in mixed case.
|
Slide 6 | * Example: showString(), main(), goToHelp() Here s of show is in lowercase and S of string is in uppercase .The variable name should not begin with digits. .
|
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 to 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. |