Difference between revisions of "Java/C2/First-Java-Program/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 27: Line 27:
 
<nowiki>* To compile the program.</nowiki>
 
<nowiki>* To compile the program.</nowiki>
  
<nowiki>* To run the program.</nowiki> and
+
<nowiki>* To run the program</nowiki> and
  
 
<nowiki>* About the naming conventions followed.</nowiki>
 
<nowiki>* About the naming conventions followed.</nowiki>
Line 278: Line 278:
 
| style="border-top:none;border-bottom:0.035cm solid #000000;border-left:0.035cm solid #000000;border-right:0.035cm solid #000000;padding:0.097cm;"| After successful compilation, run the program using the command,  
 
| style="border-top:none;border-bottom:0.035cm solid #000000;border-left:0.035cm solid #000000;border-right:0.035cm solid #000000;padding:0.097cm;"| After successful compilation, run the program using the command,  
  
'''''java '''''(no c) space '''''HelloWorld '''''(no dot java) extension and hit '''Enter'''.
+
'''''java '''''(no c) space '''''HelloWorld '''''(no dot java extension) and hit '''Enter'''.
  
 
|-
 
|-

Latest revision as of 16:32, 22 January 2014

Title of script: Getting started with java: first program.

Author: Prathamesh Salunke

Keywords: video tutorial, Hello World, first program


Visual Cue
Narration
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
  • Ubuntu version 11.10 and
  • Java Development Environment jdk 1.6


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


Highlight 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


type HelloWorld dot 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.


So we will save the file as HelloWorld dot java

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


main(String arg[])

Inside the class, we write the main method.


So type:

public static void main parentheses inside parentheses String arg Square brackets


Main functions marks the starting point of the program.


We will describe public, static, void and String arg in a future tutorial.


Then once again, open curly bracket.

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();

}


Highlight semicolon


System.out.println(“My first java program!”);

We will now write a code to display a line on the Terminal.


So inside main method typeSystem dot out dot println parentheses semi-colon


This is the statement used to print a line.


 semi-colon is used to terminate a line.


Now let us tell Java what to print.


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


cd Demo


ls

Make sure that you are in the directory where you save your HelloWorld.java


Remember that I am in my home directory.



So type cd Space Demo and hit Enter


We see HelloWorld.java file present in the Demo folder.

javac HelloWorld.java Lets compile this file so type javac Space HelloWorld dot java and hit enter


This compile the file that we have created.

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.


The up arrow points to the error statement.

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.
  • The class name should be in CamelCase.
  • Which means each new word begins with an upper case.
  • Example: class HelloWorld, class ChessGame.

So, H of hello and W of World are in uppercase.


Similarly C and G of Chess and Game respectively are in uppercase.

Slide 5


* The method name should be in mixed case.
  • Which means that the first word should begin with a lower case.
  • And all new words followed should begin with an upper case.
  • Also the method name should be a verb.


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.
.
  • We cannot use keywords for our class, method or variable name.
  • For example: cannot use keywords like public, private, void, static and many more.


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.


To know more about the Spoken Tutorial Project

Slide 9

About Slide

  • If you do not have good bandwidth, you can download and watch it


Slide 10

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 11

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Slide 12

Contributor Slide

We have come to the end of this tutorial.

Thanks for joining.

This is Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Ashwini, Chandrika, Sneha