Java/C2/Numerical-Datatypes/English

From Script | Spoken-Tutorial
Revision as of 09:51, 8 May 2013 by Sneha (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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