Java/C2/Primitive-type-conversions/English
Title of script: Type Conversion in Java
Author: TalentSprint
Keywords: datatype, int, float, char, conversion, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Type Conversion in Java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn:
|
Slide 3
Tools Used |
For this tutorial we are using
|
Slide 4
Prerequisites |
To follow this tutorial, you must have knowledge of data types in Java.
|
Slide 5
Type Conversion |
Type conversion means converting data from one data type to another.
|
Minimize Slides and open Eclipse
Eclipse should contain the following code public class TypeConversion { public static void main(String[] args) { } } |
Here we have the Eclipse IDE and the skeleton required for the rest of the code.
|
In the main method, type
int a = 5; float b; b = a;
|
int a equal to 5
float b b equal to a
|
Save and run. Point to output | We can see that the integer 5 has been converted to float 5.0
|
Change a = 5 to a
Change b to b = 2.5f; Change b = a to a = b;
|
Change a equal to 5 to a
Change b to b equal to 2.5f; Change b equal to a to a equal to b;
|
Change a = b to a = (int) b
and point to that line |
We do that by using an int in parentheses.
|
save and run. Point to output | save and run
|
Change int a to int a = 5
Change float b = 2.5f to float b; Change a = (int) b to b = (float) a; Change println(a) to println(b)
|
Change a to a equal to 5
|
Remove everything in the main function and type the following
int a; a = (int) c; System.out.println(a); |
char c equal to 'm';
int a; a equal to (int) c; System dot out dot println within brackets a.
|
Save and run. Point to output | As we can see, the output is 109 which the ascii value of m.
|
Change c = 'm' to c = ' ' | Change c equal to 'm' to c equal to '5'
|
Save and run. Point to output | As we can see, the output is 53 which is the ascii value of the character ‘5’
It is not the number 5.
|
Remove everyting in main function and type the following.
int h = (int) sHeight; System.out.println(h); |
Remove everyting in main function.
Next line type int h equal to within brackets int sHeight semicolon. System dot out dot println within brackets h semicolon.
|
Point to cross mark on the left | It reads Cannot cast from String to int.
|
Remove the int h... and type
int h = Integer.parseInt(sHeight); |
int h equal to Integer dot parseInt within brackets sHeight semicolon.
|
Save and run. Point to output | As we can see, The string has now been converted to integer.
|
Change sHeight = “6” to sHeight = “6.5”
|
Change 6 to 6.5
|
Point to console | It gives an error .
|
Remove the line int h... and type
float h = Float.parseFloat(sHeight);
|
Let us use it.
|
Save and run. Point to output | We can see that string has been converted to a float.
|
Minimize the Eclipse window and switch to slides.
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt how to convert data from one type to another. What is meant by implicit conversion and explicit conversin and How to convert strings to numbers. |
Slide 7Assignment
|
Read about Integer.toString and Float.toString.
|
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.
if you do not have good bandwidth, you can download and watch it |
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and 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 and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|