Difference between revisions of "Java/C2/Primitive-type-conversions/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 20: | Line 20: | ||
| 00:18 | | 00:18 | ||
|* '''How to convert strings to numbers''' | |* '''How to convert strings to numbers''' | ||
− | |||
− | |||
|- | |- | ||
Line 30: | Line 28: | ||
* '''JDK 1.6''' and | * '''JDK 1.6''' and | ||
* '''Eclipse 3.7''' | * '''Eclipse 3.7''' | ||
− | |||
− | |||
|- | |- | ||
| 00:33 | | 00:33 | ||
| To follow this tutorial, you must have knowledge of '''data types in Java'''. | | To follow this tutorial, you must have knowledge of '''data types in Java'''. | ||
− | |||
|- | |- | ||
Line 45: | Line 40: | ||
| 00:47 | | 00:47 | ||
| '''Type conversion''' means converting data from one data type to another. | | '''Type conversion''' means converting data from one data type to another. | ||
− | |||
|- | |- | ||
| 00:53 | | 00:53 | ||
|Let us see how it it done. | |Let us see how it it done. | ||
− | |||
|- | |- | ||
Line 59: | Line 52: | ||
| 01:02 | | 01:02 | ||
| Here we have the Eclipse IDE and the skeleton required for the rest of the code. | | Here we have the Eclipse IDE and the skeleton required for the rest of the code. | ||
− | |||
|- | |- | ||
| 01:07 | | 01:07 | ||
|I have created a class '''TypeConversion''' and added the main method to it. | |I have created a class '''TypeConversion''' and added the main method to it. | ||
− | |||
|- | |- | ||
Line 77: | Line 68: | ||
'''b '''''equal to''''' a''' | '''b '''''equal to''''' a''' | ||
− | |||
|- | |- | ||
| 01:33 | | 01:33 | ||
|I have created two variables. '''a''' which is an integer and '''b''' which is a float. | |I have created two variables. '''a''' which is an integer and '''b''' which is a float. | ||
− | |||
|- | |- | ||
| 01:39 | | 01:39 | ||
|I’m storing the integer value in a float variable. | |I’m storing the integer value in a float variable. | ||
− | |||
|- | |- | ||
| 01:43 | | 01:43 | ||
|Let us see what the float variable now contains. | |Let us see what the float variable now contains. | ||
− | |||
|- | |- | ||
| 01:48 | | 01:48 | ||
|'''System '''''dot''' ''out '''''dot '''''println ''''' '''''(b); '''' | |'''System '''''dot''' ''out '''''dot '''''println ''''' '''''(b); '''' | ||
− | |||
|- | |- | ||
Line 106: | Line 92: | ||
| 02:07 | | 02:07 | ||
| We can see that the integer '''5''' has been converted to float '''5.0''' | | We can see that the integer '''5''' has been converted to float '''5.0''' | ||
− | |||
|- | |- | ||
| 02:13 | | 02:13 | ||
|This type of conversion is called '''implicit conversion'''. | |This type of conversion is called '''implicit conversion'''. | ||
− | |||
|- | |- | ||
| 02:17 | | 02:17 | ||
|As the name goes, the value is automatically converted to suit the data type. | |As the name goes, the value is automatically converted to suit the data type. | ||
− | |||
|- | |- | ||
Line 129: | Line 112: | ||
| 02:50 | | 02:50 | ||
|Save the file. | |Save the file. | ||
− | |||
|- | |- | ||
| 02:56 | | 02:56 | ||
| we see that there is an error. | | we see that there is an error. | ||
− | |||
|- | |- | ||
| 03:00 | | 03:00 | ||
|The error message reads, '''Type mismatch: cannot convert from float to int''' | |The error message reads, '''Type mismatch: cannot convert from float to int''' | ||
− | |||
|- | |- | ||
| 03:06 | | 03:06 | ||
|It means '''Implicit conversion''' is possible only from an '''int''' to a '''float''' but not the the other way. | |It means '''Implicit conversion''' is possible only from an '''int''' to a '''float''' but not the the other way. | ||
− | |||
|- | |- | ||
| 03:13 | | 03:13 | ||
|To convert a '''float '''to an '''int''' we have to use explicit conversion. | |To convert a '''float '''to an '''int''' we have to use explicit conversion. | ||
− | |||
|- | |- | ||
Line 158: | Line 136: | ||
| 03:23 | | 03:23 | ||
| We do that by using an '''int''' in parentheses, before the variable | | We do that by using an '''int''' in parentheses, before the variable | ||
− | |||
|- | |- | ||
Line 171: | Line 148: | ||
| 03:51 | | 03:51 | ||
| As we can see, the '''float''' value has been converted to '''int'''. | | As we can see, the '''float''' value has been converted to '''int'''. | ||
− | |||
|- | |- | ||
| 03:56 | | 03:56 | ||
| But to suit the data type, the data has been changed accordingly. | | But to suit the data type, the data has been changed accordingly. | ||
− | |||
|- | |- | ||
| 04:01 | | 04:01 | ||
| Explicit conversion can also be used to convert data from int to float. | | Explicit conversion can also be used to convert data from int to float. | ||
− | |||
|- | |- | ||
Line 200: | Line 174: | ||
| 04:42 | | 04:42 | ||
| Save the file and Run it. | | Save the file and Run it. | ||
− | |||
|- | |- | ||
| 04:51 | | 04:51 | ||
| we see that the int value has been converted to a '''float''' value | | we see that the int value has been converted to a '''float''' value | ||
− | |||
|- | |- | ||
Line 234: | Line 206: | ||
| 05:53 | | 05:53 | ||
| As we can see, the output is 109 which the ascii value of '''m'''. | | As we can see, the output is 109 which the ascii value of '''m'''. | ||
− | |||
|- | |- | ||
| 05:58 | | 05:58 | ||
| It means when a '''char''' is converted to '''int''', its ascii value is stored. | | It means when a '''char''' is converted to '''int''', its ascii value is stored. | ||
− | |||
|- | |- | ||
Line 260: | Line 230: | ||
| 06:24 | | 06:24 | ||
| It is not the number 5. | | It is not the number 5. | ||
− | |||
|- | |- | ||
| 06:26 | | 06:26 | ||
| To get the number, we have to use a string and convert it to an integer. | | To get the number, we have to use a string and convert it to an integer. | ||
− | |||
|- | |- | ||
Line 278: | Line 246: | ||
| 06:38 | | 06:38 | ||
| type | | type | ||
− | |||
|- | |- | ||
| 06:40 | | 06:40 | ||
|'''String sHeight ''''' string form of Height equal to in double quotes '''''6 ''''' | |'''String sHeight ''''' string form of Height equal to in double quotes '''''6 ''''' | ||
− | |||
− | |||
|- | |- | ||
Line 296: | Line 261: | ||
| 07:27 | | 07:27 | ||
|I’ve created a string variable with value 6 and i am trying to convert it to an integer but we see that there is an error | |I’ve created a string variable with value 6 and i am trying to convert it to an integer but we see that there is an error | ||
− | |||
− | |||
|- | |- | ||
| 07:37 | | 07:37 | ||
| And the error message reads '''Cannot cast from String to int.''' | | And the error message reads '''Cannot cast from String to int.''' | ||
− | |||
|- | |- | ||
| 07:42 | | 07:42 | ||
|This means for converting strings, we cannot use implicit or explicit conversion. | |This means for converting strings, we cannot use implicit or explicit conversion. | ||
− | |||
|- | |- | ||
Line 319: | Line 280: | ||
| 08:21 | | 08:21 | ||
|Save the file and run it | |Save the file and run it | ||
− | |||
− | |||
|- | |- | ||
Line 335: | Line 294: | ||
|- | |- | ||
| 08:49 | | 08:49 | ||
− | | Save the file and run it | + | | Save the file and run it. |
− | + | ||
|- | |- | ||
| 08:55 | | 08:55 | ||
− | | We see that again the string containing the number has been successfully converted to an integer | + | | We see that again the string containing the number has been successfully converted to an integer. |
|- | |- | ||
| 09:03 | | 09:03 | ||
− | |Now let us see what happens if the strings is a floating point number | + | |Now let us see what happens if the strings is a floating point number. |
|- | |- | ||
| 09:10 | | 09:10 | ||
− | |Change''' 6543 | + | |Change '''6543''' to '''65.43'''. So we have a floating point number in the strings and we are converting it to an integer. |
|- | |- | ||
| 09:22 | | 09:22 | ||
− | |Save the file | + | |'''Save''' the file, '''run''' it. |
− | + | ||
|- | |- | ||
| 09:31 | | 09:31 | ||
− | | We see that there is an error | + | | We see that there is an error. This happens because we cannot convert a string which contains '''floating point number''' to an '''integer'''. |
− | + | ||
|- | |- | ||
| 09:41 | | 09:41 | ||
− | | We have to convert it to a float.Let us see how to do so; | + | | We have to convert it to a '''float'''. Let us see how to do so; |
|- | |- | ||
| 09:45 | | 09:45 | ||
− | |First data type should be float, | + | |First data type should be '''float''', |
|- | |- | ||
| 09:51 | | 09:51 | ||
− | |Second we will use ''' float . parsefloat''' | + | |Second we will use '''float. parsefloat'''. |
|- | |- | ||
| 10:07 | | 10:07 | ||
− | |We are using the Parsefloat methods of the float class to convert the string containing a floating point number into an actual floating point number. | + | |We are using the Parsefloat '''methods''' of the '''float class''' to convert the string containing a floating point number into an actual floating point number. |
|- | |- | ||
| 10:18 | | 10:18 | ||
− | |Save the file | + | |'''Save''' the file, '''run''' it. We can see that the string containing a '''floating point number''' has been successfully converted to floating point number. |
|- | |- | ||
| 10:33 | | 10:33 | ||
− | |And this is how we do | + | |And this is how we do implicit and explicit conversion and How do we converts strings to numbers'''. |
|- | |- | ||
| 10:45 | | 10:45 | ||
− | | | + | | This brings us to the end of the tutorial. |
|- | |- | ||
| 10:48 | | 10:48 | ||
− | |In this tutorial we have learnt how to convert | + | |In this tutorial we have learnt: how to convert data from one type to another. |
|- | |- | ||
| 10:54 | | 10:54 | ||
− | | What is meant by implicit and explicit conversion | + | | What is meant by '''implicit''' and '''explicit''' conversion. |
|- | |- | ||
| 10:57 | | 10:57 | ||
Line 391: | Line 347: | ||
|- | |- | ||
| 11:01 | | 11:01 | ||
− | | |As an assignment for this tutorial | + | | |As an assignment for this tutorial, read about the '''Integer.toString''' and '''Float.toString.''' |
− | + | ||
|- | |- | ||
Line 400: | Line 355: | ||
|- | |- | ||
| 11:14 | | 11:14 | ||
− | | To know more about the | + | | To know more about the 'Spoken Tutorial' project, watch the video available at the following link. |
|- | |- | ||
| 11:20 | | 11:20 | ||
− | | It | + | | It summarizes the spoken- tutorial project. |
|- | |- | ||
| 11:23 | | 11:23 | ||
− | | | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 11:27 | | 11:27 | ||
− | | | + | | The Spoken Tutorial Project Team. Conducts workshops using 'spoken tutorials'. |
|- | |- | ||
| 11:31 | | 11:31 | ||
Line 421: | Line 376: | ||
|- | |- | ||
| 11:40 | | 11:40 | ||
− | |The | + | |The 'Spoken Tutorial' Project is a part of the 'Talk to a Teacher' project. |
|- | |- | ||
| 11:44 | | 11:44 | ||
− | |It is supported by the | + | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 11:50 | | 11:50 | ||
− | | | + | |More information on this mission is available at the following link '''spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro'''. |
|- | |- | ||
| 11:55 | | 11:55 | ||
| This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | | This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | ||
− | |||
− | |||
− | |||
|} | |} |
Revision as of 10:00, 21 March 2015
Time | Narration |
00:01 | Welcome to the spoken tutorial on Type Conversion in Java. |
00:06 | In this tutorial, we learn: |
00:08 | * How to convert data from one data type to another. |
00:13 | * the two types of conversion, namely implicit and explicit conversion. And |
00:18 | * How to convert strings to numbers |
00:23 | For this tutorial we are using
|
00:33 | To follow this tutorial, you must have knowledge of data types in Java. |
00:38 | If not, for relevant tutorial please visit our website as shown |
00:47 | Type conversion means converting data from one data type to another. |
00:53 | Let us see how it it done. |
00:55 | Switch to Eclipse |
01:02 | Here we have the Eclipse IDE and the skeleton required for the rest of the code. |
01:07 | I have created a class TypeConversion and added the main method to it. |
01:13 | Now let us create a few variables. |
01:19 | int a equal to 5
float b b equal to a |
01:33 | I have created two variables. a which is an integer and b which is a float. |
01:39 | I’m storing the integer value in a float variable. |
01:43 | Let us see what the float variable now contains. |
01:48 | System dot out dot println (b); ' |
01:58 | Save the file and run it. |
02:07 | We can see that the integer 5 has been converted to float 5.0 |
02:13 | This type of conversion is called implicit conversion. |
02:17 | As the name goes, the value is automatically converted to suit the data type. |
02:24 | Now let us convert float to an int, using the same method. |
02:30 | Remove the 5 float b equal to 2.5f and let us store b in a and print the value of a . |
02:50 | Save the file. |
02:56 | we see that there is an error. |
03:00 | The error message reads, Type mismatch: cannot convert from float to int |
03:06 | It means Implicit conversion is possible only from an int to a float but not the the other way. |
03:13 | To convert a float to an int we have to use explicit conversion. |
03:17 | Let us see how to do so. |
03:23 | We do that by using an int in parentheses, before the variable |
03:34 | This statement says the data in the variable b has to be converted to int data type and stored in a. |
03:43 | save and run the file |
03:51 | As we can see, the float value has been converted to int. |
03:56 | But to suit the data type, the data has been changed accordingly. |
04:01 | Explicit conversion can also be used to convert data from int to float. |
04:07 | Let us try the previous example. |
04:10 | int a =5, float b, b = (float) a |
04:32 | System.out.println(b); |
04:36 | We are using Explicit conversion to convert integer to a float |
04:42 | Save the file and Run it. |
04:51 | we see that the int value has been converted to a float value |
04:58 | Let us see what happens when we convert a character to an integer. |
05:06 | int a, char c equal to in single quotes 'm';
' |
05:24 | a equal to '(int) c |
05:32 | System dot out dot println ' (a); |
05:36 | We are converting the character m to an integer and printing the value |
05:43 | Let us save and run it |
05:53 | As we can see, the output is 109 which the ascii value of m. |
05:58 | It means when a char is converted to int, its ascii value is stored. |
06:03 | Let us try this with a digit. |
06:06 | char c = digit 5 |
06:11 | Save it and run it |
06:18 | As we can see, the output is 53 which is the ascii value of the character ‘5’ |
06:24 | It is not the number 5. |
06:26 | To get the number, we have to use a string and convert it to an integer. |
06:31 | Now let us see how to do so. |
06:33 | Clean up the main function |
06:38 | type |
06:40 | String sHeight string form of Height equal to in double quotes 6 |
06:58 | int h equal to explicit conversion int of sHeight and |
07:11 | System dot out dot println h Save the file. |
07:27 | I’ve created a string variable with value 6 and i am trying to convert it to an integer but we see that there is an error |
07:37 | And the error message reads Cannot cast from String to int. |
07:42 | This means for converting strings, we cannot use implicit or explicit conversion. |
07:48 | It must be done by other methods. let us use them |
07:58 | Remove int sHeight and type Integer dot parseInt sHeight '. |
08:21 | Save the file and run it |
08:29 | we see that the value has been successfully converted to an integer. |
08:35 | To do this we use the parseInt method of the integer module. |
08:41 | Now let us see what happens if there are more than one digits like 6543 |
08:49 | Save the file and run it. |
08:55 | We see that again the string containing the number has been successfully converted to an integer. |
09:03 | Now let us see what happens if the strings is a floating point number. |
09:10 | Change 6543 to 65.43. So we have a floating point number in the strings and we are converting it to an integer. |
09:22 | Save the file, run it. |
09:31 | We see that there is an error. This happens because we cannot convert a string which contains floating point number to an integer. |
09:41 | We have to convert it to a float. Let us see how to do so; |
09:45 | First data type should be float, |
09:51 | Second we will use float. parsefloat. |
10:07 | We are using the Parsefloat methods of the float class to convert the string containing a floating point number into an actual floating point number. |
10:18 | Save the file, run it. We can see that the string containing a floating point number has been successfully converted to floating point number. |
10:33 | And this is how we do implicit and explicit conversion and How do we converts strings to numbers. |
10:45 | This brings us to the end of the tutorial. |
10:48 | In this tutorial we have learnt: how to convert data from one type to another. |
10:54 | What is meant by implicit and explicit conversion. |
10:57 | and How to convert strings to numbers. |
11:01 | As an assignment for this tutorial, read about the Integer.toString and Float.toString. |
11:07 | And Find out What do they do? |
11:14 | To know more about the 'Spoken Tutorial' project, watch the video available at the following link. |
11:20 | It summarizes the spoken- tutorial project. |
11:23 | If you do not have good bandwidth, you can download and watch it. |
11:27 | The Spoken Tutorial Project Team. Conducts workshops using 'spoken tutorials'. |
11:31 | Gives certificates for those who pass an online test. |
11:34 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
11:40 | The 'Spoken Tutorial' Project is a part of the 'Talk to a Teacher' project. |
11:44 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
11:50 | More information on this mission is available at the following link spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro. |
11:55 | This tutorial has been contributed by TalentSprint. Thanks for joining. |
Contributors and Content Editors
Gaurav, Kavita salve, PoojaMoolya, Priyacst, Sandhya.np14, Sneha