Difference between revisions of "Java/C2/Primitive-type-conversions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Type Conversion in Java '''Author''': TalentSprint '''Keywords: '''datatype, int, float, char, conversion, video tutorial {| style="border-spacing:0;"…')
 
 
Line 113: Line 113:
  
  
'''System '''''dot''' ''out '''''dot '''''println '''''within brackets '''''b '''''semicolon.''
+
'''System '''''dot''' ''out '''''dot '''''println ''''' '''''b ''''
  
  
Line 126: Line 126:
  
  
As the name goes, it means the value is automatically converted to suit the data type.
+
As the name goes, the value is automatically converted to suit the data type.
  
  
Now let us convert float to an int, using same method.
+
Now let us convert float to an int, using the same method.
  
 
|-
 
|-
Line 143: Line 143:
  
 
Point to the cross mark on left.
 
Point to the cross mark on left.
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''a '''''equal to '''''5''' to '''a'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove the 5 float b equal to 2.5f and let us store b in a and print the value of  a .  
 
+
Change '''b '''to '''b '''''equal to '''''2.5f<nowiki>;</nowiki>'''
+
 
+
Change '''b '''''equal to '''''a''' to '''a '''''equal to '''''b'''<nowiki>;</nowiki>
+
 
+
 
+
Let us print the value in a.
+
 
+
 
+
Change '''b''' to '''a'''.
+
 
+
 
+
I'm now trying to store a '''float''' value in an '''int''' variable.
+
 
+
 
+
 
Save the file.
 
Save the file.
  
  
But as we can see, there is an error.
+
  we   see that there is an error.
  
  
It reads, '''Type mismatch: cannot convert from float to int'''
+
The error message  reads, '''Type mismatch: cannot convert from float to int'''
  
  
It means '''Implicit conversion''' is possible 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.
  
  
Line 180: Line 166:
  
 
and point to that line
 
and point to that line
| style="border:0.035cm solid #000000;padding:0.097cm;"| We do that by using an '''int''' in parentheses.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| We do that by using an '''int''' in parentheses.before the variable
 
+
 
+
Change '''a '''''equal to '''''b''' to '''a '''''equal to '''''(int) b'''
+
 
+
  
 +
 
This statement says the data in the variable '''b''' has to be converted to '''int''' data type and stored in a.
 
This statement says the data in the variable '''b''' has to be converted to '''int''' data type and stored in a.
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| save and run. Point to output
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| save and run. Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| save and run
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| save and run the file
  
  
Line 202: Line 185:
  
  
Let us try an example.
+
Let us try anthe previous example.
  
 
|-
 
|-
Line 215: Line 198:
  
 
save and run. Point to output
 
save and run. Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''a''' to '''a '''''equal to '''''5'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| int a =5, float b, b = float a
 +
'''System.out.println(b);'''
 +
We are using Explicit conversion to convert integer to a ''' float'''
 +
 +
Save  the file and Run it.
  
  
Change '''b '''''equal to '''''2.5f''' to '''float b<nowiki>;</nowiki>'''
+
we  see that  the int value has been converted  to a '''float''' value
  
  
Change '''a '''''equal to '''''(int) b''' to '''b '''''equal to '''''(float) a<nowiki>;</nowiki>'''
+
Let us see what happens when we convert '''character''' to '''integer'''.
 
+
 
+
Change '''println(a)''' to '''println(b)'''
+
 
+
 
+
Save and Run it.
+
 
+
 
+
As we can see, the data has been converted from '''int''' to '''float'''.
+
 
+
 
+
Let us what happens when we convert '''character''' to '''integer'''.
+
  
 
|-
 
|-
Line 246: Line 221:
  
 
'''System.out.println(a);'''
 
'''System.out.println(a);'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''char c '''''equal to ''''''m';'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|int a, charc '''char c '''''equal to ''''''m';'''
 
+
'''int a<nowiki>;</nowiki>'''
+
  
 +
'
 
'''a '''''equal to '''''(int) c<nowiki>;</nowiki>'''
 
'''a '''''equal to '''''(int) c<nowiki>;</nowiki>'''
  
'''System '''''dot '''''out '''''dot '''''println '''''within brackets '''''a.'''
+
'''System '''''dot '''''out '''''dot '''''println '''' '''''a.'''
 
+
 
+
I'm converting the alphabet '''m '''to an integer and storing it in '''a.'''
+
 
+
  
Let us run and see the output.
+
We are convewrting character m to an integer and printing the value
 +
Let us save and run it
  
 
|-
 
|-
Line 272: Line 243:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''c = 'm'''' to '''c = ' ''''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''c = 'm'''' to '''c = ' ''''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''c '''''equal to ''''''m'''' to '''c '''''equal to ''''''5''''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| char c = digit 5
  
 
+
Save it and run it
Now I'm converting the character '''‘5’''' to an integer.
+
 
+
 
+
Let us see the output.
+
  
 
|-
 
|-
Line 301: Line 268:
  
 
'''System.out.println(h);'''
 
'''System.out.println(h);'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove everyting in main function.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Clean up the main function  
  
 +
type
  
Then type
 
  
 +
'''String sHeight '''''equal to in double quotes '''''6 '''''semicolon.''
  
'''String sHeight '''''equal to within double quotes '''''6 '''''semicolon.''
+
  
Next line type
+
'''int h '''equal to '''explicit conversion''' to  '''int of sHeight '''  and
 +
'''System '''''dot''''' out '''''dot '''''println '''''  '''''h ''''' Save the file.
  
'''int h '''equal to within brackets '''int sHeight '''''semicolon.''
+
I’ve created a string variable with value 6 and i am trying to convert it to an integer but we see that their is an error
  
'''System '''''dot''''' out '''''dot '''''println '''''within brackets '''''h '''''semicolon.''
 
 
 
I’ve created a string variable with value 6.
 
 
 
I'm using the '''explicit conversion'''
 
 
 
Save the file.
 
 
 
We see that there is an error.
 
  
 +
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to cross mark on the left
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to cross mark on the left
| style="border:0.035cm solid #000000;padding:0.097cm;"| It reads '''Cannot cast from String to int.'''  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| And the error message reads '''Cannot cast from String to int.'''  
  
  
Line 335: Line 292:
  
  
It must be done by other methods.
+
It must be done by other methods. let us use them
  
 
|-
 
|-
Line 341: Line 298:
  
 
'''int h = Integer.parseInt(sHeight);'''
 
'''int h = Integer.parseInt(sHeight);'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''int h '''''equal to '''''Integer '''''dot '''''parseInt '''within brackets''' sHeight '''semicolon'''.'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove the '''int h...''' and type  '''''Integer '''''dot '''''parseInt ''' sHeight .
 +
Save the file and run it
  
  
We have to use the '''parseInt''' method from the '''Integer''' class to do so.
 
 
 
Let us look at the output
 
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Save and run. Point to output
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Save and run. Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| As we can see, The string has now been converted to integer.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| we   see that  value  has   been successfully  converted to an  integer.
  
 +
To do this we use the parseInt method of the integer module. Now let us see what happens if their are more than one digits like '''6543'''
 +
Save the file and run it
  
Now what if the value was a decimal number. Let us try that.
 
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''sHeight = “6”''' to '''sHeight = “6.5”'''
 
 
 
save and run
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''6''' to '''6.5'''
 
 
 
We don't see any error as of now.
 
 
 
Run it.
 
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to console
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to console
| style="border:0.035cm solid #000000;padding:0.097cm;"| It gives an error .
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
 
+
 
+
This is because the parseInt function can only be used on strings that contain integers.
+
 
+
 
+
For strings that contain floating point numbers, we have to use the parseFloat method.
+
 
+
|-
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove the line '''int h...''' and type
+
 
+
'''float h = Float.parseFloat(sHeight);'''
+
 
+
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us use it.
+
 
+
 
+
'''float h '''''equal to '''''Float.parseFloat(sHeight)'''
+
  
 +
We see that again strings contain the number has been successfully converted to an integer
 +
Now let us see what happens if the strings is a floating point number
 +
Change''' 6543 to65.43'''.So we have a floating point number in the string and we are converting it to an integer
 +
Save the file run it.
 +
 +
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save and run. Point to output
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save and run. Point to output
| style="border:0.035cm solid #000000;padding:0.176cm;"| We can see that string has been converted to a '''float'''.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| We see that their is an error  This happens beacuse we cannot convert strings which contains    floating point number into an integer
 
+
  
This is how we convert data from one type to another, in Java.
 
  
 +
We have to convert it TO  afloat.Let us see how to do so;
 +
First data type should be float,
 +
Second we will use float . parsefloat,
 +
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.
 +
Save the file  run it. We can  see  the string containing a floating point number  has been successfully converted into floating point number.
 +
And this is how we do '''implicit and explicit conversion and How do we converts strings to numbers
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Minimize the '''Eclipse''' window and switch to slides.
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Minimize the '''Eclipse''' window and switch to slides.
Line 405: Line 339:
  
 
'''Summary'''
 
'''Summary'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have come to the end of this tutorial.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| This brings us 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'''.
+
In this tutorial we have learnt how to convert '''data from one type to another'''. What is meant by implicit explicit conversin and How to '''convert strings to numbers'''.
  
 
|-
 
|-
Line 414: Line 348:
  
  
| style="border:0.035cm solid #000000;padding:0.097cm;"| Read about '''Integer.toString''' and '''Float.toString. '''
+
| style="border:0.035cm solid #000000;padding:0.097cm;" |As an assignment for this tutorial Read about '''Integer.toString''' and '''Float.toString. '''
  
  
What do they do?
+
Find out What do they do?
  
 
|-
 
|-

Latest revision as of 12:23, 8 May 2013

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:
  • How to convert data from one data type to another.
  • the two types of conversion, namely implicit and explicit conversion.
  • How to convert strings to numbers


Slide 3

Tools Used

For this tutorial we are using
  • Ubuntu 11.10
  • JDK 1.6
  • Eclipse 3.7


Slide 4

Prerequisites

To follow this tutorial, you must have knowledge of data types in Java.


If not, for relevant tutorial please visit our website as shown

Slide 5

Type Conversion

Type conversion means converting data from one data type to another.


Let us see how it it done.


Switch to Eclipse

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.


I have created a class TypeConversion and added the main method to it.


Now let us create a few variables.

In the main method, type

int a = 5;

float b;

b = a;


System.out.println(b);

int a equal to 5

float b

b equal to a


I have created two variables. a which is and integer and b which is a float.


I’m storing the integer value in a float variable.


Let us see what the float variable now contains.


System dot out dot println b '


Save the file and run it.

Save and run. Point to output We can see that the integer 5 has been converted to float 5.0


This type of conversion is called implicit conversion.


As the name goes, the value is automatically converted to suit the data type.


Now let us convert float to an int, using the same method.

Change a = 5 to a

Change b to b = 2.5f;

Change b = a to a = b;


Change b to a inside println


Point to the cross mark on left.

Remove the 5 float b equal to 2.5f and let us store b in a and print the value of a .

Save the file.


 we   see that there is an error.


The error message reads, Type mismatch: cannot convert from float to int


It means Implicit conversion is possible only from an int to a float but not the the other way.


To convert a float to int we have to use explicit conversion.


Let us see how to do so.

Change a = b to a = (int) b

and point to that line

We do that by using an int in parentheses.before the variable


This statement says the data in the variable b has to be converted to int data type and stored in a.

save and run. Point to output save and run the file


As we can see, the float value has been converted to int.


But to suit the data type, the data has been changed accordingly.


Explicit conversion can also be used to convert data from int to float.


Let us try anthe previous example.

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)


save and run. Point to output

int a =5, float b, b = float a

System.out.println(b); We are using Explicit conversion to convert integer to a float

Save the file and Run it.


we  see that   the int value has been converted  to  a float value 


Let us see what happens when we convert a character to integer.

Remove everything in the main function and type the following


char c = 'm';

int a;

a = (int) c;

System.out.println(a);

int a, charc char c equal to 'm';

' a equal to (int) c;

System dot out dot println ' a.

We are convewrting character m to an integer and printing the value Let us save and run it

Save and run. Point to output As we can see, the output is 109 which the ascii value of m.


It means when a char is converted to int, its ascii value is stored.


Let us try this with a digit.

Change c = 'm' to c = ' ' char c = digit 5
Save it and run it 
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.


To get the number, we have to use a string and convert it to an integer.


Now let us see how to do so.

Remove everyting in main function and type the following.


String sHeight = “6”;

int h = (int) sHeight;

System.out.println(h);

Clean up the main function
type


String sHeight equal to in double quotes 6 semicolon.


int h equal to explicit conversion to int of sHeight and System dot out dot println h Save the file.

I’ve created a string variable with value 6 and i am trying to convert it to an integer but we see that their is an error


Point to cross mark on the left And the error message reads Cannot cast from String to int.


This means for converting strings, we cannot use implicit or explicit conversion.


It must be done by other methods. let us use them

Remove the int h... and type

int h = Integer.parseInt(sHeight);

Remove the int h... and type Integer dot parseInt sHeight .

Save the file and run it


Save and run. Point to output we see that value has been successfully converted to an integer.

To do this we use the parseInt method of the integer module. Now let us see what happens if their are more than one digits like 6543

Save the file and run it


Point to console
We see that again strings contain the number has been successfully converted to an integer

Now let us see what happens if the strings is a floating point number Change 6543 to65.43.So we have a floating point number in the string and we are converting it to an integer Save the file run it.


Save and run. Point to output We see that their is an error This happens beacuse we cannot convert strings which contains floating point number into an integer


We have to convert it TO  afloat.Let us see how to do so;

First data type should be float, Second we will use float . parsefloat, 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. Save the file run it. We can see the string containing a floating point number has been successfully converted into floating point number. And this is how we do implicit and explicit conversion and How do we converts strings to numbers

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

This brings us 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 explicit conversin and How to convert strings to numbers.

Slide 7Assignment


As an assignment for this tutorial Read about Integer.toString and Float.toString.


Find out What do they do?
Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


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
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


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
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Chandrika, Sneha