Difference between revisions of "Java/C2/Primitive-type-conversions/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
|00:01 | |00:01 | ||
− | | Welcome to the | + | | Welcome to the Spoken Tutorial on '''Type Conversion in Java'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | | + | | In this tutorial, we learn: |
|- | |- | ||
| 00:08 | | 00:08 | ||
− | | | + | | How to convert data from one data type to another. |
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | | + | |The two types of conversion, namely '''implicit''' and '''explicit conversion''' and |
|- | |- | ||
| 00:18 | | 00:18 | ||
− | | | + | |How to convert '''strings''' to numbers. |
|- | |- | ||
| 00:23 | | 00:23 | ||
− | | For this tutorial we are using | + | | For this tutorial we are using: |
− | + | '''Ubuntu v 11.10''', | |
− | + | '''JDK 1.6''' and | |
− | + | '''Eclipse 3.7''' | |
− | + | ||
|- | |- | ||
| 00:33 | | 00:33 | ||
− | | To follow this tutorial, you must have knowledge of '''data types | + | | To follow this tutorial, you must have knowledge of '''data types''' in Java. |
|- | |- | ||
| 00:38 | | 00:38 | ||
− | |If not, for relevant | + | |If not, for relevant tutorials, please visit our website as shown. |
|- | |- | ||
Line 47: | Line 46: | ||
|- | |- | ||
| 00:55 | | 00:55 | ||
− | |Switch to '''Eclipse''' | + | |Switch to '''Eclipse'''. |
|- | |- | ||
| 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 63: | Line 62: | ||
|- | |- | ||
| 01:19 | | 01:19 | ||
− | | '''int a | + | | '''int a ''' equal to '''5;''' |
− | + | '''float b;''' | |
− | '''float b''' | + | |
− | + | '''b''' equal to '''a;''' | |
− | '''b | + | |
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 83: | Line 81: | ||
|- | |- | ||
| 01:48 | | 01:48 | ||
− | |'''System | + | |'''System''' dot '''out''' dot '''println(b);''' |
|- | |- | ||
| 01:58 | | 01:58 | ||
− | |Save the file and run it. | + | |'''Save''' the file and '''run''' it. |
|- | |- | ||
| 02:07 | | 02:07 | ||
− | | We can see that the | + | | We can see that the '''integer 5''' has been converted to '''float 5.0''' |
|- | |- | ||
Line 103: | Line 101: | ||
|- | |- | ||
| 02:24 | | 02:24 | ||
− | |Now let us convert float to an int, using the same method. | + | |Now let us convert '''float''' to an '''int''', using the same method. |
|- | |- | ||
| 02:30 | | 02:30 | ||
− | + | | Remove the '''5''', '''float b''' equal to '''2.5f''' and let us store 'b' in 'a' and print the value of 'a'. | |
|- | |- | ||
Line 115: | Line 113: | ||
|- | |- | ||
| 02:56 | | 02:56 | ||
− | | | + | | we see that there is an error. |
|- | |- | ||
| 03:00 | | 03:00 | ||
− | |The error message reads | + | |The error message reads- '''Type mismatch: cannot convert from float to int'''. |
|- | |- | ||
Line 127: | Line 125: | ||
|- | |- | ||
| 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 135: | Line 133: | ||
|- | |- | ||
| 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. |
|- | |- | ||
| 03:34 | | 03:34 | ||
− | | This statement says the data in the variable | + | | This statement says the data in the variable 'b' has to be converted to '''int''' data type and stored in 'a'. |
|- | |- | ||
| 03:43 | | 03:43 | ||
− | | | + | | save and '''run''' the file. |
|- | |- | ||
Line 155: | Line 153: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
| 04:07 | | 04:07 | ||
− | | Let us try | + | | Let us try the previous example. |
|- | |- | ||
| 04:10 | | 04:10 | ||
− | | int a =5 | + | | '''int a =5; float b; b = (float) a;''' |
|- | |- | ||
| 04:32 | | 04:32 | ||
Line 169: | Line 167: | ||
|- | |- | ||
| 04:36 | | 04:36 | ||
− | | We are using Explicit conversion to convert integer to a ''' float''' | + | | We are using '''Explicit conversion''' to convert integer to a ''' float'''. |
|- | |- | ||
| 04:42 | | 04:42 | ||
− | | Save the file and Run it. | + | | '''Save''' the file and '''Run''' it. |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | we | + | | we see that the '''int''' value has been converted to a '''float''' value. |
|- | |- | ||
| 04:58 | | 04:58 | ||
− | | Let us see what happens when we convert | + | | Let us see what happens when we convert a '''character''' to an '''integer'''. |
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | int a | + | | '''int a'''; '''char c ''' equal to in single quotes '''m'''; |
− | + | ||
− | + | ||
|- | |- | ||
| 05:24 | | 05:24 | ||
− | | '''a | + | | '''a''' equal to '''(int) c;''' |
|- | |- | ||
| 05:32 | | 05:32 | ||
− | | '''System | + | | '''System''' dot '''out''' dot '''println(a);''' |
|- | |- | ||
| 05:36 | | 05:36 | ||
− | | We are converting the | + | | We are converting the character 'm' to an '''integer''' and printing the value. |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | | Let us save and run it | + | | Let us '''save''' and '''run''' it. |
|- | |- | ||
| 05:53 | | 05:53 | ||
− | | As we can see, the output is 109 which the ascii value of | + | | As we can see, the output is 109 which is the ascii value of 'm'. |
|- | |- | ||
Line 217: | Line 214: | ||
|- | |- | ||
| 06:06 | | 06:06 | ||
− | | char c = digit 5 | + | | '''char c =''' digit '''5'''; |
|- | |- | ||
| 06:11 | | 06:11 | ||
− | | Save it and run it | + | | '''Save''' it and '''run''' it. |
|- | |- | ||
| 06:18 | | 06:18 | ||
− | | As we can see, the output is 53 which is the ascii value of the character ‘5’ | + | | As we can see, the output is 53 which is the ascii value of the character ‘5’. |
|- | |- | ||
Line 233: | Line 230: | ||
|- | |- | ||
| 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 241: | Line 238: | ||
|- | |- | ||
| 06:33 | | 06:33 | ||
− | | Clean up the main function | + | | Clean up the '''main''' function. |
|- | |- | ||
| 06:38 | | 06:38 | ||
− | | | + | | Type: |
|- | |- | ||
| 06:40 | | 06:40 | ||
− | |'''String sHeight ''' | + | |'''String sHeight''' meaning string form of Height equal to in double quotes 6 |
|- | |- | ||
| 06:58 | | 06:58 | ||
− | |'''int h '''equal to | + | |'''int h''' equal to explicit conversion '''int''' of '''sHeight''' and |
|- | |- | ||
| 07:11 | | 07:11 | ||
− | |'''System | + | |'''System''' dot '''out''' dot '''println(h)''' '''Save''' the file. |
|- | |- | ||
| 07:27 | | 07:27 | ||
− | |I’ve created a string variable with value 6 and | + | |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.''' |
|- | |- | ||
| 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. |
|- | |- | ||
| 07:48 | | 07:48 | ||
− | |It must be done by other methods. let us use them | + | |It must be done by other methods. let us use them. |
|- | |- | ||
| 07:58 | | 07:58 | ||
− | | Remove '''int sHeight ''' and type | + | | Remove '''int sHeight ''' and type '''Integer''' dot ''parseInt'' sHeight'''; |
|- | |- | ||
| 08:21 | | 08:21 | ||
− | |Save the file and run it | + | |'''Save''' the file and '''run''' it. |
|- | |- | ||
| 08:29 | | 08:29 | ||
− | | we | + | | we see that the value has been successfully converted to an integer. |
|- | |- | ||
| 08:35 | | 08:35 | ||
− | |To do this we use the''' parseInt method '''of the integer module. | + | |To do this, we use the '''parseInt method''' of the '''integer module'''. |
|- | |- | ||
| 08:41 | | 08:41 | ||
− | |Now let us see what happens if there are more than one digits like | + | |Now let us see what happens if there are more than one digits like "6543" |
|- | |- | ||
| 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 | + | |Now, let us see what happens if the '''string''' is a floating point number. |
|- | |- | ||
| 09:10 | | 09:10 | ||
− | |Change | + | |Change "6543" to "65.43". So, we have a floating point number in a '''string''' and we are converting it to an integer. |
|- | |- | ||
| 09:22 | | 09:22 | ||
Line 311: | Line 308: | ||
|- | |- | ||
| 09:31 | | 09:31 | ||
− | | We see that there is an error. This happens because we cannot convert a string which contains '''floating point number''' | + | | We see that there is an error. This happens because we cannot convert a '''string''' which contains '''floating point number''' into 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'''. |
+ | |||
|- | |- | ||
| 10:07 | | 10:07 | ||
− | |We are using the | + | |We are using the '''parseFloat method''' 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, '''run''' it. We can see | + | |'''Save''' the file, '''run''' it. We can see that the string containing a '''floating point number''' has been successfully converted to a floating point number. |
|- | |- | ||
| 10:33 | | 10:33 | ||
− | |And this is how we do implicit and explicit conversion and | + | |And this is how we do '''implicit''' and '''explicit conversion''' and how we convert '''strings''' to '''numbers'''. |
|- | |- | ||
| 10:45 | | 10:45 | ||
Line 337: | Line 335: | ||
|- | |- | ||
| 10:48 | | 10:48 | ||
− | |In this tutorial we have learnt: | + | |In this tutorial we have learnt: How to convert data from one type to another. |
|- | |- | ||
| 10:54 | | 10:54 | ||
Line 347: | Line 345: | ||
|- | |- | ||
| 11:01 | | 11:01 | ||
− | | |As an assignment for this tutorial, read about the '''Integer.toString''' and '''Float.toString | + | | |As an assignment for this tutorial, read about the '''Integer.toString''' and '''Float.toString''' '''methods''' |
|- | |- | ||
| 11:07 | | 11:07 | ||
− | | | + | |and find out what do they do? |
|- | |- | ||
| 11:14 | | 11:14 | ||
− | | | + | | To know more about the 'Spoken Tutorial' project, watch the video available at the following link. |
|- | |- | ||
| 11:20 | | 11:20 | ||
− | | It summarizes the | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 369: | Line 367: | ||
|- | |- | ||
| 11:31 | | 11:31 | ||
− | | Gives certificates | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
| 11:34 | | 11:34 |
Latest revision as of 10:19, 9 June 2018
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:
Ubuntu v 11.10, JDK 1.6 and Eclipse 3.7 |
00:33 | To follow this tutorial, you must have knowledge of data types in Java. |
00:38 | If not, for relevant tutorials, 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 is 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 meaning 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 string is a floating point number. |
09:10 | Change "6543" to "65.43". So, we have a floating point number in a string 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 into 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 method 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 a floating point number. |
10:33 | And this is how we do implicit and explicit conversion and how we convert 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 methods |
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 to 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