How To Implement Type Conversion In Java
Rate this article
0 out of 5
In this tutorial we will show you how to implement type conversion in java. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code

type_conversion.java

//Implementing various type conversions in java

import java.io.*;

//creating the class type_conversion

class type_conversion
{
public static void main(String args[])
{
byte b=42;
float f=5.67f;
int i=257,k=97;
char c='a';
double d=323.142;
short s=1234;
long l=123456789;

System.out.println(" TYPE CONVERSION IN JAVA.");
System.out.println(" ************************");

System.out.println(" Type conversion of integer to byte.");
b=(byte)i;
System.out.println(" i="+i+" and b="+b);

System.out.println(" Type conversion of integer to float.");
f=(float)i;
System.out.println(" i="+i+" and f="+f);

System.out.println(" Type conversion of integer to double.");
d=(double)i;
System.out.println(" i="+i+" and d="+d);

System.out.println(" Type conversion of double to integer.");
i=(int)d;
System.out.println(" d="+d+" and i="+i);

System.out.println(" Type conversion of double to byte.");
b=(byte)d;
System.out.println(" d="+d+" and b="+b);

System.out.println(" Type conversion of float to integer.");
i=(int)f;
System.out.println(" f="+f+" and i="+i);

System.out.println(" Type conversion of float to byte.");
b=(byte)f;
System.out.println(" f="+f+" and b="+b);

System.out.println(" Type conversion of long to short.");
s=(short)l;
System.out.println(" l="+l+" and s="+s);

System.out.println(" Type conversion of char to integer.");
i=(int)c;
System.out.println(" c="+c+" and i="+i);

System.out.println(" Type conversion of integer to character.");
c=(char)97;
System.out.println(" f=97"+" and c="+c);

System.out.println(" Type conversion of string to integer.");
String st=Integer.toString(k);
System.out.println(" k="+k+" and st="+st);
}
}



Source Code

About The Author
TekTips Videos

Number Of articles Posted      : 27
Number Of Messages Posted  : 0
Joined Date : 2013/2/04
   
Author
Messages
Posted:
Post Your Comments  
Name (Max 50 Chars)
Comments
 
 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android