Difference between revisions of "Java/C2/Numerical-Datatypes/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Numerical Datatypes in Java '''Author''': TalentSprint '''Keywords: '''datatype, int, float, numerical data, video tutorial {| style="border-spacing:0…')
 
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
'''Keywords: '''datatype, int, float, numerical data, video tutorial
 
'''Keywords: '''datatype, int, float, numerical data, video tutorial
  
 
+
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
Line 21: Line 21:
  
 
'''Learning Outcomes'''
 
'''Learning Outcomes'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn about:
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, we will learn about:
  
 
* the various '''Numerical Datatypes '''available in '''Java''' and
 
* the various '''Numerical Datatypes '''available in '''Java''' and
Line 80: Line 80:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''class NumericalData '''and''' main '''method
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''class NumericalData '''and''' main '''method
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have created a class '''NumericalData''' and added the main method to it.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have created a class '''NumericalData''' and added the main method to it.  
  
  
Line 96: Line 96:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''distance'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''distance'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| The name '''distance''' is called as an integer variable.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| The name '''distance''' is called an integer variable.
  
  
Line 112: Line 112:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Save and Run the code.
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Save and Run the code.
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now '''Save''' the file and '''Run''' it using '''Control''' '''F11'''.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' the file and '''Run''' it
  
  
Line 140: Line 140:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output.
| style="border:0.035cm solid #000000;padding:0.176cm;"| We can see that the output is different accordingly.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| We see that the output has changed accordingly.
  
 
|-
 
|-
Line 156: Line 156:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save and run the code
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save and run the code
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' it and '''Run it'''.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run ''''.
  
 
|-
 
|-
Line 164: Line 164:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''distance = 24''' to '''distance = 9876543210'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''distance = 24''' to '''distance = 9876543210'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Data type '''int '''is enough for most of our programming needs.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|The Data type '''int '''is enough for most of our programming needs.
  
  
Line 188: Line 188:
  
  
Let use It to store a large value.
+
Let us  use It to store a large value.
  
 
|-
 
|-
Line 195: Line 195:
  
 
Change '''9876543210''' to '''9876543210L'''
 
Change '''9876543210''' to '''9876543210L'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have to change the data type to '''long''' and  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change the '''int '''to '''long''' and  
  
add a capitol '''L''' at the end of the number.
+
add a capital '''L''' at the end of the number.
  
  
Save it.
+
Save it with''' Ctrl S'''
  
  
Line 206: Line 206:
  
  
Let us run it.
+
Let us run it with '''Ctrl F11'''. The value has been printed
  
  
Line 237: Line 237:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' and '''Run'''.  
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' and '''Run'''.  
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Control S''' to save.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|   save it.
  
  
Line 251: Line 251:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''23.5f''' to '''23.123456789f'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''23.5f''' to '''23.123456789f'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us change the value of float.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us change the value of the variable distance
  
  
Line 258: Line 258:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''.  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''.  
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save it with '''Control S.'''
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save it and Run it
  
  
'''Control F11''' to run.
+
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to '''Output'''.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to '''Output'''.
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is a little different from what has been stored.  
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|   we see, that  the output is   little different from what has been stored.  
  
  
Line 284: Line 283:
  
  
And we see that, there is a '''syntax error.'''  
+
  we see that, there is a '''syntax error.'''  
  
  
Line 294: Line 293:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''2distance''' to '''distance2'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''2distance''' to '''distance2'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us add a '''2''' at the end of the name.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|now Let us add a '''2''' at the end of the variable name.
  
  
Line 300: Line 299:
  
  
A variable name can have digits but not at the beginning.
+
A variable name can have digits but not at the beginning.  
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''distance2''' to '''dist_from'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''distance2''' to '''dist_from'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us add an '''underscore''' to the variable name.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now add an underscore in the middle of the name
 
+
  
As we can see, there is no error.
+
we   see that there is no error.
  
  
An underscore in a variable name is permitted.
+
Which means an underscore is permitted  in a variable name
  
  
But any other punctuation in a variable name might give a syntax error or other errors.
+
But any other punctuation in a variable that give an syntax error or other errors.
  
  
Line 331: Line 328:
 
How to use them to '''store numerical data. '''
 
How to use them to '''store numerical data. '''
  
We have learnt the '''rules for naming a variable'''.
+
We have also learnt the '''rules for naming a variable'''.
  
 
|-
 
|-

Latest revision as of 09:51, 8 May 2013

Title of script: Numerical Datatypes in Java

Author: TalentSprint

Keywords: datatype, int, float, numerical data, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on Numerical Datatypes in Java.
Slide 2

Learning Outcomes

In this tutorial, we will learn about:
  • the various Numerical Datatypes available in Java and
  • how to use them to store numerical data.


Slide 3

Tools Used

For this tutorial we are using
  • Ubuntu 11.10,
  • JDK 1.6 and
  • Eclipse 3.7.0


Slide 4

Prerequisites

To follow this tutorial, you must know how to write and run a simple java program in Eclipse.


If not, for relevant tutorial please visit our website as shown



Slide 5

Numerical Datatypes

The data type used to store integers is called int


The data type used to store decimal numbers is called float.


Let us define and use integers first.

Minimize slides and switch to Eclipse.

It should contain the following code.

Public class NumericalData {

public static void main(String[] args) {

}

}

Here, we have the Eclipse IDE and skeleton required for the rest of the code.
Point to the class NumericalData and main method We have created a class NumericalData and added the main method to it.


Now let us see how to store a number.

Type the following code in the main method.

int distance = 28;

int distance equal to 28


This statement stores the integer value in the name distance.

Point to the distance The name distance is called an integer variable.


Now we shall use the variable distance to print the value stored in it.

Type the following code after int...

System.out.println(distance);

System dot out dot println. In parantheses distance.


This statement prints the value of the variable distance

Save and Run the code. Save the file and Run it



Point to the output console We can see that the value 28 has been stored in distance and it has been printed.


Now let us change the value stored in the variable.

Change distance = 28 to distance = 24 change 28 to 24



Save and run the file. Save and Run



Point to the output. We see that the output has changed accordingly.
Replace distance = 24 with

distance = -25

int can also store negative values.


Change 24 to minus 25



Save and run the code Save and Run '.
Point to the output As we can see, even negative values can be stored in variables of the type int.
Change distance = 24 to distance = 9876543210 The Data type int is enough for most of our programming needs.


But it can only store values up to a limit.


Let us try to store a large value and see what happens.

Point to the red line As we can see, there is a red line below the number which indicates an error.
Point to the cross mark on the left The error message says the number is out of range for a variable of the type int


int takes 32 bits of memory and can store values only from -2 power 31 to 2 power 31.

To store large numbers, Java provides the long data type.


Let us use It to store a large value.

Change the int to long


Change 9876543210 to 9876543210L

Change the int to long and

add a capital L at the end of the number.


Save it with Ctrl S


We see that now there is no error.


Let us run it with Ctrl F11. The value has been printed


We can see that large numbers can be stored in a long variable.

Change long distance = … to int distance = 23.5; Now let us store a decimal number in a int variable.


Change long to int and change the number to 23.5;

Point to the error. As we can see, there is an error. That is because int can only store integers.


To store decimal numbers, we have to use float.

Change int distance... to float distance


Change 23.5 to 23.5f

change the data type to float.


And add an f at the end of the value.

Save and Run. save it.


We see that there is no error.


run it with Control F11

Point to output As we can see, the decimal value has been stored and the value has been printed.
Change 23.5f to 23.123456789f Now let us change the value of the variable distance


Add lot of numbers after the decimal point as shown.

Save and Run. Save it and Run it


Point to Output. we see, that the output is little different from what has been stored.


This happens because there is a limit to the precision of a floating point number.


It is rounded off to the closest possible number if it cannot be stored accurately.

Change distance to 2distance


Point to the cross mark on the left

Now let us see the naming rules for variables.


Add a number 2 before the name.


 we see that, there is a syntax error. 


This is because a variable name must only start with an alphabet or an underscore.


But generally underscore is not used to start a variable name.

Change 2distance to distance2 now Let us add a 2 at the end of the variable name.


We see that, there is no error.


A variable name can have digits but not at the beginning.

Change distance2 to dist_from Now add an underscore in the middle of the name
we   see that there is no error.


Which means an underscore is permitted in a variable name


But any other punctuation in a variable that give an syntax error or other errors.


This is how you store numerical data in Java.

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

This brings us to the end of this tutorial.

In this tutorial we have learnt about the various numerical datatypes.

How to use them to store numerical data.

We have also learnt the rules for naming a variable.

Slide 7Assignment


As an assignment for this tutorial,


Read about other numerical data types and

see how they are different from int and float.


Java tutorials are available at the following link.

Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


To know more about the Spoken Tutorial project, watch the video available at the following link.


It summarises the Spoken Tutorial project.If you do not have good bandwidth, you can download and watch it.

Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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 AT spoken HYPHEN tutorial DOT org.

Slide 10Acknowledgement
  • 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


Spoken Tutorial Project is a part of the Talk to a Teacher project.


It supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at the following link.

Slide 11About the contributor
  • This tutorial has been contributed by TalentSprint
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Chandrika, Sneha