Java/C2/Primitive-type-conversions/English-timed
From Script | Spoken-Tutorial
Revision as of 17:40, 21 March 2015 by Sandhya.np14 (Talk | contribs)
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 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 the 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 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 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. |
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