1 00:00:01 --> 00:00:06 Welcome to the spoken tutorial on Numerical Data types in Java. 2 00:00:07 --> 00:00:09 In this tutorial, we will learn about: 3 00:00:10 --> 00:00:12 * The various Numerical Data types available in Java and 4 00:00:13 --> 00:00:17 * How to use them to store numerical data. 5 00:00:18 --> 00:00:26 For this tutorial, we are using Ubuntu 11.10, JDK 1.6 and Eclipse 3.7. 6 00:00:27 --> 00:00:33 To follow this tutorial, you must know how to write and run a simple java program in Eclipse. 7 00:00:34 --> 00:00:41 If not, for relevant tutorials, please visit our website as shown. 8 00:00:42 --> 00:00:46 The data type used to store integers is called int. 9 00:00:47 --> 00:00:51 The data type used to store decimal numbers is called float. 10 00:00:52 --> 00:01:01 Let us define and use integers first. 11 00:01:02 --> 00:01:09 Here, we have the 'Eclipse IDE' and the skeleton required for the rest of the code. 12 00:01:10 --> 00:01:14 We have created a class NumericalData and added the main method to it. 13 00:01:15 --> 00:01:19 Now, let us see how to store a number. 14 00:01:20 --> 00:01:26 int distance equal to 28; 15 00:01:27 --> 00:01:32 This statement stores the integer value in the name distance. 16 00:01:33 --> 00:01:36 The name distance is called an integer variable. 17 00:01:37 --> 00:01:46 Now we shall use the variable distance to print the value stored in it. 18 00:01:47 --> 00:02:00 System dot out dot println in parentheses distance; 19 00:02:01 --> 00:02:05 This statement prints the value of the variable distance. 20 00:02:06 --> 00:02:13 Save the file and Run it. 21 00:02:14 --> 00:02:20 We can see that the value 28 has been stored in distance and it has been printed. 22 00:02:21 --> 00:02:24 Now let us change the value stored in the variable. 23 00:02:25 --> 00:02:28 change 28 to 24. 24 00:02:29 --> 00:02:33 Save and Run. 25 00:02:34 --> 00:02:38 We see that the output has changed accordingly. 26 00:02:39 --> 00:02:41 int can also store negative values. 27 00:02:42 --> 00:02:47 Change 24 to minus 25 (-25). 28 00:02:48 --> 00:02:55 Save and Run. 29 00:02:56 --> 00:03:01 As we can see, even negative values can be stored in variables of the type int. 30 00:03:02 --> 00:03:05 The data type int is enough for most of our programming needs. 31 00:03:06 --> 00:03:09 But it can only store values up to a limit. 32 00:03:10 --> 00:03:24 Let us try to store a large value and see what happens. 33 00:03:25 --> 00:03:33 As we can see, there is a red line below the number which indicates an error. 34 00:03:34 --> 00:03:41 The error message says: the number is out of range for a variable of the type int. 35 00:03:42 --> 00:03:48 int takes 32 bits of memory and can store values only from -2 power 31 to 2 power 31. 36 00:03:49 --> 00:03:53 To store large numbers, Java provides the long data type. 37 00:03:54 --> 00:03:58 Let us use it to store a large value. 38 00:03:59 --> 00:04:03 Change int to long and 39 00:04:04 --> 00:04:10 add a capital L at the end of the number. 40 00:04:11 --> 00:04:15 Save it with Ctrl, S. 41 00:04:16 --> 00:04:18 We see that now there is no error. 42 00:04:19 --> 00:04:26 Let us run it with Ctrl, F11. The value has been printed. 43 00:04:27 --> 00:04:31 We can see that large numbers can be stored in a long variable. 44 00:04:32 --> 00:04:36 Now, let us store a decimal number in a int variable. 45 00:04:37 --> 00:04:49 Change long to int and change the number to 23.5; 46 00:04:50 --> 00:04:59 As we can see, there is an error. That is because int can only store integers. 47 00:05:00 --> 00:05:04 To store decimal numbers, we have to use float. 48 00:05:05 --> 00:05:09 change the data type to float. 49 00:05:10 --> 00:05:16 And add an f at the end of the value; 50 00:05:17 --> 00:05:18 Save it. 51 00:05:19 --> 00:05:21 We see that now their is no error. 52 00:05:22 --> 00:05:28 Run it with Control F11. 53 00:05:29 --> 00:05:36 As we can see, the decimal value has been stored and the value has been printed. 54 00:05:37 --> 00:05:45 Now, let us change the value of the variable distance. 55 00:05:46 --> 00:05:52 Add a lot of numbers after the decimal point as shown. 56 00:05:53 --> 00:06:00 Save it and Run it. 57 00:06:01 --> 00:06:05 we see that the output is little different from what has been stored. 58 00:06:06 --> 00:06:10 This happens because there is a limit to the precision of a floating point number. 59 00:06:11 --> 00:06:17 It is rounded off to the closest possible number if it cannot be stored accurately. 60 00:06:18 --> 00:06:22 Now let us see the naming rules for variables. 61 00:06:23 --> 00:06:29 Add a number 2 before the name. 62 00:06:30 --> 00:06:33 we see that there is a syntax error. 63 00:06:34 --> 00:06:39 This is because a variable name must only start with an alphabet or an underscore. 64 00:06:40 --> 00:06:44 But generally 'underscore' is not used to start a variable name. 65 00:06:45 --> 00:06:54 Now, let us add the number at the end of the variable name. 66 00:06:55 --> 00:06:58 We see that there is no error. 67 00:06:59 --> 00:07:03 A variable name can have digits but not at the beginning. 68 00:07:04 --> 00:07:14 Now add an 'underscore' in the middle of the name, 69 00:07:15 --> 00:07:16 we see that there is no error 70 00:07:17 --> 00:07:21 which means an 'underscore' is permitted in a variable name. 71 00:07:22 --> 00:07:27 But any other punctuation in a variable name might give a syntax error or other errors. 72 00:07:28 --> 00:07:34 This is how you store numerical data in Java. 73 00:07:35 --> 00:07:37 This brings us to the end of the tutorial. 74 00:07:38 --> 00:07:43 In this tutorial, we have learnt about the various numerical data types 75 00:07:44 --> 00:07:45 and how to store numerical data. 76 00:07:46 --> 00:07:50 And we have also learnt the rules for naming a variable. 77 00:07:51 --> 00:07:52 As an assignment for this tutorial, 78 00:07:53 --> 00:07:55 read about other numerical data types and 79 00:07:56 --> 00:07:59 see how they are different from int and float. 80 00:08:00 --> 00:08:04 Java tutorials are available at the following link. 81 00:08:05 --> 00:08:10 To know more about the Spoken Tutorial project, watch the video available at the following link. [1] 82 00:08:11 --> 00:08:13 It summarizes the Spoken Tutorial project. 83 00:08:14 --> 00:08:19 If you do not have good bandwidth, you can download and watch it. 84 00:08:20 --> 00:08:23 The Spoken Tutorial Project team: conducts workshops using spoken tutorials. 85 00:08:24 --> 00:08:34 Gives certificates to those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. 86 00:08:35 --> 00:08:38 Spoken Tutorial Project is a part of the Talk to a Teacher project. 87 00:08:39 --> 00:08:44 It supported by the National Mission on Education through ICT, MHRD, Government of India. 88 00:08:45 --> 00:08:50 More information on this mission is available at the following link. 89 00:08:51 --> 00:08:56 This tutorial has been contributed by TalentSprint. Thanks for joining.